Hey Everyone,

Ive been working on and off with Drupal for a while but haven't done a huge amount with forms and in particular, forms on the admin side. Ive read quite a bit of material on them and have a vague understanding on how they work but am still having issues with implementing this particular feature.

I want to add an Enabled checkbox to the Menu Settings section in Edit/Add Node. Ive overwritten the menu_form_alter function in my module and added in the enabled checkbox (as seen below).
I know it also has something to do with $menu['mlid']['hidden'] (or something to that effect) but am having problems working it out.

function wander_mod_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
    // Note - doing this to make sure the delete checkbox stays in the form.
    $form['#cache'] = TRUE;

    $form['menu'] = array(
      '#type' => 'fieldset',
      '#title' => t('Menu settings'),
      '#access' => user_access('administer menu'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
      '#weight' => -2,
      '#attributes' => array('class' => 'menu-item-form'),
    );
    $item = $form['#node']->menu;
        
    if ($item['mlid']) {
      // There is an existing link.
      $form['menu']['delete'] = array(
        '#type' => 'checkbox',
        '#title' => t('Delete this menu item.'),
      );
      
    }
    if (!$item['link_title']) {
      $form['menu']['#collapsed'] = TRUE;
    }

    foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
      $form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]);
    }
    $form['menu']['#item'] = $item;

    $form['menu']['link_title'] = array('#type' => 'textfield',
      '#title' => t('Menu link title'),
      '#default_value' => $item['link_title'],
      '#description' => t('The link text corresponding to this item that should appear in the menu. Leave blank if you do not wish to add this post to the menu.'),
      '#required' => FALSE,
    );
    // Generate a list of possible parents (not including this item or descendants).
    $options = menu_parent_options(menu_get_menus(), $item);
    $default = $item['menu_name'] .':'. $item['plid'];
    if (!isset($options[$default])) {
      $default = 'primary-links:0';
    }
    $form['menu']['parent'] = array(
      '#type' => 'select',
      '#title' => t('Parent item'),
      '#default_value' => $default,
      '#options' => $options,
      '#description' => t('The maximum depth for an item and all its children is fixed at !maxdepth. Some menu items may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)),
      '#attributes' => array('class' => 'menu-title-select'),
    );
    $form['#submit'][] = 'menu_node_form_submit';

    $form['menu']['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight'),
      '#delta' => 50,
      '#default_value' => $item['weight'],
      '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
    );
    
    $form['menu']['enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enabled'),
      '#weight' => 50,
      '#default_value' => !$item['hidden'],
      '#description' => t('Menu items that are not enabled will not be listed in any menu.'),
    );
    
  }
}

I have the enabled checkbox showing up in the Menu Settings area but it wont save. I also overwrote menu_form_submit but it doesnt seem to trigger on node save (I put a die in it to test it out).

The function menu_overview_form_submit in menu.admin.inc (in the menu module) also gives a few clues on how to save enabled as it traverses through the menu items, checks which have been changed, flips the hidden value with this

// Hidden is a special case, the value needs to be reversed.
      if ($element['hidden']['#value'] != $element['hidden']['#default_value']) {
        $element['#item']['hidden'] = !$element['hidden']['#value'];
        $updated_items[$mlid] = $element['#item'];
      }

and finally saves the updated items with menu_link_save.


So mostly my questions boil down to:
1. Am I going about this the right way?
2. How are the enabled and hidden values connected and how do you save them?
3. What is the order of function calls to edit and then save the menu settings in the add/edit node form?

Comments

janar’s picture

I used this solution, and it seems working.

http://eagerfish.eu/adding-enabled-checkbox-into-node-menu-settings-in-d...

It uses hook_nodeapi and hook_form_alter

chingmanle’s picture

i follow the instruction from the link you given

I dBug($node), it doesn't contain any $node->menu['hidden'], can you give me the details how it works?!

janar’s picture

Where did you print out node data?
It didn't work or just curious?

Also blog post is littl bit changed form original post. There was a little bug that made it work only for page node type.

suegmune’s picture

Does this work similarly for D7?

I tried using menu_position, but for whatever reason it isn't working properly. Adding the checkbox would probably be our preferred way. OR, even having an action in "Rules" to be able to set menu for that item to be disabled.

Ideas?

rcodina’s picture

I have wrote a Drupal 7 version based on code found on eagerfish.eu post:

https://github.com/rogercodina/node_menusettings_visibility_checkbox