1
function welcome_menu() {
  $item=array();
  $item['welcome'] = array(
    'title' => 'welcome',
    'access callback' => 'welcome_init', 
    'page callback' => 'welcome_test',
    'type' => MENU_CALLBACK,
  ); 

  return $item;
}

/*
 * Implements hook_init().
 */
function welcome_init() {
  if (empty($GLOBALS['user']->uid)&& arg(0)!='welcome') {
    drupal_goto('welcome');
  }
}

function welcome_test() {
  $output = 'welcome';
  return $output;
}

I want to auto redirect the anonymous users to the login page, and avoid that an authenticated user can see this page. how I use user_login_block() as page callback?

2 Answers 2

1

An access callback must return a boolean value that is TRUE when users can access the page, and FALSE when they cannot access the page; it should not redirect users to other pages.

Drupal has a function that returns TRUE when the user is anonymous.

function welcome_menu() {
  $items = array();

  $items['welcome'] = array(
    'title' => 'welcome',
    'access callback' => 'user_is_anonymous', 
    'page callback' => 'welcome_test',
    'type' => MENU_CALLBACK,
  ); 

  return $item;
}

The code is similar to the one used by the User module.

function user_menu() {
  // …

  $items['user/login'] = array(
    'title' => 'Log in', 
    'access callback' => 'user_is_anonymous', 
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  // …
  return $items;
}

To show the login block using your menu callback, you need to change your function code with the following one.

function welcome_test() {
  return drupal_get_form('user_login_block');
}

hook_init() is not invoked for cached pages, and Drupal normally caches the pages for anonymous users (when it is set to do so).

9
  • thank you.got it. but how to make the page callback call the login block?
    – enjoylife
    Commented Mar 26, 2011 at 3:19
  • and why this can;t work? function welcome_init() { if (empty($GLOBALS['user']->uid)&& arg(0)!='welcome') { drupal_goto('welcome'); } }
    – enjoylife
    Commented Mar 26, 2011 at 3:23
  • 1
    @enjoylife: Use drupal_get_form('user_login_block') and return the value you obtain from the function.
    – apaderno
    Commented Mar 26, 2011 at 3:37
  • 1
    @enjoylife: If you have enabled the page caching for anonymous users, hook_init() doesn't have effect on the cached pages, except when it adds JavaScript or CSS files to the page.
    – apaderno
    Commented Mar 26, 2011 at 14:18
  • 1
    @enjoylife: You can use the function defined from any modules that are enabled. If the module is not always enabled (which means is not one of the modules that are required by Drupal to work) then your module needs to declare its dependency from that module; user.module is necessary for Drupal to work, and you don't need to declare its dependency from user.module. You can use the functions defined from other modules as callbacks; if the function is not in the module file, then you can use file and file path to tell Drupal which is the file to load to get that function.
    – apaderno
    Commented Mar 26, 2011 at 14:30
2

The most obvious thing is that it does not follow Drupal's coding standards at all. It makes it very difficult to read.

hook_init() is it's own hook, it should not be used as an access callbacks. Access callbacks need to return either TRUE or FALSE.

1
  • how to correct the code? thank you.
    – enjoylife
    Commented Mar 26, 2011 at 2:20

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.