You are here

function menu_link_weight_node_element_process in Menu Link Weight 8

Same name and namespace in other branches
  1. 8.2 menu_link_weight.node.inc \menu_link_weight_node_element_process()
  2. 7 menu_link_weight.module \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_form_node_form_alter in ./menu_link_weight.node.inc
Implements hook_form_BASE_FORM_ID_alter() for node forms.

File

./menu_link_weight.node.inc, line 123

Code

function menu_link_weight_node_element_process($element, FormStateInterface $form_state, &$complete_form) {

  // Find out which parent to select after loading (or AJAX reloading) the form.
  $parent_element = $complete_form['menu']['link']['menu_parent'];
  $parent_value = _menu_link_weight_get_parent_value_from_element($parent_element, $form_state);
  if (strstr($parent_value, ':')) {

    // The parent value is of the form "$menu_name:" or
    // "$menu_name:$link_plugin_id".

    /** @see \Drupal\Core\Menu\MenuParentFormSelector::getParentSelectOptions() */
    list($menu_name, $parent_id) = explode(':', $parent_value, 2);
  }
  else {
    return $element;
  }

  // Get the menu title for the parent based on the current menu selection.
  $url = Url::fromRoute('entity.menu.edit_form', array(
    'menu' => $menu_name,
  ));
  if ($parent_id === '') {
    $label = Menu::load($menu_name)
      ->label();
  }
  else {

    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');

    /** @var \Drupal\Core\Menu\MenuLinkInterface $parent_link */
    $parent_link = $menu_link_manager
      ->createInstance($parent_id);
    $label = $parent_link
      ->getTitle();

    /** @see menu_link_weight_form_menu_form_alter() */
    $html_id = 'menu-link-weight-link-id-' . Html::getId($parent_id);
    $url
      ->setOption('fragment', $html_id);
  }
  $generated_url = $url
    ->toString(TRUE);
  $generated_url
    ->applyTo($element);
  $element['#description'] = t('Change the weight of the links within the <a href=":url">@menu</a> menu by dragging the items up/down.', array(
    ':url' => $generated_url
      ->getGeneratedUrl(),
    '@menu' => $label,
  ));

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

  // Get the title for the current menu link.
  $new_item_title = $form_state
    ->hasValue([
    'menu',
    'link_title',
  ]) ? $form_state
    ->getValue([
    'menu',
    'link_title',
  ]) : $complete_form['menu']['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, $parent_id, $current_mlid, $new_item_title);

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

  // Build the form elements using the options array.
  foreach ($options as $link_id => $info) {
    $element['table'][$link_id] = array(
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
      '#weight' => $info['weight'],
      'name' => $info['title'],
      'weight' => array(
        '#type' => 'weight',
        '#title' => t('Weight'),
        '#default_value' => $info['weight'],
        '#delta' => MENU_LINK_WEIGHT_MAX_DELTA,
        '#title_display' => 'invisible',
        '#attributes' => [
          'class' => [
            'menu-link-weight-item-weight',
          ],
        ],
      ),
    );
    if ($link_id != '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'][$link_id] = array(
        '#type' => 'hidden',
        '#value' => $info['db_weight'],
      );
    }
  }
  return $element;
}