views:

46

answers:

2

How can I create a form that requires anonymous viewers to register in order to view it?

Right now, I've created a content-type (Submit Plan) which is a primary link available to everyone (anonymous + authenticated). I've restricted 'view' user permissions to anonymous users but they can still see the 'Title' input (I don't want that).

I'd like it so that when an anonymous user clicks on 'Adding a Plan' primary link (the Submit Plan content-type), it goes to the page and says:

  • "You must register for an account" OR
  • Redirects them to the registration page asking them to register to submit a plan.

I've been searching for a module or maybe some code to use but have come up short on this topic. Any help would be appreciated. Thanks!

A: 

This can be done a few ways.

One, in your menu output, you can change the link to Submit Plan like this:

<?php
  global $user;

  if ($user->uid == 0) {
     print '<a href="/user/register">Add a Plan</a>';
  } else {
     print '<a href="/node/add/submit-plan">Add a Plan</a>'l;
  }
?>

The above code looks to see if the user object has a UID. 0 is anonymous, so that will print the link that sends them to register. Otherwise it will take them to the node add form for Submit Plan content type. This also assumes you control your own menu output. You can also override it in a similar manner by using a theme function.

There are a few ways you can do this, so start there and let me know what you think.

Kevin
This might sound stupid but how do you edit the menu output? I'm on the admin account so I'd assume I do have control over that.
Chetan
There are a few ways. You can try this: http://api.drupal.org/api/function/theme_menu_item_link try to override that in your theme, and adapt the above code to work with it. So for example, instead of print -html- you would say $link['href'] = '/user/register' or $link['href'] = '/node/add/submit-plan' if they are logged in.
Kevin
I wouldn't go with this method unless I was also able to make it crystal clear what the rest of the workflow would be and make it automatically happen for the user. (E.g. adjusting the messaging, making their login after confirming their email automatically take them to the create plan page, etc.) Also, an anonymous user might not need to register -- maybe they need to log in.
sprugman
+1  A: 

Following up on my comment on @Kevin's answer, if you're going to be redirecting users to the registration page, I'd strongly consider using something like logintoboggan to make the registration > node-creation process smooth. Otherwise, registration is a multi-step process and I'd imagine it being easy for users to lose their way back to the Add a Plan form in the process.

For the "show links or show the form" direction, there are at least two ways of approaching that: 1) create a custom page where you "import" the add_plan form (or show the links). 2) modify the node/add/plan page itself, either through themeing or the fapi (the forms api).

Without having tried this, I'd probably lean toward method one.

Update: just had another thought: you could also add the plan form to the registration form so they'd fill them out in one shot. I'm not sure of how to do that in general, but the node profile would work if they're only ever going to make one plan, and if not, you can look at how that's put together.

sprugman

related questions