You are here

function gin_lb_preprocess_field_multiple_value_form in Gin Layout Builder 1.0.x

Implements hook_preprocess_HOOK() for field_multiple_value_form.

File

./gin_lb.module, line 649
Provides hooks for gin_lb module.

Code

function gin_lb_preprocess_field_multiple_value_form(&$variables) {

  // Add gin_lb_form attribute to tables.
  if ($variables['element']['#gin_lb_form'] ?? NULL) {
    $variables['table']['#attributes']['class'][] = 'glb-table';

    // 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';
      }
    }
  }
}