You are here

function claro_preprocess_field_multiple_value_form in Drupal 8

Same name and namespace in other branches
  1. 9 core/themes/claro/claro.theme \claro_preprocess_field_multiple_value_form()

Implements hook_preprocess_HOOK() for field_multiple_value_form.

File

core/themes/claro/claro.theme, line 799
Functions to support theming in the Claro theme.

Code

function claro_preprocess_field_multiple_value_form(&$variables) {

  // Make disabled available for the template.
  $variables['disabled'] = !empty($variables['element']['#disabled']);
  if ($variables['multiple']) {

    // Add an additional CSS class for the field label table cell.
    // This repeats the logic of template_preprocess_field_multiple_value_form()
    // without using '#prefix' and '#suffix' for the wrapper element.
    //
    // If the field is multiple, we don't have to check the existence of the
    // table header cell.
    //
    // @see template_preprocess_field_multiple_value_form().
    $header_attributes = [
      'class' => [
        'form-item__label',
        'form-item__label--multiple-value-form',
      ],
    ];
    if (!empty($variables['element']['#required'])) {
      $header_attributes['class'][] = 'js-form-required';
      $header_attributes['class'][] = 'form-required';
    }

    // Using array_key_first() for addressing the first header cell would be
    // more elegant here, but we can rely on the related theme.inc preprocess.
    $variables['table']['#header'][0]['data'] = [
      '#type' => 'html_tag',
      '#tag' => 'h4',
      '#value' => $variables['element']['#title'],
      '#attributes' => $header_attributes,
    ];
    if ($variables['disabled']) {
      $variables['table']['#attributes']['class'][] = 'tabledrag-disabled';
      $variables['table']['#attributes']['class'][] = 'js-tabledrag-disabled';

      // We will add the 'is-disabled' CSS class to the disabled table header
      // cells.
      $header_attributes['class'][] = 'is-disabled';
      foreach ($variables['table']['#header'] as &$cell) {
        if (is_array($cell) && isset($cell['data'])) {
          $cell = $cell + [
            'class' => [],
          ];
          $cell['class'][] = 'is-disabled';
        }
        else {

          // We have to modify the structure of this header cell.
          $cell = [
            'data' => $cell,
            'class' => [
              'is-disabled',
            ],
          ];
        }
      }
    }

    // Make add-more button smaller.
    if (!empty($variables['button'])) {
      $variables['button']['#attributes']['class'][] = 'button--small';
    }
  }
}