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
add comment

1 Answer 1

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
add comment

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.