Same name and namespace in other branches
  1. 8.9.x core/includes/theme.inc \template_preprocess_field_multiple_value_form()
  2. 9 core/includes/theme.inc \template_preprocess_field_multiple_value_form()

Prepares variables for individual form element templates.

Default template: field-multiple-value-form.html.twig.

Combines multiple values into a table with drag-n-drop reordering.

Parameters

array $variables: An associative array containing:

  • element: A render element representing the form element.

File

core/includes/theme.inc, line 1613
The theme system, which controls the output of Drupal.

Code

function template_preprocess_field_multiple_value_form(&$variables) {
  $element = $variables['element'];
  $variables['multiple'] = $element['#cardinality_multiple'];
  $variables['attributes'] = $element['#attributes'];
  if ($variables['multiple']) {
    $table_id = Html::getUniqueId($element['#field_name'] . '_values');
    $order_class = $element['#field_name'] . '-delta-order';
    $header_attributes = new Attribute([
      'class' => [
        'label',
      ],
    ]);
    if (!empty($element['#required'])) {
      $header_attributes['class'][] = 'js-form-required';
      $header_attributes['class'][] = 'form-required';
    }
    $header = [
      [
        'data' => [
          '#type' => 'html_tag',
          '#tag' => 'h4',
          '#value' => $element['#title'],
          '#attributes' => $header_attributes,
        ],
        'colspan' => 2,
        'class' => [
          'field-label',
        ],
      ],
      [],
      t('Order', [], [
        'context' => 'Sort order',
      ]),
    ];
    $rows = [];

    // Sort items according to '_weight' (needed when the form comes back after
    // preview or failed validation).
    $items = [];
    $variables['button'] = [];
    foreach (Element::children($element) as $key) {
      if ($key === 'add_more') {
        $variables['button'] =& $element[$key];
      }
      else {
        $items[] =& $element[$key];
      }
    }
    usort($items, '_field_multiple_value_form_sort_helper');

    // Add the items as table rows.
    foreach ($items as $item) {
      $item['_weight']['#attributes']['class'] = [
        $order_class,
      ];

      // Remove weight form element from item render array so it can be rendered
      // in a separate table column.
      $delta_element = $item['_weight'];
      unset($item['_weight']);

      // Render actions in a separate column.
      $actions = [];
      if (isset($item['_actions'])) {
        $actions = $item['_actions'];
        unset($item['_actions']);
      }
      $cells = [
        [
          'data' => '',
          'class' => [
            'field-multiple-drag',
          ],
        ],
        [
          'data' => $item,
        ],
        [
          'data' => $actions,
        ],
        [
          'data' => $delta_element,
          'class' => [
            'delta-order',
          ],
        ],
      ];
      $rows[] = [
        'data' => $cells,
        'class' => [
          'draggable',
        ],
      ];
    }
    $variables['table'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => [
        'id' => $table_id,
        'class' => [
          'field-multiple-table',
        ],
      ],
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => $order_class,
        ],
      ],
    ];
    if (!empty($element['#description'])) {
      $description_id = $element['#attributes']['aria-describedby'];
      $description_attributes['id'] = $description_id;
      $variables['description']['attributes'] = new Attribute($description_attributes);
      $variables['description']['content'] = $element['#description'];

      // Add the description's id to the table aria attributes.
      $variables['table']['#attributes']['aria-describedby'] = $element['#attributes']['aria-describedby'];
    }
  }
  else {
    $variables['elements'] = [];
    foreach (Element::children($element) as $key) {
      $variables['elements'][] = $element[$key];
    }
  }
}