问题
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:
- Hack it with JQuery --> VERY BAD
- Edit the exposed value using some hook or something BEFORE it's outputted on the page
- 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