15

I created a view which has three exposed filters. Everything works fine except the fact that I can neither translate or change the default string (-Any-) for the dropdowns. Is there a way to change this string to something more meaningful like "Please Select" and make it translatable so the German version displays "Bitte wählen"? I have two screen captures that may be helpful:

the exposed filters

and

dropdown box

A further improvement would be the ability to change the text "any" to something like "please select a (field name here)" but I am losing hope for that =)

UPDATE

IMPORTANT: On further testing, I found that if you choose to display "-Any-" from "admin/build/views/tools", then THAT IS translatable.

Community
  • 1
  • 1
Ege Özcan
  • 12,341
  • 2
  • 28
  • 50

7 Answers7

23

For anyone who wants to just change the value of "- Any -" to something in particular then use a custom module to override that looks like this:

function yourmodulename_form_alter(&$form, $form_state, $form_id) {

  if($form_state['view']->name == 'your_view_name_here') {

    $form['your_dropdown_name']['#options']['All'] = t('- Type -'); // overrides <All> on the dropdown

  }
}

The reason you might want to do this is if you have 3 (for example) dropdowns for 3 separate fields. Then having on them wouldn't be very useful for a user (especially if you are not using labels).

In the code above just remember to change "yourmodulename" to the name of your module.

your_view_name_here should be the name of your view (replace dashes with underscores - for example "property-search-bar" would become "property_search_bar")

And change "your_dropdown_name" to the field name - I found this by using dsm($form) with the devel module installed and enabled. This is usually the field name of your drop down so it might be something like "field_my_custom_value".

Hope this helps anyone who needs it!

aendrew
  • 4,918
  • 3
  • 35
  • 54
Garry
  • 454
  • 5
  • 14
  • Thank you :) However, could you help when I want to do this same thing with the HS widget on the exposed filter, as opposed to the dropdown box? Wherever in the array I put the 3rd line of code, it fails to override the Any option - could you please give me any leads on that? – Aditya M P Jan 21 '12 at 18:51
  • This is the best option, especially if you need only one "- any -" overridden. Really, what that's called should be an option. – aendrew Jan 18 '13 at 14:04
  • Is this accurate for D7? – Jeff Jan 25 '14 at 00:18
  • We can use a quick test as well to see if the view exists, otherwise we get : Notice: Undefined index: view on other forms ( such as search module...) : if (isset($form_state['view']) && $form_state['view']->name == 'my_view_name' ) {/*...*/} – OlivierLarue Mar 03 '15 at 22:35
9

Three options:

  • You could change it with localisation, if you have that enabled already. Introducing localisation only for this string is far too much overhead.
  • You can change it with a form_alter, if you already alter the form anyway. Introducing a module with a hook_form alter for just one string is way too much (maintainance and performance) overhead.
  • You coud change it with a simple string override in your settings.php

In Drupal 7 (Drupal6 differs in details only)

/**
 * String overrides:
 *
 * To override specific strings on your site with or without enabling locale
 * module, add an entry to this list. This functionality allows you to change
 * a small number of your site's default English language interface strings.
 *
 * Remove the leading hash signs to enable.
 */
$conf['locale_custom_strings_en'][''] = array(
   '<Any>'      => 'Whatever!',
);

Note though, that this will change every occurrance of the full string <Any> (case sensitive) to Whatever, not just the ones in that single form.

berkes
  • 25,081
  • 21
  • 107
  • 188
  • I already have the localization enabled (it's a multi-language site) but when I search for "" (even "Any"), I can't find that string as listed in "/admin/build/translate/search". By the way I am using Drupal 6 and I will update the question for clearing that. Thanks for the great explanation by the way. – Ege Özcan Jan 12 '11 at 12:21
  • Drupal offers translations for *every*, *nonconfigurable* string. So either you can change the string with a configuration option (often the case in views) or you can change it trough localisation. If you cannot, then it is a bug. – berkes Jan 12 '11 at 12:24
  • I tried to find it on settings but the options are not, to say the least, too much: http://bit.ly/dWCXQo =) I will now accept this answer as a solution but maybe I'll post a bug to the views project. Thanks for the help. – Ege Özcan Jan 12 '11 at 12:35
  • 1
    IMPORTANT: On further testing, I found that if you choose to display "-Any-" from "admin/build/views/tools", then THAT IS translatable. Wow, who could have known that, if I didn't find: http://drupal.org/node/663156#comment-2388514 – Ege Özcan Jan 12 '11 at 13:00
  • I can't believe the string override worked so easily! Thank you. – octern Apr 09 '12 at 15:10
  • This seems to be changing everything at once.. How about when on one view, i'd like to change "- Any -" to "All Authors", and then the next one "All Articles", and then "All Blog Posts", and so on... I don't think this approach is possible. – bonbon.langes Jun 29 '13 at 05:54
  • @bonbon.langes: the answer lists three options. Only translating will change it everywhere at once, if that is what you want, it is your best option, else you'd need a form_alter. – berkes Jul 01 '13 at 07:21
2

Views exposed filter label is not translatable in D6. Go to Administer > Site building > Views and select tab tools. Replace 'Label for "Any" value on optional single-select exposed filters: ' by the translatable '- Any -'. Important: visit the views with exposed filters in at least one language which isn't your default language. Then you can translate "- Any -" through Aminister > Site building > Translate interface (case sensitive).

Laurent
  • 21
  • 1
  • I already mentioned that as a comment for the accepted answer: http://stackoverflow.com/questions/4668266/how-to-change-the-label-of-the-default-value-any-of-an-exposed-filter-in-drup/4668683#4668683 – Ege Özcan Sep 05 '11 at 08:11
1

The Better Exposed Filter module allows you to change the "-any-" label in a Views exposed filter.

rwilson
  • 119
  • 2
1

Or you can simply use a line of jQuery code like this:

$(document).ready(function(){

$("#views-exposed-form-url-name-display-name #edit-tid-all a").text("All");

});
Tritof
  • 129
  • 6
  • 1
    Hardcoding the strings in javaScript statements doesn't seem like a good idea. It even has an easy fix on the server side. Please see my comment on the accepted answer. – Ege Özcan Jul 12 '11 at 07:17
0

I'd rather go with the simple solution: String Overrides. With this you simply add a string you want to change on your site, and replace it with anything you want (Strings of course).

0

May be module https://www.drupal.org/project/views_advanced_labels helps? I found it, but have not tried it yet.

VVS
  • 1,699
  • 1
  • 10
  • 12