-1

I tried to using hook_menu_alter but it doesn't work. It doesn't call page callback function.

function mymodule_menu_alter(&$items) {
    $items['taxonomy/term/%taxonomy_term']['page callback'] = 'mymodule_taxonomy_term_page';
}

function mymodule_taxonomy_term_page($term) {
    echo "hello"; die;
}
6
  • Check your spelling
    – Clive
    Commented Mar 14, 2018 at 15:13
  • My bad. But still it is not working.
    – Reshma
    Commented Mar 14, 2018 at 16:25
  • Was the typo present in the module, or did you just make a typo when copying the code in the question?
    – apaderno
    Commented Mar 14, 2018 at 18:29
  • If you just made a typo when copying the code in the question, how do you check your code is not working? Which user account do you use to test it?
    – apaderno
    Commented Mar 14, 2018 at 18:30
  • The typo it was in question, Its not working.
    – Reshma
    Commented Mar 15, 2018 at 6:27

1 Answer 1

0

I think I understand... You can use a hook_preprocess_page and global $user to determine the page the user is viewing, and the roles the current user has, and then do a redirect or whatever if they aren't allowed.... Example:

/**
 * Implements hook_preprocess_HOOK().
 */
function mymodule_preprocess_page(&$variables)
{
  $current_path = current_path();
  $url_components = parse_url($current_path);
  $protected_taxonomy_term = "whatever_taxonomy_term_you_want";
  if (strpos($url_components['path'], $protected_taxonomy_term) !== FALSE) {
    // User is on the path
    global $user;
    $user_roles = $user->roles;
    // Change to whatever role you want...
    $allowed_role = "authenticated user";
    if (in_array($allowed_role, $user_roles)) {
      drupal_set_message("USER IS ALLOWED ACCESS");
    }
    else {
      // User not allowed, Redirect user to another page..
      drupal_goto("<front>");
    }
  }
}
2
  • Thanks for your quick response, This solution wont work, page is going into infinite loop.
    – Reshma
    Commented Mar 14, 2018 at 17:13
  • I tested my specific code, and it was working for me. Of course, I could imagine you've modified it significantly. If the drupal_goto() logic was redirecting to a page that triggers this logic, than I can see why you would have an infinite loop problem. Perhaps you can update your question with the current version of your code to take a closer look? Commented Mar 15, 2018 at 17:56

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.