0

I need to add a new condition to access callback function for a menu path.

For it I write hook_menu_alter and redefine there new access callback function.

function mymodule_menu_alter(&$items){
  $items[node/add']['access callback'] = 'mymodule_custom_callback'; 
}

And I have a question. I want this function to not overwrite previous access callback function, but add a new condition, which would be added to previous conditions for access callback.

How to do it most correctly ?

And is it possibly to do it not knowing the name of default access callback function ? For example, default access callback function for path node/add is _node_add_access().

1 Answer 1

2

You can call default access callback function in your custom access callback and if default access callback return TRUE, then you can check your own conditions and return TRUE/FALSE. Check example below:

function mymodule_custom_callback() {
  // If node access return TRUE.
  if (_node_add_access()) {
    // Do your processing here.
    if (condition) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }
  return FALSE;
}

And is it possibly to do it not knowing the name of default access callback function ? For example, default access callback function for path node/add is _node_add_access().

In such cases, you can either find it by debugging (print $items['node/add']; exit;) or you can append that as an access argument and call it from your custom callback. Something like below:

function mymodule_menu_alter(&$items){
  $items['node/add']['access arguments'][] = $items['node/add']['access callback'];
  $items['node/add']['access callback'] = 'mymodule_custom_callback'; 
}

function mymodule_custom_callback($function) {
  // If node access return TRUE.
  if (function_exists($function) && $function()) {
    // Do your processing here.
    if (condition) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }
  return FALSE;
}
2
  • The OP expressly said not knowing the name of the default access callback function.
    – apaderno
    Dec 22, 2016 at 11:08
  • That is, to add my condition into access callback I can't do without information about default access function name ? And I can't merely add my access callback function into some list, which would include all the access callback functions. And afterwarls they would be executed one by one when a user tried to go to that menu ? path ? In my opinion, it's not not very convenient when developer has to search needed default function digging in the in the kernel of Drupal 7. Although, of course, it would probably increase his skills. Dec 22, 2016 at 11:20

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.