I have a view with exposed filter to a field as follows :
I want to change the text 'Deployment date from' and 'And' to 'From' and 'To'
Can i do this with hook_views_query_alter
or hook_views_pre_render
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityYou can easily change the first label with the views editor, by setting it in Filter criteria part. But for the "And" i think you have it because you selected the "is between" operator. So the easiest way to change it is to use hook_form_alter() our hook_form_FORM_ID_alter() and change the '#title' of that second form field.
If you inspect the html of your field in front, you will see that it is in a tag. So get the id of that form, for example
<form id="views-exposed-form-activities-page-1" ></form>
then in your_module.module file implements hook_form_FORM_ID_alter():
<?php
/**
* @file
* This is your_module.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter().
*/
function YOURMODULE_form_views_exposed_form_activities_page_1_alter(array &$form, FormStateInterface $form_state, $form_id) {
$form['your_annoying_field']['#title'] = t('To'); //Instead of "And".
}
YOURMODULE_form_alter(array &$form, FormStateInterface $form_state, $form_id)
and make var_dump($form_id)
, this way you can maybe target it easily.
Jan 13, 2017 at 13:19
Finally i got the answer . Actually it should be used like YOURMODULE_form_views_exposed_form_alter
only . No need of activities_page_1_
as expected . Am adding the answer below :
/**
* Implements hook_form_FORM_ID_alter().
*/
function module_name_form_views_exposed_form_alter(&$form, $form_state, $form_id) {
$form['created']['min']['#title'] = t('From');
$form['created']['max']['#title'] = t('To');
}
The easiest way to do this is with the String Overrides module. It's still under heavy development for Drupal 8 but MegaChriz (maintainer of the Feeds module a.o.) made a pretty stable version that you can download here:
https://www.drupal.org/files/issues/stringoverrides.zip
More information:
I was able to do this with the following code.
Note that my field is actually called field_date
, but to access the label I had to add "_value" to the end of my field to make it field_date_value
.
Also note that $form_id
for an exposed form is going to be generic. To check for a unique form ID, check $form['#id']
.
/**
* Implements hook_form_FORM_ID_alter().
*/
function MY_MODULE_form_views_exposed_form_alter(&$form, $form_state, $form_id) {
if($form['#id'] == 'views-exposed-form-my-unique-form-id') {
$form['field_date_value']['min']['#title'] = t('From');
$form['field_date_value']['max']['#title'] = t('To');
}
}