1

Looking into build a top bar to be used for clients for quick access to edit/add pages. Big question I have is, how do you hook this into the Permissions module in core? Say if I want to allow another user to log in and assign this due to certain permissions.

Is this easily done? Or something a bit much?

function mymodule_preprocess_html(&$vars) {
  if (user_access('moderator')) {
    drupal_add_css(drupal_get_path('module', 'mymodule') . '/css');
    $my_links = array();
    if (in_array('manager', $vars['user']->roles)) {
      $my_links[] = array(
        'title' => t('Add new page'),
        'path' => '/location',
      )
      $my_links[] = array(
        'title' => t('Edit pages'),
        'path' => '/location',
      );
    }
  }
}

Seen some things used as below, but having issues...

function mymodule_permission() {
  return array(
    'moderator' => array(
      'title' => t('Use these top links'),
      'description' => t('Blah blah blah.'),
    ),
  );
}
1

First of all I assume that the Admin Menu module isn't suffient for your needs? If not then I think you're approaching the custom module in the incorrect way.

Basically all menu routes in Drupal (node/add for example) use access arguments to determine who can access the route in question. So if you're looking to build a custom admin bar, I would rather create a new custom Drupal menu, and then add all my links in there. Then if a user does not have sufficient access rights to a menu route, Drupal will not display that specific link within the menu to the user.

If your menu links are coming from a custom module that you've written, you could apply the following example to include your module's menu items into your custom menu. You would use this within your hook_menu declarations:

$items['my/custom/route'] = array(
  'title' => 'Custom module section',
  'page callback' => 'mymodule_custom_page',
  'access callback' => 'mymodule_custom_access',    
  'weight' => 0,
  'type' => MENU_NORMAL_ITEM,
  'menu_name' => 'my-custom-menu-id'
);

Using a custom menu instead of using your $my_links[] array you included above, will allow greater control over each menu item within the Drupal UI too.

*EDIT: provided further explanation based on comments*

Regarding your comments I still think your approach of implementing access controls within hook_preprocess_html is not the best way. We should always be looking to separate our application logic from our frontend logic, and using that hook to apply access control goes against that best practise.

I still recommend that you create a custom menu and then add your admin links and restructure as you want it to be. Then set which roles can create each content type within your system from here /admin/people/permissions, and Drupal will automatically exclude the link for content types the logged in user doesn't have access to.

With regards to displaying the menu, you can just assign the menu's block to a region within your theme, and use CSS to apply styling and positioning within the browser. You can also adjust the access permissions for the block, so only certain roles will ever see your custom menu. You can do this by editing the Visibility settings for menu's block.

Wow I didn't expect this to be such a long winded answer haha! :)

| improve this answer | |
  • Thanks for the reply. Yeah, I have used the Admin Menu in the past for such reasons but looking for an easier way to add/remove links right into it. (which if there were a way to have it load an entire custom menu of my choice, that would be amazing). Example of what I do now: I remove the 'Content/Add Content/[list of node types]' and re-add it as 'Add Content/[list of node types]', so I can hide the 'Media' and 'Webform' options and allow one less click for the user to make to add content. --- Via function _admin_menu_output_alter(&$content) – tdm Feb 17 '14 at 22:52
  • OK I see what you're saying, I've editing my answer above. – Alex Kirsten Feb 18 '14 at 13:49
  • Good to know! Used your method and seeing how much quicker it is. Thanks so much for the help again. :) – tdm Feb 20 '14 at 19:16
  • No problem, glad I could help you out :) – Alex Kirsten Feb 21 '14 at 7:17

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.