According to hook_menu API docs:
"options": An array of options to be passed to l() when generating a link from this menu item.
I have tried all possible variations for the options parameter in the following code, but the class never gets added to the rendered link:
function eventbook_menu(){
$items = array();
$items['node/%/blog'] = array(
'title' => 'Blog',
'description' => 'Blog for this event.',
'type' => MENU_LOCAL_TASK,
'page callback' => '_eventbook_event_blog_callback',
'page arguments' => array(1),
'file' => 'eventbook.callback.inc',
'access callback' => '_eventbook_menu_show_event_tabs',
'options' => array('attributes'=>array('class'=>array('event-blog-link'))));
return $items;
}
I am not sure if this is a bug with the docs and options are not used at all. But as it is currently mentioned in the docs, I am filing it as a bug in the code.
Comments
Comment #1
grendzy commentedIt works for me - I tested by adding some 'options' to node_menu(). The class was added to /node/add links as expected.
You may need to clear your cache - changes to hook_menu implementations don't take effect until the next time the menu router is rebuilt.
Comment #2
johnbarclay commentedI can reproduce this bug. Basically the hook_menu items array 'options' parameter is ignored for MENU_LOCAL_TASK so tabs don't behave as they should when the options are set.
"options": An array of options to be passed to l() when generating a link from this menu item.
An easy way to replicate this is with the menu_example module. add the following code at the bottom of hook_menu and the link attributes will be applied to menu items in the menu blocks, but not menu items in tabs. Attached is an image.
I'm guesing the bug is in _menu_item_localize() but am not familiar with the menu system. Its not in the theming layer as the render array is missing the options data.
Comment #3
DizzyC commentedI can confirm this bug.
It doesn't work for MENU_LOCAL_ACTION either.
Comment #4
simon georges commentedSubscribe.
Comment #5
bfroehle commentedI think this is a documentation issue. The local tasks & local actions are built entirely out of the {menu_router} table. The options array seems to be only for the actual menu links (i.e., things in the user configurable menus).
Comment #6
Anonymous (not verified) commentedI can confirm that this is also not working on D6
Comment #7
ParisLiakos commentedsubscribe
Comment #8
ParisLiakos commentedComment #9
aspilicious commentedComment #10
jhodgdonSo, have the menu system component maintainers agreed that this is a purely documentation bug? If so, could you please make an issue summary (or at least a comment saying "this is what happens and it needs to be documented here")? And then please change the component to "documentation" instead of "menu system". Because after reading this through, I'm still not sure whether it's a code or doc bug, and if doc, what needs to be documented.
Thanks!
Comment #11
ParisLiakos commentedand if its a documentation issue it is a feature request,cause the ability to add id and classes to your generated links is a pretty common need
Comment #12
martijn houtman commentedConfirmed that it does not work for MENU_LOCAL_ACTION., Drupal 7.10.
Comment #13
martijn houtman commentedIt appears that menu_local_tasks() is looking in the menu_router table, while the options are in the menu_links table, so it will never append the options to the links. I've created a work-around which manually queries the options and merges them with localized_options:
Comment #14
jhodgdonOK. For the moment, I think we should document the current behavior in Drupal 7 and 8. And then we should consider a feature request for Drupal 8 to make the tabs work with options.
Those seem like two separate issues. So I'm filing a separate Documentation issue to document the current behavior, and leaving this one as the feature request.
#1417754: hook_menu() docs need to explain that options don't work for tabs
Comment #15
mlncn commentedRelated, i think, is local tasks (tabs) failing to print a title tooltip text even when a description is handed in with the link, [#492380: Menu item "description" is not applied to title attribute of menu tab links]
Comment #16
jienckebd commentedHere's a D7 solution that works for me. Add the attributes/options to your hook_menu as expected, and add this to your theme's template.php. It will add the attributes/options from the menu_links table to your local tasks.
Here's an example menu item:
Comment #17
japo32 commentedThanks @jienckebd
I used this form for the query.
Comment #18
dawehnerWe no longer have
hook_menu()Comment #19
jhodgdonWe do in D7. :)
Comment #20
David_Rothstein commentedSo is this possible in Drupal 8, then - and if so, how?
I just spent a little while trying to figure it out, both by adding an "options" key to the .yml file and by trying to use hook_menu_links_discovered_alter() to add it there, and haven't got anywhere with it yet - actually I couldn't even figure out how to get it to work on regular menu links either...
Comment #21
David_Rothstein commentedI've gotten it to work with regular menu links using hook_menu_links_discovered_alter() now (fun fact - if you want a Drupal 8 hook implementation to take effect, it helps if you put it in a Drupal 8 codebase, not in the Drupal 7 codebase you have running alongside it :)
But it only seemed to work for regular menu links, not action links or local tasks - so I think this issue is valid for Drupal 8.
I'm still not sure how to do any of this via the .yml files though (which is the most direct equivalent of hook_menu in Drupal 7).
Comment #22
David_Rothstein commentedActually I would bet you can do this for local tasks and actions via hook_menu_local_tasks_alter() and hook_menu_local_actions_alter() (untested)... but that still doesn't help if you want to simply do it when defining the menu item in the first place.
Comment #23
dawehnerYou can totally do that in Drupal 8: Both
\Drupal\Core\Menu\LocalTaskDefault::getOptionsand\Drupal\Core\Menu\LocalActionDefault::getOptionsallows you define those entries inside the yml file (and all corresponding alter hooks).
Comment #24
jhodgdonHm. In #20, it looks like David tried to do this and it didn't work. @David_Rothstein: Maybe you can post your YML file; @dawehner: Maybe you can post what you think should work, and we can see if we can figure this out?
Comment #25
dawehnerTried out
Note: On both
/adminand/admin/indexon seven we ignore attributes on menu links due to the specialcore/themes/seven/templates/admin-block-content.html.twigComment #32
svendecabooterFYI: I was able to add a class attribute to a local task via the .yml file definition.
Here is an example of the code inside my [MODULENAME].links.task.yml, since no explicit example was provided yet in this issue:
Comment #37
larowlanBased on #32 this is a documentation task now