1

I'd like to know if it is possible to create a link only if a certain condition is respected. Example with my current menu:

function mymodule_menu() {
  $items = array();
  $items['mycontent/%my_object/my_place'] = array(
     ---(code)---
  );
  return $items;
}

The link created by this menu should not be available for each instance of my_object. This menu should only be used by objects that have for example $my_object->flag set to TRUE.

In my page callback I could check the object and exit the form if the flag is not set, however the link would be still displayed and people could click on it.

Do you have any idea ? Thank you.

0

You can use an access callback for that sort of thing:

function mymodule_menu() {
  $items = array();
  $items['mycontent/%my_object/my_place'] = array( 
    'access callback' => 'mymodule_foo_access',
    'access arguments' => array(1),
    ...

  );
  return $items;
}

function mymodule_foo_access($object) {
  // Contrived example, use something better
  return $object->flag === TRUE;
}
  • 1
    Thank you, it works like a charm. I was already using the 'access arguments' to check the permissions so I didn't thought about that solution. I modified it to : return user_access('my permission') && $object->flag === TRUE; – azeaze Mar 9 '16 at 13:53

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.