You are here

function claro_form_views_ui_config_item_form_alter in Drupal 9

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

Implements hook_form_FORM_ID_alter() for the Views UI config form.

File

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

Code

function claro_form_views_ui_config_item_form_alter(array &$form, FormStateInterface $form_state) {
  $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']);
    unset($form['options']['clear_markup_end']);
    $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 (strpos($form['options']['operator']['#prefix'], $left_class) !== FALSE) {
          $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']) && strpos($form['options']['value']['#prefix'], $wrapper_div_to_remove) !== FALSE) {
      $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 (strpos($form['options']['value']['#prefix'], $right_class) !== FALSE) {
          $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']) || strpos($form['options']['value']['#prefix'], 'views-group-box--value') === FALSE) {
      $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]) && count($form['options'][$form_item]) === 2 && isset($form['options'][$form_item]['#prefix']) && $form['options'][$form_item]['#suffix']) {
        unset($form['options'][$form_item]);
      }
    }
  }
}