Supplying a predefined list of options for an exposed item in a view in Drupal?

℡╲_俬逩灬. 提交于 2019-12-13 06:52:51

问题


I've got a view that filters by year. The year is a normal text CCK field on the content type. I've exposed this field in the view, so that the user can type in a value for it. E.g. 2010. It will then show all the content types with the field set to 2010. My problem is, I don't want the user to type in the value. I want to change that text field to a dropdown with several years.

My options are:

  1. Hack it with JQuery --> VERY BAD
  2. Edit the exposed value using some hook or something BEFORE it's outputted on the page
  3. Any other options?

My question is, how do I do option 2, or worst case, option 3?


回答1:


When you edit the CCK field in Manage fields, you can set the allowed values for that field. If you do, when you go back into the View, you'll have a new filter, Field - allows values which will give you a select menu of the allowed values when you expose it.

If you don't want to limit the values during creation, you're going to have to alter the exposed form in a custom module:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id === 'views_exposed_form') {
    // Change field_test_value to the name of your field
    $form['field_test_value']['#type'] = 'select';
    $form['field_test_value']['#options'] = array(
      '' => '', 
      '2010' => '2010', 
      '2009' => '2009');
  }
}

See the Form API reference to see what else you can do.



来源:https://stackoverflow.com/questions/3397655/supplying-a-predefined-list-of-options-for-an-exposed-item-in-a-view-in-drupal

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