0

I used hook_menu_local_tasks_alter to add new tab items for node pages like so:

function custom_module_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  if (arg(0) == 'admin' || arg(0) != 'node') { return; }

  $href='user/'.arg(1).'/hobbies';
  $data['tabs'][0]['output'][] = array(
    '#theme' => 'menu_local_task',
    '#link' => array(
      'title' => t('Hobbies'),
      'href' => $href,
    ),
  );
  $href='user/'.arg(1).'/events';
  $data['tabs'][0]['output'][] = array(
    '#theme' => 'menu_local_task',
    '#link' => array(
      'title' => t('Events'),
      'href' => $href,
    ),
  );

However while admin can see the new tabs, regular users (Drupal 7) do not. Adding '#access' => TRUE, to item array does not help as hook_menu_local_tasks_alter doesn't have options for access argument.

And they appear normally if users are given node edit permission.

How can I make access to these new tabs without giving users permission to edit their nodes?

0

You have to use hook_permission

So in your module file something like this will do the job. (taken from the hook's page).

function hook_permission() {
 return array(
  'administer my module' => array(
  'title' => t('Administer my module'),
  'description' => t('Perform administration tasks for my module.'),
  ),
 );
}

When you do that you will get a new entry in your permissions page (admin/people/permissions). Enable that for the anonymous users and it will show up ok.

  • Isn't giving module admin permissions to anonymous users is too excessive? :) – Nick Jan 9 '17 at 7:51
  • In your case nothing will be administered, but actually the anonymous users will be able to use the functionality of your module. – pierostz Jan 9 '17 at 7:56
  • that changed nothing by itself, so there is more to do with the hook. – Nick Jan 9 '17 at 8:07
  • I am afraid my custom module has to deal with node access hook as no access settings for itself has any effect. – Nick Jan 9 '17 at 8:14

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.