0

Im trying to add a button to the toolbar for my module. Now I know I can just it in the menu and blah blah but I want it to make it when the module is activated. I think im on the right track using hook_menu however it doesn't exactly specify the callback to do this.

This is what I have so far....

function mysite_form_menu() {
$items = array();
$items['admin/config/mysite_form/ads'] = array(
    'title' => 'mysite Ad\'s',
    'description' => 'Allows administrators to adjust ads.',
    'page callback' => 'ads_page_callback',
    'access callback' => 'current_user_hide_tabs',
    'page arguments' => array('ads'),
    'access arguments' => array('administer editor mysite settings'),
    'file' => 'thank_you_ads.inc',
    'file path' => drupal_get_path('module', 'mysite_form'),
);
}
function current_user_hide_tabs() {
  global $user;

  if (isset($user->roles['3']) || isset($user->roles['5'])) :
  return true;
  else :
  return false;
  endif;

}

This is a image of the toolbar I am talking about so there is not any confusion. enter image description here

1

The buttons on the toolbar are automatically added when the module is enabled, as long as the menu items defined from the module with hook_menu() are sub-menus of the admin menu defined from the System module.

The function that renders the toolbar is toolbar_view(), which contains the following code.

  // Retrieve the admin menu from the database.
  $links = toolbar_menu_navigation_links(toolbar_get_menu_tree());
  $build['toolbar_menu'] = array(
    '#theme' => 'links__toolbar_menu', 
    '#links' => $links, 
    '#attributes' => array('id' => 'toolbar-menu'), 
    '#heading' => array(
      'text' => t('Administrative toolbar'),
      'level' => 'h2',
      'class' => 'element-invisible',
    ),
  );

toolbar_menu_navigation_links() excludes the hidden menu items, and the ones to which the user doesn't have access.

toolbar_view() is called from toolbar_pre_render(), which is the pre-render function added from toolbar_page_build() to $page['page_top']['toolbar']. Once the module is disabled, the menu items should not be anymore visible on the toolbar. The Toolbar module doesn't use any cache; I would expect the change to be immediately visible.

To appear on the toolbar, the menu item needs to be a direct sub-menu of admin. Differently, as in your case, it will not appear in the toolbar.

To test it, I added the following code in a module I use to test code.

/**
 * Implements hook_menu().
 */
function code_testing_menu() {
  $items['admin/mysite_form'] = array(
    'title' => 'My site form',
    'description' => 'Set custom settings for a site',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('code_testing_mysite_form'),
    'access callback' => 'code_testing_mysite_access',
  );

  return $items;
}

function code_testing_mysite_form($form, &$form_state) {
  $form['code_testing_switch_lights'] = array(
    '#type' => 'checkbox', 
    '#title' => t('Switch lights on.'),
    '#default_value' => variable_get('code_testing_switch_lights', TRUE),
  );

  return $form;
}

function code_testing_mysite_access() {
  return user_access('administer site');
}

What I obtained is visible in the following screenshots.

screenshot

screenshot

Once the module is disabled, the toolbar doesn't contain anymore the menu.

screenshot

I would rather not put a menu item directly, under admin, if not in particular cases. If the menu item just shows a form for a module's settings, I would not show it in the toolbar. If, vice versa, the module would show a page of links like the Advanced help does, then I could consider adding the menu item directly under admin.

In the other cases, I would consider adding links through the Shortcut module. This would mean adding code in hook_enable() that:

  • Creates a new shortcut set to use for the users with a specific role
  • Sets the new shortcut set as default for those users

In hook_disable(), the code should:

  • Change back to the old shortcut set

Once the module is uninstalled, it should remove the shortcut set it created.

As alternative, the module could add a new link to the shortcut set used from user with a specific role.

The functions that such code would use are the following ones:

Playing with the Shortcut module, and manually adding a link to a menu item defined in hook_menu() from a module that I then disable, I noticed the link is automatically removed from the shortcut set once the module is disabled. Even after re-enabling the module, the link is not anymore shown as part of the shortcut set.

screenshot

screenshot

The second screenshot shows the shortcut content after the module is disabled.

After I re-enable the module (which defines the "My site form" menu callback I shown before), this is what appears as content of the shortcut set. I didn't save the shortcut set, since the other screenshot.

screenshot

  • I know this is a bit late but is it used like this? If so it removes the button from the toolbar completely and just doesn't show the button for the admin – Jeremy Love Dec 8 '12 at 1:11
  • If you are asking if you should define a menu item under admin just to make it appear in the toolbar, then no: Normally a module should not create menu callback for menu items that are added directly under admin. There are modules that do this, such as the Advanced help module, but that is a specific case. Normally, modules should add their menu under admin/config, or admin/structure. I didn't have problems defining a menu callback for admin/config; it was shown basing on the access defined from the access callback, and only for those users for which the access callback returned TRUE. – kiamlaluno Dec 8 '12 at 2:43
  • Ok, I think I get what you are saying and I had my url structre incorrect, thats why it would not show up on my end, however my problem is this. Although the menu does appear in the toolbar now the problem is it only appears only when logged in as a admin, is there a certain access argument to make it appear for other user roles, for example if i have a role named editor should the menu button appear using this argument. 'access arguments' => array('administer editor site'), or is this wrong? – Jeremy Love Dec 10 '12 at 18:44
  • The permission must be one defined from a module, and assigned to that role; you could also use a custom permission like "access editor menu" if there is a module defining it. "administer editor site" is fine too, but from the name I get it is a permission you want for administrator users, not common users. (It's not something Drupal forces; it's just something that is commonly done, to assign any "administer X" permission to administrator users.) – kiamlaluno Dec 10 '12 at 19:45

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.