I'm trying with this:
$block = module_invoke('user', 'block_view', 'login');
It returns an empty array.
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.
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:
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.
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'));
}