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

grendzy’s picture

Status: Active » Closed (cannot reproduce)

It 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.

johnbarclay’s picture

Title: hook_menu - options parameter for menu items has no effect » hook_menu - options parameter for items has no effect on MENU_LOCAL_TASK (tabs)
Status: Closed (cannot reproduce) » Active
StatusFileSize
new19.12 KB

I 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.

  $options = array();
  $options['attributes']['class'] = array('element-invisible');
  $options['attributes']['title'] = 'my title';
  foreach ($items as $path => $item) {
    $items[$path]['options'] = $options;
  }
  return $items;

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.

DizzyC’s picture

I can confirm this bug.
It doesn't work for MENU_LOCAL_ACTION either.

simon georges’s picture

Subscribe.

bfroehle’s picture

I 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).

Anonymous’s picture

I can confirm that this is also not working on D6

ParisLiakos’s picture

Version: 7.0 » 8.x-dev

subscribe

ParisLiakos’s picture

aspilicious’s picture

Issue tags: +Needs documentation
jhodgdon’s picture

So, 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!

ParisLiakos’s picture

and 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

martijn houtman’s picture

Confirmed that it does not work for MENU_LOCAL_ACTION., Drupal 7.10.

martijn houtman’s picture

It 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:

function theme_menu_local_action($variables) {
	$link = $variables['element']['#link'];
	 
	$query = db_select('menu_links', 'l')->fields('l', array('options'))->condition('l.router_path', $link['path'])->execute();
	foreach ($query as $result) {
		$link['localized_options'] = array_merge($link['localized_options'], unserialize($result->options)); 
	}
	 
	$output = '<li>';
	if (isset($link['href'])) {
		$output .= l($link['title'], $link['href'], isset($link['localized_options']) ? $link['localized_options'] : array()); 
	}
	elseif (!empty($link['localized_options']['html'])) {
		$output .= $link['title']; 
	}
	else { 
		$output .= check_plain($link['title']);
	}
	$output .= "</li>\n";
	 
	return $output;
}
jhodgdon’s picture

Title: hook_menu - options parameter for items has no effect on MENU_LOCAL_TASK (tabs) » hook_menu - make options parameter work for MENU_LOCAL_TASK/MENU_LOCAL_ACTION (tabs)
Category: bug » feature

OK. 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

mlncn’s picture

Related, 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]

jienckebd’s picture

Here'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.

function MYTHEME_menu_local_task($variables) {
	$menu_item_options = db_query("SELECT options FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $variables['element']['#link']['path']))->fetchField();
	if ($menu_item_options) {
		$menu_item_options = unserialize($menu_item_options);
		$variables['element']['#link']['localized_options'] = $menu_item_options;
	}
	return theme_menu_local_task($variables);
}

Here's an example menu item:

function MYMODULE_menu() {
	$items = array();
	
	$items['member/%/like'] = array(
		'title' => 'Example Link',
		'page callback' => 'mymodule_example_page_callback',
		'page arguments' => array(1),
		'description' => t('Like this member.'),
		'access callback' => 'mymodule_example_access_callback',
		'access arguments' => array(1),
		'weight' => 81,
		'type' => MENU_LOCAL_TASK,
		'options' => array('attributes' => array('class' => array('use-ajax', 'tab-has-icon', 'icon-like', 'primary-relationship-like')))
	);

	return $items;
}
japo32’s picture

Issue summary: View changes

Thanks @jienckebd
I used this form for the query.

    $query = db_select('menu_links', 'm')
             ->fields('m', array('options'))
             ->condition('link_path', $variables['element']['#link']['path']);
    $menu_item_options = $query->execute()->fetchField();
dawehner’s picture

Status: Active » Fixed

We no longer have hook_menu()

jhodgdon’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Fixed » Active
Issue tags: -Needs documentation, -Needs backport to D7

We do in D7. :)

David_Rothstein’s picture

Title: hook_menu - make options parameter work for MENU_LOCAL_TASK/MENU_LOCAL_ACTION (tabs) » Make it possible to add link options when defining local tasks and actions
Version: 7.x-dev » 8.0.x-dev
Issue tags: +Needs backport to D7

So 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...

David_Rothstein’s picture

I'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).

David_Rothstein’s picture

Actually 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.

dawehner’s picture

You can totally do that in Drupal 8: Both \Drupal\Core\Menu\LocalTaskDefault::getOptions and \Drupal\Core\Menu\LocalActionDefault::getOptions
allows you define those entries inside the yml file (and all corresponding alter hooks).

jhodgdon’s picture

Hm. 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?

dawehner’s picture

Tried out

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.

svendecabooter’s picture

FYI: 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:

entity.node.collaborators:
  route_name: entity.node.collaborators
  base_route: entity.node.canonical
  title: 'Manage collaborators'
  options:
    attributes:
      class:
        - 'collaborators'

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

Title: Make it possible to add link options when defining local tasks and actions » Document how to possible to add link options when defining local tasks and actions
Category: Feature request » Task

Based on #32 this is a documentation task now

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.