You are here

function menu_link_weight_node_form_validate in Menu Link Weight 7

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

Validation hook for the menu_link weight element.

1 string reference to 'menu_link_weight_node_form_validate'
menu_link_weight_form_node_form_alter in ./menu_link_weight.module
Implements hook_form_FORM_ID_alter().

File

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

Code

function menu_link_weight_node_form_validate($form, &$form_state) {

  // PAreview gives the following warning when using form_state['input']:
  // "Do not use the raw form_state['input'], use form_state['values'] instead
  // where possible".
  // In this case we *have* to use the raw 'input' values though, as 'values'
  // has already been overwritten with the current weights during
  // menu_link_weight_node_element_process().
  $parent_element = $form['menu']['link']['parent'];
  $value = _menu_link_weight_get_parent_value_from_element($parent_element, $form_state);
  if (strstr($value, ':') && !empty($form_state['input']['menu']['menu_link_weight'])) {
    list($menu_name, $plid) = explode(':', $value);

    // Loop through submitted weights and confirm that the parent link/menu
    // are still the same.
    $weights = $form_state['input']['menu']['menu_link_weight'];
    unset($weights['link_current']);
    foreach ($weights as $mlid => $weight) {
      $link = menu_link_load($mlid);
      if ($link['plid'] != $plid) {
        form_set_error('menu_link_weight', t('The parent for one of the menu links have been changed by another user, please try again.'));
      }
      if ($link['menu_name'] != $menu_name) {
        form_set_error('menu_link_weight', t('The menu for one of the menu links have been changed by another user, please try again.'));
      }
    }
  }
  if (isset($form_state['input']['menu']['db_weights'])) {

    // Check that children and weights are still the same as when the form was
    // loaded. Get the old values from the "hidden" form elements.
    foreach ($form_state['input']['menu']['db_weights'] as $mlid => $db_weight) {
      $link = menu_link_load($mlid);
      if (!empty($link)) {
        if ($link['weight'] != $db_weight) {
          form_set_error('menu_link_weight', t('The menu link weights have been changed by another user, please try again.'));
        }
      }
    }
  }
}