在Drupal的视图中为暴露项目提供预定义的选项列表? [英] Supplying a predefined list of options for an exposed item in a view in Drupal?

查看:168
本文介绍了在Drupal的视图中为暴露项目提供预定义的选项列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看法,按年过滤。年份是内容类型的正常文本CCK字段。我已经在视图中暴露了此字段,以便用户可以为其输入一个值。例如。然后它将显示所有的内容类型,字段设置为2010.我的问题是,我不希望用户键入值。我想将文本字段更改为几年的下拉列表。



我的选项是:


  1. 使用JQuery进行破解 - > VERY BAD

  2. 在页面输出之前,使用某些钩子或其他东西编辑公开值

  3. 任何其他选项?

我的问题是,我如何做选项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:

  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?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆