Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a menu router with type MENU_CALLBACK and it's 'page callback' property points to a function that implements render API, as follow:

 /** Implements hook_menu **/
function custom_render_menu() {
  $items['register-profile'] = array(
    'page callback' => 'custom_render_register_profile',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/** Implements callback page for register-profile **/
function custom_render_register_profile() {
  $user_register_form = drupal_get_form('user_registration_form');
  $markup = user_is_logged_in() ? t("Alternative Content") : drupal_render($user_register_form);
  $output = array( '#markup' => $markup);
  return $output;
}

If the user is not logged in, the registration form displays correctly, but if the user is logged in, the page was redirected to user/uid router, showing user's profile. How can I display an alternative content instead of redirecting the user to their user page?

share|improve this question
    
Where does the redirect happen? –  Scott Joudry Jul 22 at 14:19
    
when a logged in user accesses localhost/register-profile. –  rwaery Jul 22 at 14:24
    
There is nothing in the code you provided that will cause a redirect. –  Scott Joudry Jul 22 at 15:06

3 Answers 3

function custom_render_register_profile() {
global $user;
global $base_url;
$uid=$user->uid;
if($uid)
{
drupal_goto('url where you want to take login user..like $base_url./node/1');
}
else
{
  $user_register_form = drupal_get_form('user_registration_form');
  $markup =drupal_render($user_register_form);
  $output = array( '#markup' => $markup);
  return $output;
}
}
share|improve this answer
    
Why couldn't I use just one menu router to do this? Shouldn't the render API knows what I put into the '#markup'? –  rwaery Jul 22 at 14:49

How about this:

/** Implements callback page for register-profile **/
function custom_render_register_profile() {
  $markup = user_is_logged_in() ? t("Alternative Content") : drupal_render(drupal_get_form('user_registration_form'));
  $output = array( '#markup' => $markup);
  return $output;
}
share|improve this answer
    
I tried that too. I'm so puzzled now. In fact for a kick, I tried this: $bazinga = TRUE $markup = $bazinga ? t("Alt Content") : drupal_render(drupal_get_form('user_register_form')); And it will still redirect to user/nid –  rwaery Jul 22 at 15:10
up vote 1 down vote accepted

Scott's answer helped me on this. For some reason, if you had called drupal_get_form('user_register_form'), even if you assigned it to a variable that isn't being concatenated to '#markup', drupal will still go through the process of checking if the user is logged in or not.

Calling the drupal_get_form('user_register_form') right when assigning the '#markup' does the trick.

function custom_render_register_profile() {
  $output = array( 
    '#markup' => user_is_logged_in() ? t("Alternative Content") : drupal_render(drupal_get_form('user_registration_form')); 
  );
  return $output;
}

edited: The code above will work but will result in a strict warning: only variables should be passed by reference. Use elaborated if statement instead:

function custom_render_register_profile() {
  $output = array();
  if(user_is_logged_in()) {
    $output['#markup'] = t('Alt. Content');
  }else{
    $form_registration = drupal_get_form('user_register_form');
    $output['#markup'] = drupal_render($form_registration);
  }

 $output += array(
  //Add more attributes to your $output
 );
 return $output;
}

As to why the ternary version of the if statement wouldn't work, I'm not sure.

share|improve this answer
    
I still don't see the redirect. Can you provide some code for user_registration_form? –  Scott Joudry Jul 22 at 15:32
    
I must have misspelled that. It is 'user_register_form' On the default user login screen, it is the form rendered under first tab, labelled: "Create New Account". I bet the same behaviour would show I was programmatically rendering 'user_login', in the same manner. –  rwaery Jul 22 at 15:38
1  
Despite its name, drupal_get_form() doesn't just build the form, it processes the input including executing the redirect. Btw there's no need to render the form directly, you can just return it - and allow it be modified by hook_page_alter() before final rendering. –  Andy Jul 23 at 10:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.