3

I have a view with exposed filter to a field as follows :

enter image description here

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

5

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');
}
3

You 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".
}
  • Yea i changed the first label from the view itself . For the second i didnt get the point you said . How can i access a view field in hook_form_alter ? – Crazyrubixfan Jan 12 '17 at 13:37
  • Ok, t did just update my answer, hope it helps :) – Drupal de Comptoir Jan 12 '17 at 14:28
  • Its not working !! , am taking the id of the field ('edit-created-min'). Is that right or need to take the name ? – Crazyrubixfan Jan 13 '17 at 9:34
  • I will add as an answer , there was a mistake in taking the form_id from inspection – Crazyrubixfan Jan 13 '17 at 10:24
  • Yep, be carrefull with that id, i didn't test it but that was the idea. If you are not sure about the form'id then use YOURMODULE_form_alter(array &$form, FormStateInterface $form_state, $form_id) and make var_dump($form_id), this way you can maybe target it easily. – Drupal de Comptoir Jan 13 '17 at 13:19
1

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:

https://www.drupal.org/node/2236791#comment-8783307

1

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');
  }
}
  • Worked a charm to bring custom sort order of date ranges in an exposed filter. – vrwired Jul 21 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.