Skip to content

Instantly share code, notes, and snippets.

@jez500
Last active October 12, 2023 12:20
Show Gist options
  • Save jez500/4d1ef061e41f69a964732461cb0ec43a to your computer and use it in GitHub Desktop.
Save jez500/4d1ef061e41f69a964732461cb0ec43a to your computer and use it in GitHub Desktop.
Drupal 8 - Hook form alter views exposed form
<?php
/**
* Implements hook_form_alter().
*/
function MYMODULE_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$view_names = array('my_view_name');
$view = $form_state->getStorage('view');
if ($form_id == 'views_exposed_form' && in_array($view['view']->id(), $view_names)) {
// Do some shilzzle.
}
}
@jigarius
Copy link

jigarius commented Mar 5, 2018

Thanks for the code! But, instead of a hook_form_alter() it would be better to use a hook_form_FORM_ID_alter().

function MYMODULE_form_views_exposed_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $view_ids = ['view_id_1', 'view_id_2'];
  $view = $form_state->getStorage('view');
  if ($form_id == 'views_exposed_form' && in_array($view['view']->id(), $view_ids)) {
    // Do some shlanga.
  }
}

@HongPong
Copy link

Thanks for this. I just would add that the views exposed form block, you may need to look at $form['#id'] instead of $form_id . It seems the $form_id value is quite generic for exposed form blocks.

@ericjgruber
Copy link

ericjgruber commented May 22, 2019

Thanks for this. I just would add that the views exposed form block, you may need to look at $form['#id'] instead of $form_id . It seems the $form_id value is quite generic for exposed form blocks.

I did that today and it worked perfectly. You are correct: exposed forms have a $form_id of views_exposed_form which isn't as specific as one would likely need.

@Nikit
Copy link

Nikit commented Aug 31, 2019

For certain display in view:

/**
 * Implements hook_form_alter().
 */
function MODULE_form_views_exposed_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $view = $form_state->getStorage('view');
  if (($view['view']->id() == 'VIEWNAME') && ($view['view']->current_display == 'DISPLAYNAME')) {
     // some stuff
  }
}

@calvez
Copy link

calvez commented Jun 18, 2021

For certain display in view:

/**
 * Implements hook_form_alter().
 */
function MODULE_form_views_exposed_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $view = $form_state->getStorage('view');
  if (($view['view']->id() == 'VIEWNAME') && ($view['view']->current_display == 'DISPLAYNAME')) {
     // some stuff
  }
}

excellent!

@DamienGR
Copy link

$form_state->getStorage() does not accept arguments.
Use $form_state->get('view') to get a ViewExecutable object

/**
 * Implements hook_form_alter().
 */
function MODULE_form_views_exposed_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $view = $form_state->get('view');
  if (($view->id() == 'VIEWNAME') && ($view->current_display == 'DISPLAYNAME')) {
     // some stuff
  }
}

@featuriz
Copy link

featuriz commented Jan 27, 2022

My code doesn't work, here is my code I'm using drupal9.3x

function mytheme_form_views_exposed_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $view = $form_state->getStorage('view');
  if (($view['view']->id() == 'views-exposed-form-careers-page-1') && ($view['view']->current_display == 'page_1')) {
    $form['actions']['submit_careers']['#value'] = t('Send Message1');
    $form['actions']['submit']['#value'] = t('Apply Nowx1');
  }
}
<form class="views-exposed-form" data-drupal-selector="views-exposed-form-careers-page-1" action="/careers" method="get" id="views-exposed-form-careers-page-1" accept-charset="UTF-8" data-once="form-updated exposed-form" data-drupal-form-fields="edit-title,edit-field-category-target-id,edit-field-location-target-id">

<div data-drupal-selector="edit-actions" class="form-actions js-form-wrapper form-wrapper" id="edit-actions">

<input data-drupal-selector="edit-submit-careers" type="submit" id="edit-submit-careers" value="Apply" class="button js-form-submit form-submit btn btn-primary">

</div>
</form>

Please check on this, what i'm doing wrong here?

@Nikit
Copy link

Nikit commented Jan 27, 2022

function mytheme_form_views_exposed_form_alter

  • no theme, module should be.

@featuriz
Copy link

featuriz commented Jan 27, 2022

I don't have any custom module, do I have to create one?

@mbomb007
Copy link

@featuriz Yes.

@profwebsait
Copy link

profwebsait commented Mar 2, 2023

Hi, this code is working for Drupal 9.5.3 in theme:

function THEMENAME_form_views_exposed_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { 
    if ($form['#id'] == 'views-exposed-form-all-products-page') {
	 $form['keys']['#attributes']['placeholder'] = 'Product search';
    }
}

My form:

<form data-block="nav_additional" action="/products" method="get" id="views-exposed-form-all-products-page" accept-charset="UTF-8" data-once="form-updated" data-drupal-form-fields="edit-keys">
  <div class="form--inline clearfix">
	<div class="js-form-item form-item js-form-type-textfield form-type-textfield js-form-item-keys form-item-keys">
		<label for="edit-keys">Search</label> 
		<input placeholder="Product search" data-drupal-selector="edit-keys" type="text" id="edit-keys" name="keys" value="" size="30" maxlength="128" class="form-text form-control">
    </div>
	<div data-drupal-selector="edit-actions" class="form-actions js-form-wrapper form-wrapper" id="edit-actions">
		<input data-drupal-selector="edit-submit-all-products" type="submit" id="edit-submit-all-products" value="Search" class="button js-form-submit form-submit btn btn-primary">
	</div>
  </div>
</form>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment