1

I'm trying with this:

$block = module_invoke('user', 'block_view', 'login');

It returns an empty array.

3 Answers 3

5

The user_block_view() function has this line in it for the login delta:

if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {

If you're logged in, the function will return an empty array, which is probably what's happening in your case.

If that's not the reason (and even if it is) you can get around it by calling the form function directly:

$form = drupal_get_form('user_login_block');

There doesn't appear to be any extra access checks done in the form function so it should be rendered regardless of whether you're logged in or not.

3

If you look at the code of user_block_view(), the hook you are invoking, you will notice it contains the following code.

case 'login':
  // For usability's sake, avoid showing two login forms on one page.
  if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {

    $block['subject'] = t('User login');
    $block['content'] = drupal_get_form('user_login_block');

The login form is shown only for the anonymous user, and when the user is looking at example.com/user.

Both the restrictions make sense because:

  • An authenticated user cannot log in without first log out. In that case, it doesn't make sense to show the log in form when the user could not use it. It would also make the users think they are not logged in when they are.
  • example.com/user, for not logged-in users, already shows the login form. It doesn't make sense to show it twice.
2
  • Hmmm. I was trying to solve a problem in a somewhat oblique way. I was hopeful that if I rendered the block, and placed it in the content myself, I could add some stuff to the user login form in a template file instead of with hook_form_alter. It doesn't seem to use template files when rendered on user/login.
    – Joren
    Apr 5, 2012 at 0:30
  • Generally, forms don't use template files for rendering. If you need to add fields to the login form, you can implement hook_form_user_login_block_alter(); if you need to theme in a different way the login form, you can implement hook_form_user_login_block_alter() to associate a theme function to the form.
    – apaderno
    Apr 5, 2012 at 0:58
0

If above answer did not solved your problem somehow, try this:

if (user_is_logged_in()) {
echo "not logged in";
}
else{

echo drupal_render(drupal_get_form('user_login'));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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