在Drupal的视图中为暴露项目提供预定义的选项列表? [英] Supplying a predefined list of options for an exposed item in a view in Drupal?
问题描述
我的选项是:
- 使用JQuery进行破解 - > VERY BAD
- 在页面输出之前,使用某些钩子或其他东西编辑公开值
- 任何其他选项?
我的问题是,我如何做选项2或最坏情况选项3?
当您编辑管理字段中的CCK字段时,可以设置该字段的允许值。如果您这样做,当您返回到View时,您将有一个新的过滤器, Field - 允许值,当您公开它时,它将为您提供允许值的选择菜单。
如果您不想在创建过程中限制值,则必须在自定义模块中更改暴露的表单:
function mymodule_form_alter(& $ form,$ form_state,$ form_id){
if($ form_id ==='views_exposed_form'){
//将field_test_value更改为字段名称
$ form ['field_test_value'] ['#type'] ='select';
$ form ['field_test_value'] ['#options'] = array(
''=>'',
'2010'=>'2010',
'2009'=>'2009');
}
}
请参阅表单API参考,看看你还能做些什么。
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?
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.
这篇关于在Drupal的视图中为暴露项目提供预定义的选项列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!