Summary: Local tasks (e.g., tabs) created by modules implementing hook_menu_local_tasks_alter() are only viewable by privileged users, even if the link is to publicly-accessible content. (Put another way, only users with edit permissions on the path in question view the tab.)

I'm speculating this is because the new entry made to $data['tabs'][0]['output'] (per the example in the api docs) isn't populated with nearly any of the options inside the #link array as is found in system-generated local tasks. Even when setting 'access_callback' => TRUE to emulate those system links, the problem persists.

This bug report sprouted from discussion on this g.d.o. post.

The following snippet produces a properly-formed tab for admins, but nothing for other authenticated users or anonymous:

function nga_helpers_menu_local_tasks_alter(&$data, $router_item, $root_path) {
    $data['tabs'][0]['output'][] = array(
      '#theme' => 'menu_local_task',
      '#link' => array(
	'title' => t('Example tab'),
	'href' => 'public/careers',
	'access_callback' => TRUE,
      ),
    );
}

Comments

aspilicious’s picture

The tabs are made to be used as local tasks for admins...
So 'im prety confident this is by design.
Will verify later.

Did you saw this comment?

Brad, i don't know if you know panels or not but you can do what you're trying to do with panels using context to set the tabs. Its kind of a similar way you'd create tabs using the views modules and specifing paths like careers, careers/benefits, etc... but with panels you can actually go a little deeper and specify certain node/add/edit forms, specific nodes, etc... to build your tabs.
The context module may do something similar but i'm not as experienced with that one. Otherwise, perhaps you're looking for a way to just code it yourself.
bradjones1’s picture

aspilicious - Appreciate the reply, helping to move this along.

If this is by design that's fine I guess, though the documentation should at least note that.

The common assumption, I agree, is that these tabs are generally used for admin tasks. However various menu system features are extended or used in creative ways for end-user experience.

I still don't believe this should be marked "works as designed," though. Who's to say only admins could or should use these types of tabs? What if someone wanted to use node revisions and a liberal access policy to allow wiki-like editing of certain pages? That could expose an "edit" tab, or a tab to show related content, or whatever. Since this tab functionality is in core, and there's an API to use and extend it, I think an admin-only limit on display seems arbitrary.

Also, just because a contrib module (like panels) could add tabs, there are many examples in Drupal of achieving the same goal through different means. However if all I'm after is to add a tab for display, I don't see why this part of the menu system couldn't be used.

bradjones1’s picture

Status: Active » Closed (works as designed)

I'm pretty sure this is by design despite my old comments above. I'm sitting in a Drupalcon presentation by Gabor right now about fixes to Drupal core and I think this is just clogging the active queue!

Traverus’s picture

Version: 7.0 » 8.x-dev
Status: Closed (works as designed) » Active

Could we have some more conversation about this, I think either the documentation on the page needs to be changed for the hook in the API docs or this should be allowed.

Adding a access callback much like other menu systems would make sense and I feel like this is a feature that could be used for other means than just administrative tasks.

Thanks!

jacine’s picture

This seems like a bug to me. These tabs have been used for non-administrative links for ages. The user/register and user/login pages are prime examples of this. It also seems kinda crazy to have to use hook_menu() to add tabs for existing pages, especially nodes.

I was able to get this working by manually updating the $data['tabs'][0]['count'] value. Even when new tabs are added the count is never updated after being altered, which results in no tabs displaying despite the fact that the data is there. It's possible I'm doing something wrong in my implementation, since developer is not my primary role, but I don't know...

The code I'm using is here and is in use on this page. You can reproduce by taking the following steps:

  1. Enable the module in the gist.
  2. Enable the contact module and set permissions for anon users to access it.
  3. Add a node and change the paths in the module to point to one in your DB.
  4. Comment out lines 61-63 and log out.

You'll notice that the tabs will appear on the contact page (where an existing menu item already exists and is just altered), and no tabs will appear on the node (because there are no tabs normally). Then enable the last few lines, clear the cache and the tabs will work as expected.

Hope it helps someone.

bradjones1’s picture

@Jacine, can you post the above edit (even if it's just a few lines) as a patch against D8? See the patch guide at http://drupal.org/node/1054616 if you're not familiar - and thanks!

jacine’s picture

@bradjones It's not a patch because don't have the solution. It's a custom module with a few lines of code at the bottom that hacks in a fix. I'm not even positive that it is a bug. I posted it to better describe the issue so that someone who is able to can easily grab the code to test with and maybe add use it for unit tests.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

bethany.g’s picture

This worked for me to show Drupal-provided tabs (e.g. node view/edit/delete/etc), I do not have any custom tasks that need to be displayed.

In the Block Layout (admin/structure/block; make sure you're editing the desired theme), there is a block for Tabs. Configuration for this block seems to have a default visibility restriction by Role, set to Administrator. You can add any additional roles you want to see the block, or alternatively (like I did) uncheck the Administrator role to not restrict the visibility by role.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
larowlan’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)
Issue tags: +Bug Smash Initiative
StatusFileSize
new6.37 KB

This works as designed in D8+

Taking this example:

/**
 * Implements hook_menu_local_tasks_alter().
 */
function MYMODULE_menu_local_tasks_alter(array &$data, string $route_name, CacheableMetadata $cacheability): void {
  $data['tabs'][0]['user.page'] = [
    '#theme' => 'menu_local_task',
    '#link' => [
      'title' => t('My account'),
      'url' => Url::fromRoute('user.page'),
      'localized_options' => [
        'attributes' => [
          'title' => t('Add content'),
        ],
      ],
    ],
  ];
}

It shows for anon users when viewing a node