2

I have created one menu tab in the node page to show statistics of the article using my custom module.

I have implemented hook_menu() as follows:

function mymodule_menu() {

    $items = array();

    $items['node/%node/menu_title'] = array(
        'title' => 'Page Title',
        'page callback' => 'get_statstics',
        'page arguments' => array(1),
        'access callback' => TRUE,
        'type' => MENU_LOCAL_TASK,
    );

    return $items;
}

It is working fine. But I wanted only author of the node to have access to this link and also for specific content type. Now it is showing this link to every node and for every user.

How can I restrict it to specific content type and for author only?

-- Many thanks for your time

3

Under access callback you have TRUE, just change this to a function that evaluates whether the current user is the author of the current node. The function would look like this

function yourmodule_check_author() {
  global $user;
  $node = menu_get_object();
  return $node->uid == $user->uid;
}

Check this post. You will get more insight on the inner workings of the function.

3

This is what I have done:

function mymodule_menu() {

    $items = array();

    $items['node/%node/menu_title'] = array(
        'title' => 'Page Title',
        'page callback' => 'get_statistics',
        'page arguments' => array(1),
        'access callback' => 'my_access_callback',
        'access arguments' => array(1),
        'type' => MENU_LOCAL_TASK,
    );

    return $items;
}

function my_access_callback($node = NULL) {
   if($node->type == "article" && $node->uid == $GLOBALS['user']->uid)
     return TRUE;
   else 
     return FALSE;
}

May help some other.

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.