0

How do I change the select values of a Views groups filter? By default, they are 1,2,3... Default

<select>
<option value="all">all</option>
<option value="1">Group A</option>
<option value="2">Group B</option>
</select>

Desired Result

<select>
<option value="all">all</option>
<option value="mycustomname">Group A</option>
<option value="mycustomname2">Group B</option>
</select>
0
+50

I just did something similar for some data I'm exposing in Views for my custom module (it's kinda the same process no matter where views is getting its data).

To do this in the most efficient/easy way possible, you should create your own filter handler.

For a really simple example:

<?php
class custom_handler_filter_countries extends views_handler_filter_string {
  /**
   * Shortcut to display the exposed options form.
   */
  function value_form(&$form, &$form_state) {
    $form['value'] = array(
      '#type' => 'select',
      '#title' => t('Countries'),
      '#options' => function_that_returns_options_you_want(),
    );

    return $form;
  }
}
?>

Then, in your module's .info file, add the file containing this class (in this example, 'custom_handler_filter_countries.inc') to the .info file's files array (files[] = includes/custom_handler_filter_countries.inc).

To make your particular view use this views handler instead of the default handler, you should be able to change the handler in hook_views_pre_view(), I think. (On my particular site, since I defined the views data table myself, I just set my custom handler for fields that needed it. For your site, since the normal handler is already defined by Address Field, you need to override the filter handler.

This discussion may also have a possible solution:

Custom Options Value of Views Grouped Filter?

  • Thanks @jonathan. I was guessing there would be a UI based way to do this via Contextual fields but this may be the best option – Sam Sep 23 '17 at 8:48
  • I have not heard of any UI based way to do this, so as far as I know I would do it like this. – Jdrupal Sep 23 '17 at 8:54
  • Every place I look it is just code, code and code ;) – Jdrupal Sep 23 '17 at 8:59
  • This discussion may also have a possible solution drupal.org/node/635242 – Jdrupal Sep 23 '17 at 12:36

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.