function FormHooks::formViewsUiConfigItemFormAlter

Same name and namespace in other branches
  1. main core/themes/admin/src/Hook/FormHooks.php \Drupal\admin\Hook\FormHooks::formViewsUiConfigItemFormAlter()

Implements hook_form_FORM_ID_alter() for the Views UI config form.

Attributes

#[Hook('form_views_ui_config_item_form_alter')]

File

core/themes/admin/src/Hook/FormHooks.php, line 553

Class

FormHooks
Provides form related hook implementations.

Namespace

Drupal\admin\Hook

Code

public function formViewsUiConfigItemFormAlter(array &$form, FormStateInterface $form_state) : void {
  $type = $form_state->get('type');
  if ($type === 'filter') {
    // Remove clearfix classes from several elements. They add unwanted
    // whitespace and are no longer needed because uses of `float:` in this
    // form have been removed.
    // @todo Many of the changes to classes within this conditional may not be
    //   needed or require refactoring in https://drupal.org/node/3164890
    unset($form['options']['clear_markup_start'], $form['options']['clear_markup_end']);
    if (isset($form['options']['expose_button']['#prefix'])) {
      $form['options']['expose_button']['#prefix'] = str_replace('clearfix', '', $form['options']['expose_button']['#prefix']);
    }
    if (isset($form['options']['group_button']['#prefix'])) {
      $form['options']['group_button']['#prefix'] = str_replace('clearfix', '', $form['options']['group_button']['#prefix']);
    }
    // Remove `views-(direction)-(amount)` classes, replace with
    // `views-group-box--operator`, and add a `views-config-group-region`
    // wrapper.
    if (isset($form['options']['operator']['#prefix'])) {
      foreach ([
        'views-left-30',
        'views-left-40',
      ] as $left_class) {
        if (str_contains($form['options']['operator']['#prefix'], $left_class)) {
          $form['options']['operator']['#prefix'] = '<div class="views-config-group-region">' . str_replace($left_class, 'views-group-box--operator', $form['options']['operator']['#prefix']);
          $form['options']['value']['#suffix'] = ($form['options']['value']['#suffix'] ?? '') . '</div>';
        }
      }
    }
    // Some instances of this form input have an added wrapper that needs to
    // be removed in order to style these forms consistently.
    // @see \Drupal\views\Plugin\views\filter\InOperator::valueForm
    $wrapper_div_to_remove = '<div id="edit-options-value-wrapper">';
    if (isset($form['options']['value']['#prefix']) && str_contains($form['options']['value']['#prefix'], $wrapper_div_to_remove)) {
      $form['options']['value']['#prefix'] = str_replace($wrapper_div_to_remove, '', $form['options']['value']['#prefix']);
      $form['options']['value']['#suffix'] = preg_replace('/<\\/div>/', '', $form['options']['value']['#suffix'], 1);
    }
    if (isset($form['options']['value']['#prefix'])) {
      foreach ([
        'views-right-70',
        'views-right-60',
      ] as $right_class) {
        if (str_contains($form['options']['value']['#prefix'], $right_class)) {
          $form['options']['value']['#prefix'] = str_replace($right_class, 'views-group-box--value', $form['options']['value']['#prefix']);
        }
      }
    }
    // If the form includes a `value` field, the `.views-group-box--value` and
    // `.views-group-box` classes must be present in a wrapper div. Add them
    // here if it they are not yet present.
    if (!isset($form['options']['value']['#prefix']) || !str_contains($form['options']['value']['#prefix'], 'views-group-box--value')) {
      $prefix = $form['options']['value']['#prefix'] ?? '';
      $suffix = $form['options']['value']['#suffix'] ?? '';
      $form['options']['value']['#prefix'] = '<div class="views-group-box views-group-box--value">' . $prefix;
      $form['options']['value']['#suffix'] = $suffix . '</div>';
    }
    // If operator or value have no children, remove them from the render
    // array so their prefixes and suffixes aren't added without any content.
    foreach ([
      'operator',
      'value',
    ] as $form_item) {
      if (isset($form['options'][$form_item]['#prefix']) && count($form['options'][$form_item]) === 2 && $form['options'][$form_item]['#suffix']) {
        unset($form['options'][$form_item]);
      }
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.