You are here

function menu_link_weight_node_element_process in Menu Link Weight 7

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

Process callback for the menu link weight element.

1 string reference to 'menu_link_weight_node_element_process'
menu_link_weight_element_info_alter in ./menu_link_weight.module
Implements hook_element_info_alter().

File

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

Code

function menu_link_weight_node_element_process($element, &$form_state, &$complete_form) {
  if (empty($element['#menu_link_weight_process'])) {
    return $element;
  }
  $options = array();

  // Find out which parent to select after loading (or AJAX reloading) the form.
  $parent_element = $complete_form['menu']['link']['parent'];
  $parent_value = _menu_link_weight_get_parent_value_from_element($parent_element, $form_state);
  if (strstr($parent_value, ':')) {
    list($menu_name, $plid) = explode(':', $parent_value);
  }
  else {
    return $element;
  }

  // Get the menu title for the parent based on the current menu selection.
  if ($plid == 0) {
    $menu = menu_load($menu_name);
    $parent_menu_link = l($menu['title'], 'admin/structure/menu/manage/' . $menu_name);
  }
  else {
    $link = menu_link_load($plid);
    $parent_menu_link = l($link['link_title'], 'admin/structure/menu/manage/' . $menu_name, array(
      'attributes' => array(
        'target' => '_blank',
      ),
      'fragment' => 'menu-link-weight-mlid-' . $plid,
      'html' => TRUE,
    ));
  }
  $element['#description'] = t('Change the weight of the links within the !parent_menu_link menu by dragging the items up/down.', array(
    '!parent_menu_link' => filter_xss_admin($parent_menu_link),
  ));

  // Get the ID for the current menu link.
  $current_mlid = !empty($complete_form['menu']['link']['mlid']['#value']) ? $complete_form['menu']['link']['mlid']['#value'] : NULL;

  // Get the title for the current menu link.
  $new_item_title = isset($form_state['values']['menu']['link_title']) ? $form_state['values']['menu']['link_title'] : $complete_form['menu']['link']['link_title']['#default_value'];

  // Get the options array we will use to build the form elements for every
  // menu link.
  $options = _menu_link_weight_get_options($menu_name, $plid, $current_mlid, $new_item_title);

  // Allow other modules to reorder the options, if applicable.
  if (!empty($form_state['menu_link_weight_relative_position'][$parent_value])) {
    $options = _menu_link_weight_reorder_options($options, $form_state['menu_link_weight_relative_position'][$parent_value]);
  }

  // Build the form elements using the options array.
  foreach ($options as $mlid => $info) {
    $element[$mlid] = array(
      'name' => array(
        // The title should have already been sanitized here!
        '#markup' => $info['title'],
      ),
      'weight' => array(
        '#type' => 'weight',
        '#title' => t('Weight'),
        '#default_value' => $info['weight'],
        '#delta' => MENU_LINK_WEIGHT_MAX_DELTA,
        '#title_display' => 'invisible',
      ),
    );
    if ($mlid != 'link_current') {

      // Save the current weight in the database of the rendered menu links
      // into the form, so that we can give an error if the weights have changed
      // by the time the form is submitted.
      $complete_form['menu']['link']['db_weights'][$mlid] = array(
        '#type' => 'hidden',
        '#value' => $info['db_weight'],
      );
    }
  }
  return $element;
}