0

I have a module below that will only show "hello world" when I am an administrator. I would also like "hello world" to display when I an authenticate user but right now all I am getting is you are not authorised. I have set mymenuitem as the home page and I am using Drupal 7.

 function mymenuitem_menu() {
  $items = array();
  $items['mymenuitem'] = array(
    'title' => 'mymodule',
    'description' => 'my menu access issue',
    'page callback' => 'mymodule_homepage',
    'access arguments' => array('access mymodule homepage'),
    'type' => MENU_LOCAL_TASK,
  );

  return $items;
}


function mymodule_homepage(){
  drupal_set_title(t('menu system'));
  return "hello world";
}

How can I achieve this?

  • I suspect you're logged in as user 1, who by default has permission to view all the menu links of the site; hence you are able to see this page. The other users are not able to view it because you did not define the permission (the value provided in 'access arguments') mentioned. You do so by following the method shown by @Aiias. – AjitS Jun 7 '13 at 7:17
3
  1. Make sure you define your new permission using Drupal 7's hook_permission().
  2. Grant your new permission to authenticated user here: /admin/people/permissions/list

Try this:

function mymodule_permission() {
  return array(
    'access mymodule homepage' => array(
      'title' => t('Access My Module homepage'),
      'description' => t('View My Module homepage.'),
    )
  );
}
  • This is the right way to implement any custom permissions. However, OP mentioned that he needs to show this to authenticated users not anonymous, so you might want to change the point #2 to accommodate it. – AjitS Jun 7 '13 at 7:12
  • @develkar - Nice catch. I have changed my answer accordingly :). – Aiias Jun 7 '13 at 7:17
  • That worked perfectly and it was one hook I was missing thanks – purencool Jun 7 '13 at 7:25
  • @purencool - No problem, happy to help :) – Aiias Jun 7 '13 at 7:26
  • @purencool - If this answered your question, please mark your question as answered so others can refer back to this answer and StackExchange will not consider this as an open question. – Aiias Jun 15 '13 at 21:21

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.