You are here

function bartik_form_alter in Drupal 9

Same name and namespace in other branches
  1. 8 core/themes/bartik/bartik.theme \bartik_form_alter()
  2. 10 core/themes/bartik/bartik.theme \bartik_form_alter()

Implements hook_form_alter().

File

core/themes/bartik/bartik.theme, line 119
Functions to support theming in the Bartik theme.

Code

function bartik_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Add classes to the search form.
  if (in_array($form_id, [
    'search_block_form',
    'search_form',
  ])) {
    $key = $form_id == 'search_block_form' ? 'actions' : 'basic';
    if (!isset($form[$key]['submit']['#attributes'])) {
      $form[$key]['submit']['#attributes'] = new Attribute();
    }
    $form[$key]['submit']['#attributes']
      ->addClass('search-form__submit');
  }
  $form_object = $form_state
    ->getFormObject();

  // Add class to the Media Library views form.
  if ($form_object instanceof ViewsForm && strpos($form_object
    ->getBaseFormId(), 'views_form_media_library') === 0) {

    // The conditional below exists because the media-library-views-form class
    // is currently added by Classy, but Umami will eventually not use Classy as
    // a base theme.
    // @todo remove conditional, keep class addition in
    //   https://drupal.org/node/3110137
    // @see https://www.drupal.org/node/3109287
    // @see classy_form_alter()
    if (!isset($form['#attributes']['class']) || !in_array('media-library-views-form', $form['#attributes']['class'])) {
      $form['#attributes']['class'][] = 'media-library-views-form';
    }
  }
}