1

I'm using hook_menu() in my module which defines a menu item in the primary-links.

I flushed the cache and refreshed the modules page but the item didn't show in the primary-links.
I checked the menu_links and menu_router tables and the item was defined it them.

function mymodule_menu() {
  $items['newuser'] = array(
    'title' => 'Register',
    'menu_name' => 'primary-links',
    'page callback' => 'register_browse_page',
    'access callback' => 'newuser_access_callback',
  );

  return $items;
}

function newuser_access_callback() {
  global $user;

  if ($user->uid == 0 && user_access('access content')) {
    return TRUE;
  }

  return FALSE;
}

function register_browse_page() {
  $form = drupal_get_form("newuser_form");
  return theme("newuser_form_page", $form);
}

What is the problem? How do I fix it?

  • It's likely to be the access callback...try unconditionally returning TRUE as a test. If the menu item appears you know that's the problem – Clive Mar 7 '12 at 14:06
1

This condition in your newuser_access_callback tells to your link to shows only to anonymous.

if ($user->uid == 0 && user_access('access content')) {

If you want see it as admin also, you should add yourself like that

if ($user->uid == 1 || ($user->uid == 0 && user_access('access content'))) {
| improve this answer | |
  • ok i tested this code and it worked correctly i thought that the link should appear in the primary links admin page despite the permissions but that was wrong thank you – Muhamad Bhaa Asfour Mar 7 '12 at 18:02
0

If you want the menu to be visible to anonymous users, you should use the following code.

function mymodule_menu() {
  $items['newuser'] = array(
    'title' => 'Register',
    'menu_name' => 'primary-links',
    'page callback' => 'register_browse_page',
    'access callback' => 'user_is_anonymous',
  );

  return $items;
}

This is similar to the code used in openid_menu().

function openid_menu() {
  $items['openid/authenticate'] = array(
    'title' => 'OpenID Login', 
    'page callback' => 'openid_authentication_page', 
    'access callback' => 'user_is_anonymous', 
    'type' => MENU_CALLBACK, 
    'file' => 'openid.pages.inc',
  );

  //...
}

user_is_anonymous() also allows administrators to see items for anonymous when administering, as long as the global $menu_admin is set to TRUE. This is done before checking which users have access to a menu handler.

function user_is_anonymous() {
  // Menu administrators can see items for anonymous when administering.
  return !$GLOBALS['user']->uid || !empty($GLOBALS['menu_admin']);
}

Normally, user_is_logged_in() should instead be used as access callback.

Checking if the user anonymous user has the permission to access to the content is not necessary; plus, the code would work if you assign that permission to anonymous users.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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