0

I need to show a menu item only in certain moments. Imagine that I've a form with some options. If 'A' is selected, I need to enable a menu item that has a form for adding information about 'A'.

I have done a module that has a hook_menu like this:

> function secciones_menu() {
>   
> 
>   $items = array();
> 
>       $items['secciones'] = array(
>           'title' => t('Crear Sección'),
>           'description' => t('Crear Secciones'),
>           'page callback' => 'drupal_get_form',
>           'page arguments' => array('secciones_wizard'),
>           'access callback' = TRUE,
>           'type' => MENU_NORMAL_ITEM,
>           'menu_name' => 'main-menu',         'customized' => 1, // Must be 1 in order to not dispaly the link in any language
>           'language'  => 'es', // language is required in order to make the item translatable   );  

return $items; }

And then have my form for 'A'. So how I can enable or disable the menu item, depending a variable value?

Thank you !

EDIT 1:

What I've done is change the access callback and the menu type:

function agenda_menu() {


  $items = array();


  $items['agenda'] = array(
        'title' => t('4. Crear agenda'),
        'description' => t('Crear agenda'),
        'parent'=>array('Configurar Revista'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('agenda_wizard'),
        'access callback' => 'agenda_access_callback',
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'menu_name' => 'main-menu',
        'customized' => 1, // Must be 1 in order to not dispaly the link in any language
            'language'  => 'es', // language is required in order to make the item translatable
  );


  return $items;
}

And then I've done the next code:

function agenda_access_callback(){

    //db query
    //insert that query in $options array
    foreach ($result as $row) {
        $options[$row->id_seccion] = $row->id_seccion;
    }

    if(isset($options)){ //if the array is not empty
        return TRUE; //the menu item is visible
    }
    else{
        return FALSE; //otherwise, is not visible
    }
}

Ok, this is what I have done, but it doesn't work. Any idea? Thank you.

1 Answer 1

0

The usual way to enable a menu item only in certain circumstances is to use a custom access callback, in which you would evaluate whether this option is set. So:

'access callback' = 'my_module_access_callback',
...

function my_module_access_callback() {
   return my_module_test_option_is_true();
}

But it's a little hard to be sure if this will work for your case as you haven't given us much to go on.

2
  • I've edited me question, in order you to see what I have done and what is my objetive. I've done the hook_access_callback() function but it doesn't work. Have you got any idea? Thanks.
    – more770
    Commented Feb 20, 2015 at 8:31
  • If the query in your access callback is coded correctly then it should work. Did you clear cache after your code changes? Commented Feb 20, 2015 at 13:37

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.