How to change the label of the default value (-Any-) of an exposed filter in Drupal Views?

ε祈祈猫儿з 提交于 2019-12-18 12:55:24

问题


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:

and

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.


回答1:


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.




回答2:


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!




回答3:


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).




回答4:


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");

});



回答5:


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




回答6:


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).




回答7:


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



来源:https://stackoverflow.com/questions/4668266/how-to-change-the-label-of-the-default-value-any-of-an-exposed-filter-in-dru

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!