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?

1
  • 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
    Commented Jun 7, 2013 at 7:17

1 Answer 1

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.'),
    )
  );
}
7
  • 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
    Commented Jun 7, 2013 at 7:12
  • @develkar - Nice catch. I have changed my answer accordingly :).
    – Aiias
    Commented Jun 7, 2013 at 7:17
  • That worked perfectly and it was one hook I was missing thanks
    – purencool
    Commented Jun 7, 2013 at 7:25
  • @purencool - No problem, happy to help :)
    – Aiias
    Commented Jun 7, 2013 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
    Commented Jun 15, 2013 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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