I'm trying to get it so that when a user registers, they get taken to someplace other than the home page. This is a temporary thing, so I'm just hacking /includes/form.inc on line 196, just before the return to the drupal_submit_form() function. for now I've jsut hardcoded a

$goto = (empty($goto)) ? "forum" : $goto;

That works fine, but I would prefer to do it without a hack, but I'm just not seeing how to do it. Any suggestions?

As a warning to future readers, this hack will redirect pretty much any form submission that sends one to the home page, not just registration. In my case that's actually what I want because only the forums run on Drupal right now on the site in question, so I want people to always end up either there or at the drupal-designated page after any form submisison, so that's my rationale in this very specific situation. May not work for you.

Comments

mkabot68’s picture

Have you looked at the Login Destination module?

http://drupal.org/node/69051

Might do the trick for you without modifying the core :)

ergophobe’s picture

Great! That's such a simple module that I think I should be able to make a registration_destination module off that module.

Yosemite Explorer - hiking and climbing in Yosemite (drupal)

ergophobe’s picture

I took Moshe's module and made a few additions so that

1. you can set the landing page for both the login page and the registration page
2. you can set the relevant paths in the admin->settings->landing_page

Not sure if Moshe would want to just add this to his module, in which case it isn't really named right anymore, or whether it should be it's own module. I currently call it "landing_page"

Here's the code. Basically, install is simple.

1. Create a file called landing_page.module
2. copy the code into that file
3. put the file either in /modules or /modules/landing_page
4. go to admin->modules and enable the landing_page module
5. go to admin->settings->landing_page and set your paths.


/**
 * Implementation of hook_help().
 */
function landing_page_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Allows admin to set destination for users upon login or registration.');
  }
}

/**
 * Implementation of hook_form_alter().
 */
function landing_page_form_alter($form_id, &$form) {

  $landing_path = '';
  if ($form_id == 'user_register' && variable_get('landing_set_registration', 0)) {
    $landing_path = variable_get('landing_registration_page', '');
  }
  elseif (($form_id == 'user_login_block' || $form_id == 'user_login')
          && variable_get('landing_set_login', 0)) {
    $landing_path = variable_get('landing_login_page', '');
  }
  if (!empty($landing_path))
  {
     $form['#action'] = url($_GET['q'], "destination=$landing_path");
  }
}

/**
 * Implementation of hook_settings().
 */
function landing_page_settings() {
  $form['dest_settings']['landing_set_login'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Enable custom login destination page?'),
    '#default_value' => variable_get('landing_set_login', 0),
    '#description'   => t("Allows you to send users to any drupal path upon successful login."),
  );
  $form['dest_settings']['landing_login_page'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Set path for login destination (must have login destination redirect enabled)'),
    '#default_value' => variable_get('landing_login_page', ''),
    '#size'          => 30,
    '#description'   => t('Enter a drupal path.  Examples: <em>user</em> or <em>node/59</em>'),
  );
  $form['dest_settings']['landing_set_registration'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Enable custom registration destination page?'),
    '#default_value' => variable_get('landing_set_registration', 0),
    '#description'   => t("Allows you to send users to any drupal path upon successful registration."),
  );
  $form['dest_settings']['landing_registration_page'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Set path for registration destination (must have registration destination redirect enabled)'),
    '#default_value' => variable_get('landing_registration_page', ''),
    '#size'          => 30,
    '#description'   => t('Enter a drupal path.  Examples: <em>user</em> or <em>node/59</em>'),
  );
  return $form;
}

Yosemite Explorer - hiking and climbing in Yosemite (drupal)

reggie75’s picture

you shouldnt be putting the "?>" at the end.

i spent quite some time getting blank screens before i realised i have to remove the end quotes.

on the whole beautiful and simpe .. proved very useful ..
in fact this seems so primary i wonder why it isnt built into the core itself ...

question:
not that i know too much of the drupal core, but .. do hacks like these ending up affecting the speed of the whole system itself? as i guess the above function will now be called almost everytime a form is submitted.. does that affect performance significantly?

thanks nonetheless,
i'm going with it as this module is just what i was looking for!

rajesh

ergophobe’s picture

Every module file that invokes php should close it, so the ?> should be necessary, so what you're saying is incorrect (i.e. that I shouldn't have it, though of course I believe you were getting errors.

That begs the question, though, of why this caused you problems. I'm trying to remember whether or not I made any debug changes since posting that, but I don't think so. I don't suppose you still have any debug output (blank screen probalby means that you're having debug output logged rather than displayed). My guess is that somehow you ended up with two instances of the ?> because your text editor does that when you create a new PHP file. Is that possible?

Anyway, I don't think a little thing like this will slow drupal down much, but yes, some modules can cause a big drag on the server. Anything that takes a bunch of DB calls to function is going to have a bigger effect. The only real solution is to get XDebug and CacheGrind and see what functions use the most resources and see how you can optimize those.

Yosemite Explorer - hiking and climbing in Yosemite (drupal)

nevets’s picture

The closing PHP tag is optional an current Drupal coding standards suggest leaving it off. You can read about this at http://drupal.org/node/545, which include a link to a more in depth discussion of the topic.

reggie75’s picture

For all the help ..

ergophobe’s picture

I stand corrected. I'm one of those people who always use braces even for one-line statements and have always used the ?> closing tag. But indeed it turns out that as of 4.7 the preferred way to code for Drupal is without it and the official PHP manual recommends leaving it off. I have to say the rationale is convincing - it does no harm and can do some good with avoiding "headers already sent" errors.

Thanks for the heads up. I'll change my coding style.

That perhaps explains the problems the other poster was having - if he pasted it into his editor with whitespace at the end, he would have gotten the error and if he has his server set not to display errors, a blank screen.

Yosemite Explorer - hiking and climbing in Yosemite (drupal)

Hayakawa’s picture

it's not working in my setup. it's 4.7 on RH linux. it even does not show description and no link on settings. then i entered manually entering URL. but it didn't work.

reggie75’s picture

please be a little but more desciptive.

1. what do you mean by "manually entering URL"?
2. did you try removing the ?> at the end of the module?

Hayakawa’s picture

yes of course.
1. http://site.com/admin/settings/landing. shows nothing only save and back to default buttons.
2. yes i did.

interestingly, when i disable this module and enable login_destination module it works. the problem with login_destination is that when you change destination path it does only recognize the first entered one.
thanks

ergophobe’s picture

Actually, it seems that I have a problem with it too. It logs me in fine from the login page, but not from the login block. Not sure why that would be.
[edit: I take it back - it's working fine for me now and must have been working fine before since I haven't had complaints from the site owner]

As for the rest, the settings part works fine for me, so I don't know what's happening there. One thing - what did you call the module file? You are using the URL /landing not landing_page. Is that what you want?

Yosemite Explorer - hiking and climbing in Yosemite (drupal)

Hayakawa’s picture

i tried from login page it worked! but when i use login block it's not working at all. Rick did you make something special to work again from login block?

i could now enter settings file with landing_page and saved settings is working from login page. but still there is no link in settings and no description in modules page. it may be a small bug.

now i verified that this module has the same bug as login_destination. even if you change destination value it only remembers the first one regardless of any new value.

ergophobe’s picture

It appears that if you try to log in from a page where you are denied access (i.e. you go to /admin or /logout when you're logged out anyway) it fails. If you go to a page that you could access anyway without being logged in, it works for me, whether from the login block or from the login page.

This is getting quite annoying. Can't do anything more on it this morning, but I will spend some time on it this afternoon. Maybe I'll drop a PM to Moshe and see if he can help out since it's probably dead obvious what the problem is.

Tom

Yosemite Explorer - hiking and climbing in Yosemite (drupal)

Hayakawa’s picture

Any success, Tom?

ergophobe’s picture

I haven't really had time to get back to it yet. I'll post here for sure when I do. Also, feel free to drop me a PM so you'll be in my email so I can send you anything I learn.

Yosemite Explorer - hiking and climbing in Yosemite (drupal)

mlncn’s picture

I was trying to change the Access Denied page that users receive if they find their way to the user/register page while they are logged in. I wanted it a redirect to the user page or anything else, but after reading this thread I'll just avoid hardcoding the registration link anywhere.

It shouldn't be so hard to get rid of unnecessary access denied pages! Is it better to submit a feature request now, or wait for 5.0? I haven't yet checked if this is the way the user/register page works in CVS.

~ben

Agaric Design Collective, http://AgaricDesign.com - "Open Source Web Development"

benjamin, Agaric

ergophobe’s picture

I think redirecting to the user page or the home page on access denied would solve the problem with my little module and probably other issues. If that is the way it works in 5.0, then I'll make this into a patch to Moshe's module. Otherwise I will just keep this as a thread here until I can get it worked out (or someone else can).

Yosemite Explorer - hiking and climbing in Yosemite (drupal)

ttesteve’s picture

This worked perfectly for me, first time, no tweaking or hacking at all required!

Many thanks for this!

Steve

KMNL’s picture

Anyone figure this out for 5.x? Whats the best solution for controlling first login?

gpk’s picture

logintoboggan module may also be worth a look - has option to control where user is sent after registration (but not login, from what I can see)! Anyway may give some ideas for custom module.

emackn’s picture

/**
 * Allows admins to set custom post login and post registration pages
 * Take from http://drupal.org/node/69993
 */

/**
 * Implement hook_menu()
 *
 */
function landing_page_menu($may_cache) {
    $links = array();
    if($may_cache) {
        $links[] = array(
            'path' => 'admin/settings/landing-page',
            'title' => 'Landing Page',
            'description' => t('Defing the page that registration and login actions are redirected too'),
            'callback' => 'drupal_get_form',
            'callback arguments' => 'landing_page_admin_settings',
            'access' => user_access('administer site configuration'),
            'type' => MENU_NORMAL_ITEM,
        );
    }
    return $links;
}

/**
* Implementation of hook_help().
*/
function landing_page_help($section) {
    switch ($section) {
        case 'admin/modules#description':
            return t('<b>P2:</b> Allows admin to set destination for users upon login or registration.');
    }
}

/**
* Implementation of hook_form_alter().
*/
function landing_page_form_alter($form_id, &$form) {
    $landing_path = '';

    if ($form_id == 'user_register' && variable_get('landing_set_registration', 0)) {
        $landing_path = variable_get('landing_registration_page', 'registration_thanks');
    }
    elseif (($form_id == 'user_login_block' || $form_id == 'user_login')
                && variable_get('landing_set_login', 0)) {
        $landing_path = variable_get('landing_login_page', '');
    }

    if (($form_id == 'user_register' || $form_id == 'user_login_block' || $form_id == 'user_login') && $landing_path)
        $form['#redirect'] = $landing_path;
}

/**
* Implementation of hook_settings().
*/
function landing_page_admin_settings() {
  $form['dest_settings']['landing_set_login'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Enable custom login destination page?'),
    '#default_value' => variable_get('landing_set_login', 0),
    '#description'   => t("Allows you to send users to any drupal path upon successful login."),
  );
  $form['dest_settings']['landing_login_page'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Set path for login destination (must have login destination redirect enabled)'),       
    '#default_value' => variable_get('landing_login_page', ''),                                                   
    '#size'          => 30,                                                                                       
    '#description'   => t('Enter a drupal path.  Examples: <em>user</em> or <em>node/59</em>'),                   
  );                                                                                                              
  $form['dest_settings']['landing_set_registration'] = array(                                                     
    '#type'          => 'checkbox',                                                                               
    '#title'         => t('Enable custom registration destination page?'),                                        
    '#default_value' => variable_get('landing_set_registration', 0),                                              
    '#description'   => t("Allows you to send users to any drupal path upon successful registration."),           
  );                                                                                                              
  $form['dest_settings']['landing_registration_page'] = array(                                                    
    '#type'          => 'textfield',                                                                              
    '#title'         => t('Set path for registration destination (must have registration destination redirect enabled)'),       
    '#default_value' => variable_get('landing_registration_page', ''),                                            
    '#size'          => 30,                                                                                       
    '#description'   => t('Enter a drupal path.  Examples: <em>user</em> or <em>node/59</em>'),                   
  );                                                                                                              
                                                                                                                  
  return system_settings_form($form);                                                                             
}                                                       

And don't forget the .info file

rc2020’s picture

The module seemingly works fine, but when I enter in the path for a user to be redirected to after registration, it does not work properly. I specify the path as user/, but when I register an account to test, the user does not actually get logged in, I go to the default user/register page, and the url is mysite.com/user/23049201 or some numbers which make no sense.

I did make an info file, and the administration menu of the module works fine...

gpk’s picture

>but when I register an account to test, the user does not actually get logged in
As implemented here, registering does not automatically log you in. See prior comment about logintoboggan.

You might want to have a look down the list of modules here http://drupal.org/node/206666#DRUPAL-5 since there may now be something there to do what you want, if you aren't comfortable hacking this code.

gpk
----
www.alexoria.co.uk

Nico_0’s picture

Hi,

This worked very well for me for Drupal 5.
Anyone has this working for D 6 already?

vegeneric’s picture

http://drupal.org/node/151543#comment-1370594 worked for me in d6. here's some sample code from my module:

function my_form_alter(&$form, &$form_state, $form_id) {

  switch ($form_id) {

    case 'user_register':
      // I only want my custom submit function to run when an admin creates a user.
      if (arg(0) == 'admin' && arg(1) == 'user' && arg(2) == 'user' && arg(3) == 'create') {
        $form['#submit'][] = 'my_admin_register';
      }
      break;
  }
}

function my_admin_register($form, &$form_state) {
  // I only want my custom redirect to happen if a specific role has been selected for the new user
  if ($form_state['values']['roles'][8] == 8) {
    // Because my submit function runs after the user modules, the account has already been created and I can get the uid of the new user.
    $redirect = 'node/add/property-owner-conact-sheet/'. $form_state['user']->uid;
    $_REQUEST['destination'] = $redirect;
    return;
  }
}

rwohleb’s picture

The cleanest way to do this in D6, if all you want is to set a default redirect for user/register, is something like the following:

<?php
function mymodule_form_alter($form_id, &$form) {
  if ($form_id == 'user_register') {
    $form['#redirect'] = 'my/new/page';
  }
}
?>

This allows you to still override it with "?destination=path" if you need to.

MC_McMic’s picture

So, this goes in template.php?

gpk’s picture

You would need to put that in a custom module.