Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am creating a shortcut bar something like below, but unfortuntly it show up despite my wish that I could hide it for anonymous and administrator users/role. I want to display it for editor roles only. How do I achieve this? Thanks

function shortcat_permission() {
  return array(
    'administer shortcat' => array(
      'title' => t('Administer shortcats'),
      'restrict access' => TRUE,
    ),
  );
}

function shortcat_page_alter(&$page) {
       $page['page_top']['#pre_render'][] = 'shortcat_toolbar_pre_render';
   }



function shortcat_renderables() {

  $shortcut_set = array();
  $shortcut_set['my_list'][] = array("#markup" =>"<ul>");
  $shortcut_set['my_list'][] = array("#markup" =>"<li><a href='node/add' class='shlinks'>".t('Add Content')."</a></li>");
  $shortcut_set['my_list'][] = array("#markup" =>"<li><a href='admin/content' class='shlinks'>".t('Find Content')."</li>");
  $shortcut_set['my_list'][] = array("#markup" =>"</ul>");
  return $shortcut_set; 
}


function shortcat_toolbar_pre_render() {
  $links = shortcat_renderables();

  $links['#attached'] = array('css' => array(drupal_get_path('module', 'shortcat') . '/shortcat.css'));
  $links['#prefix'] = '<div class="toolbar-shortcats">';
  $links['#suffix'] = '</div>';

  return $links;
}

I know you can make use of:

global $user;
  if ($user->uid)  {
display it here...
{

but this won't make the user access right accesible in People > Permissions

share|improve this question
    
did you try simple_access module? I think it may help you here. –  saadlulu Feb 15 '12 at 11:57
    
The idea was to learn how hook_permission works :) –  drupal_stuff_alter Feb 15 '12 at 12:32

2 Answers 2

Since you're defining a permission you can make use of the user_access() function to check that the user is allowed to access the shortcut menu. You can actually then use the #access key on your render array to denote whether or not it should be shown:

function shortcat_toolbar_pre_render() {
  $links = shortcat_renderables();

  $links['#attached'] = array('css' => array(drupal_get_path('module', 'shortcat') . '/shortcat.css'));
  $links['#prefix'] = '<div class="toolbar-shortcats">';
  $links['#suffix'] = '</div>';

  $links['#access'] = user_access('administer shortcat');

  return $links;
}

That code will deny access for anyone who doesn't have the administer shortcat permission. All you need to do is make sure the permission is ticked for the editor role in the admin permissions page.

It's worth bearing in mind that user_access() wil always return TRUE if you are logged in as the super user (ID:1). If you need this to be different then the easiest way would be to check that the user physically has the editor role assigned to their account:

global $user;
if (!in_array('edit', $user->roles)) {
  $links['#access'] = FALSE;
}

Hope that helps.

share|improve this answer
    
Clive, i couldn't make the hook_permission and #access works. The only thing that help me fix the problem was this piece of code: global $user; if ($user->uid>1) { $page['page_top']['#pre_render'][] = 'shortcat_toolbar_pre_render'; } –  drupal_stuff_alter Feb 17 '12 at 11:48

I got similar functionality by using the Shortcuts Per Role module, permissions, tweaking of the theme's page.tpl.php file and a little jQuery.

I wanted to have the stock shortcut bar visible for a few roles with shortcut sets assigned by role, but I only wanted the administrative toolbar visible for the administrator role. I set the permissions for the roles I wanted to have the shortcut bar to be able to "Use the administration toolbar", created shortcut sets and assigned them in the Shortcuts Per Role settings.

In my theme's page.tpl.php file, I added the following snippet to pass the current user's roles to be used in my js. Replace 'path to js file' with the actual path to your js file.

drupal_add_js(array('Theme' => array('role' => $GLOBALS['user']->roles)), 'setting');

drupal_add_js(drupal_get_path('theme', 'Theme') . 'path to js file', 'file');

In my theme's js file, I added the following code:

Drupal.behaviors.Theme = {
    attach: function (context, settings) {
      var roleArr = Drupal.settings.Theme.role;
      var isAdmin = false;
      for (role in roleArr) {
        if (roleArr[role] == 'administrator') {
            isAdmin = true;
        }
      }
      if (isAdmin == false) {
        $('.toolbar-menu').remove();
      }
    }
};

This basically just removes the admin toolbar unless the logged in user is an administrator and leaves the shortcut bar intact.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.