Last active
October 12, 2023 12:20
-
-
Save jez500/4d1ef061e41f69a964732461cb0ec43a to your computer and use it in GitHub Desktop.
Drupal 8 - Hook form alter views exposed form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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. | |
} | |
} |
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?
function mytheme_form_views_exposed_form_alter
- no theme, module should be.
I don't have any custom module, do I have to create one?
@featuriz Yes.
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
$form_state->getStorage() does not accept arguments.
Use $form_state->get('view') to get a ViewExecutable object