7

I'm using Drupal 7, Views 3, and hook_menu_alter

function MYMODULE_menu_alter(&$items){
  $items['client/place/%'] = array(
    'access callback' => 'custom_function',
  );   
}

to override the view visibility (access), also flushing caches, but this doesn't seems to work, can somebody give some light about how to solve this, is this the right way to override path defined by a view ?

thanks in advance

2
  • 1
    I think you may have made a typo there; your question says "access callback" while your sample code sets the "page callback". – marcvangend Mar 13 '13 at 22:24
  • You are right. I just updated it – mauricio Mar 14 '13 at 15:13
9

Reminder: Views module allows you to set the access from Views UI

If you want to create your own logic; this is how your code should look like:

function MYMODULE_menu_alter(&$items){
  $items['client/place/%']['access callback'] = 'MYMODULE_custom_callback'; 
}

function MYMODULE_custom_callback(){ 
  $isValid = false;

  /* your logic */

  return $isValid;
}

Make sure your module is called after the Views module. Usually Views module wight is 10; so your module wight should be 11 or more.

Also, don't forget to clear your cache.

4
  • 1
    hello, thanks. It works. I need more than per role or permission filter (as through views UI). I've searched looking for a way to know which is weight module, here are some methods http://drupal.org/node/110238. – mauricio Mar 14 '13 at 16:19
  • you're welcome, glad it worked for you. – Aboodred1 Mar 14 '13 at 16:22
  • 5
    It's also possible to use hook_module_implements_alter to move your hook_menu_alter implementation after Views, and then see the menu items Views has added. Works about the same as setting the module weight, except you don't have to set an explicit weight for your module. – thirdender Jun 22 '14 at 10:27
  • I had to use %views_arg instead of % for it to work for me. So like this: $items['user/%views_arg/posts']['access callback'] = 'MYMODULE_custom_access_callback'; – jphelan Jan 20 '17 at 22: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.