You are here

function menu_link_weight_form_node_form_alter in Menu Link Weight 7

Same name and namespace in other branches
  1. 8.2 menu_link_weight.node.inc \menu_link_weight_form_node_form_alter()
  2. 8 menu_link_weight.node.inc \menu_link_weight_form_node_form_alter()

Implements hook_form_FORM_ID_alter().

File

./menu_link_weight.module, line 26
Replaces the menu link weight dropdown with a tabledrag widget.

Code

function menu_link_weight_form_node_form_alter(&$form, &$form_state) {
  if (!isset($form['menu']) || !isset($form['menu']['link']) || isset($form['menu']['#access']) && $form['menu']['#access'] === FALSE || isset($form['menu']['link']['parent']['#access']) && $form['menu']['link']['parent']['#access'] === FALSE) {

    // If there is no menu, or the user doesn't have access to the
    // menu link options, then do nothing.
    return;
  }

  // Remove the "weight" widget.
  unset($form['menu']['link']['weight']);

  // Add submission/validation handlers.
  $form['#validate'][] = 'menu_link_weight_node_form_validate';
  $form['#submit'][] = 'menu_link_weight_node_form_submit';

  // Add the Menu Link weight fieldset.
  $form['menu']['link']['menu_link_weight'] = array(
    '#tree' => TRUE,
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#title' => t('Menu link weight'),
    '#theme' => 'menu_link_weight_reorder_element',
    '#prefix' => '<div id="menu-link-weight-wrapper">',
    '#suffix' => '</div>',
    // Tell the fieldset process function to process this field by setting
    // the #menu_link_weight_process flag to TRUE.
    '#menu_link_weight_process' => TRUE,
  );

  // Define the "db_weights" element, which will hold hidden fields with
  // the values of the menu links in the database. Upon validation we will
  // check whether the weights are still the same as when the form was
  // built, to make sure users won't overwrite each other's changes.
  $form['menu']['link']['db_weights'] = array(
    '#tree' => TRUE,
  );
  $form['#attached']['js'][] = drupal_get_path('module', 'menu_link_weight') . '/js/menu_link_weight.js';
  if (module_exists('hierarchical_select')) {
    $form['#attached']['js'][] = drupal_get_path('module', 'menu_link_weight') . '/js/menu_link_weight.hierarchical_select.js';
  }

  // Define the AJAX callback for changes in the Parent element.
  $form['menu']['link']['parent']['#ajax'] = array(
    'callback' => 'menu_link_weight_parent_ajax_callback',
    'wrapper' => 'menu-link-weight-wrapper',
  );

  // This next button will not be displayed if JS is enabled.
  $form['menu']['link']['menu_link_weight_nojs'] = array(
    '#type' => 'submit',
    '#value' => menu_link_weight_get_button_text(),
    '#prefix' => '<noscript>',
    '#suffix' => '</noscript>',
    // No need to validate when submitting this.
    '#limit_validation_errors' => array(),
    '#validate' => array(),
    '#submit' => array(
      'menu_link_weight_node_form_submit',
    ),
  );
}