1

I have this code in the module cleanetica_advanced.

function cleanetica_advanced_menu() {

    $items['ajax/content/%'] = array(
      'page callback' => 'cleanetica_advanced_ajax_load',
      'page arguments' => array(2),
      'title' => 'Ajax content load',
      'access arguments' => array('access content'), 
      'type' => MENU_CALLBACK
    );

    $items['ajax/validate/form/%'] = array(
      'page callback' => 'cleanetica_advanced_ajax_validate_form',
      'page arguments' => array(3),
      'title' => 'Ajax validate form',
      'access arguments' => array('access content'), 
      'type' => MENU_CALLBACK
    );


    $items['user'] = array(
       'title' => 'blogs',
       'description' => 'Listing of blogs.',
       #'page callback' => 'blog_page',
       'access arguments' => array('access content'),
       'type' => MENU_DEFAULT_LOCAL_TASK
     );

    return $items;
}

For some reason the item "blogs" (which I created for testing only, there are no blogs in our site) does not show in the tabs of the page /users. I need to show it in the /users tabs. What is my error?

I checked that cleanetica_advanced module is loaded by inserting some nonsense in the .module file and seeing that the site ceases to work with an error message. (Then I removed the nonsense.)

By the way, does user in $items['user'] refer to the page where the menu should be shown or to the ID of the menu item itself?

1
  • There's a number of question on the general topic of implementing hook_menu. Please check some of those first.
    – Letharion
    Feb 12, 2013 at 10:51

2 Answers 2

2

"user" is a path already used by the User module; if your module is not executed after the User module (i.e. you are not altering the order hooks are executed), the menu item of the User module takes the precedence. (That is also the wrong way to alter the menu item defined from another module, but your purpose is not that.)

  // Registration and login pages.
  $items['user'] = array(
    'title' => 'User account',
    'title callback' => 'user_menu_title',
    'page callback' => 'user_page',
    'access callback' => TRUE,
    'file' => 'user.pages.inc',
    'weight' => -10,
    'menu_name' => 'user-menu',
  );

If you want to define a menu tab for each user account, as I think, the definition of your menu item should be similar to this, used from the User module.

  $items['user/%user/edit'] = array(
    'title' => 'Edit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('user_profile_form', 1),
    'access callback' => 'user_edit_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'file' => 'user.pages.inc',
  );

For example, something similar to the following would work.

  $items['user/%user/edit'] = array(
    'title' => 'Blogs',
    'description' => 'Listing of blogs.',
    'page callback' => 'cleanetica_advanced_blog_page',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_LOCAL_TASK,
  );
  • The page callback receives an argument: the user object for the user account. It will be called from Drupal as cleanetica_advanced_blog_page($account).
  • Every function implemented by a module needs to be prefixed by its short name.
  • The menu type is MENU_LOCAL_TASK since there is already a MENU_DEFAULT_LOCAL_TASK menu item, and there cannot be two MENU_DEFAULT_LOCAL_TASK; the other tab is also the tab showing the details for the user account, and it is better to keep that as default tab.

If you want to show a list of blog posts global for all the users, then you need to change the path from user, or user/%user/edit, to (for example) blogs. Adding it as tab of the user tab doesn't make any sense, since the page would list content, not a list of users; it would be also shown together user/login, user/register, and it doesn't make sense to show a list of blog posts to who is registering/logging in.

1

user path is defined by core user.module so you cannot overwrite it from hook_menu().

If you want to alter default /user page, you need to implement hook_form_alter. However, note that /user page is the only page that works in maintenance mode and it's a little bit complicated that this page shows login page or redirects to the user's profile depending on whether the user is logged in or or not.

If you want to add a new tab in user's profile(or add a new tab and set it the default), you can (for Drupal 7):

$items['user/%user/blogs'] = array(
  'title' => 'Blogs',
  'page callback' => 'blog_page', // enter your own page callback here
  'page arguments' => array(1), // first argument to your page callback would be $user
  'access arguments' => array('access content'),
  'type' => MENU_LOCAL_TASK,
  'file' => 'cleanetica_advanced.pages.inc', // whatever. If your pagecallback is in the .module file, omit this line. 
);

If you want to set this tab as the default, change MENU_LOCAL_TASK above to MENU_DEFAULT_LOCAL_TASK and implement a hook_menu_alter().

function cleanetica_advanced_menu_alter(&$items) {
  $items['user/%user/view']['type'] = MENU_LOCAL_TASK;
}

Your Answer

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

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