Drupal 7:Drupal中动态选择列表的最佳实践7 [英] Drupal 7: Best Practice for a Dynamic Select List in Drupal 7

查看:281
本文介绍了Drupal 7:Drupal中动态选择列表的最佳实践7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



a)通过UI创建带有虚拟选项的字段,并用hook_form_FORM_ID_alter





b)通过具有hook_form

的自定义模块从头创建动态选择列表



c)不同的方法?



谢谢<你可以使用Form API的主题 - forms_api_reference.html#ajaxrel =noreferrer>#ajax (以前在Drupal6中调用 #ahah )来做到这一点。 / p>

以下是Drupal 7的示例(基于开发人员示例),显示两个下拉列表,其中第一个下拉列表修改第二个下拉列表的选项列表:



文件来源 mymodule.module

 <?php 

/ **
*执行hook_menu()。
*注册一个基于表单的页面,您可以访问http:// localhost / mypage
* /
函数mymodule_menu(){
return array(
'mypage'=>数组(
'title'=>'测试ajax'的页面,
'页面回调'=>'drupal_get_form',
'页面参数' => array('mymodule_page'),
'access arguments'=> array('access content'),

);
}



/ **
*具有下拉菜单的表单,其选项取决于之前的
*选项落下。
*
*在更改第一个下拉列表时,第二个选项将更新。
* /
函数mymodule_page($ form,& $ form_state){
//获取填充第一个下拉列表的选项列表。
$ options_first = mymodule_first_dropdown_options();

//如果我们从$ form_state ['values']获得第一个下拉列表的值,我们使用
//这两个作为第一个下拉列表的默认值,也作为
//参数传递给检索
//第二个下拉列表的选项的函数。
$ value_dropdown_first = isset($ form_state ['values'] ['dropdown_first'])? $ form_state ['values'] ['dropdown_first']:key($ options_first);

$ form ['dropdown_first'] = array(
'#type'=>'select',
'#title'=>'第一个下拉菜单',
'#options'=> $ options_first,
'#default_value'=> $ value_dropdown_first,

//将ajax回调绑定到更改事件(这是默认值对于
//选择表单类型),它将替换第二个
//下拉列表重建
'#ajax'=>数组(
//当事件发生时,Drupal将在
//背景中执行ajax请求,通常默认值是足够的(例如,为
// select元素更改),但有效的值包括任何jQuery事件
//最显着的'mousedown','blur'和'submit'
'event'=>'change',
'callback'=>'mymodule_ajax_callback'
'包装=>dropdown_second_replace,
)中,
)的
$ form ['dropdown_second'] = array(
'#type'=>'select',
'#title'=>'Second Dropdown',
/ /这里创建的整个包围的div被替换,当dropdown_first
//被改变时
'#prefix'=>'< div id =dropdown_second_replace>',
'后缀'=>'< / div>',
//当在ajax处理期间重建表单时,$ value_dropdown_first变量
//现在将具有新值,因此选项将更改
'#options'=> mymodule_second_dropdown_options($ value_dropdown_first),
'#default_value'=> isset($ form_state ['values'] ['dropdown_second'])$ form_state ['values' ] ['dropdown_second']:'',
);
return $ form;
}

/ **
*仅选择要重新呈现的第二个下拉列表
*
*由于用于填充表单是以
*形式生成的函数,我们这里所做的就是选择元素并返回它进行更新。
*
* @return可渲染数组(第二个下拉菜单)
* /
函数mymodule_ajax_callback($ form,$ form_state){
return $ form ['dropdown_second' ]。
}


/ **
*帮助函数填充第一个下拉列表。这通常是
*从数据库中提取数据。
*
* @return选项数组
* /
函数mymodule_first_dropdown_options(){
返回数组(
'colors'=>'颜色',
'cities'=>'城市名称',
'animals'=>'动物名称',
);
}


/ **
*帮助函数填充第二个下拉列表。这通常是
*从数据库中提取数据。
*
* @param键。这将确定返回哪个选项集。
*
* @return选项数组
* /
函数mymodule_second_dropdown_options($ key =''){
$ options = array(
'colors '=> array(
'red'=>'Red',
'green'=>'Green',
'blue'=>'Blue'
),
'cities'=>数组(
'paris'=>'巴黎,法国',
'tokyo'=>'东京,日本',
'newyork'=>'纽约,美国'
),
'animals'=>数组(
'dog'=>'Dog',
' cat'=>'猫',
'bird'=>'Bird'
),
);
if(isset($ options [$ key])){
return $ options [$ key];
}
else {
return array();
}
}


What is the best practice for a dynamic select list in Drupal 7?

a) Create a Field with dummy options via the UI and overriding the options with hook_form_FORM_ID_alter

or

b) Creating a dynamic select list from scratch via a custom module with the hook_form

or

c) different approach?

Thanks

解决方案

You could use Form API's #ajax (it used to be called #ahah in Drupal6) to do that.

Here is an example for Drupal 7 (based on Examples for Developers) that shows two dropdowns where the first dropdown modifies the list of options of the second dropdown:

Source of file mymodule.module:

<?php

/**
 * Implementation of hook_menu().
 * Registers a form-based page that you can access at "http://localhost/mypage"
 */
function mymodule_menu(){
    return array(
        'mypage' => array(
            'title' => 'A page to test ajax',
            'page callback' => 'drupal_get_form',
            'page arguments' => array('mymodule_page'),
            'access arguments' => array('access content'), 
        )
    );
}



/**
 * A form with a dropdown whose options are dependent on a
 * choice made in a previous dropdown.
 *
 * On changing the first dropdown, the options in the second are updated.
 */
function mymodule_page($form, &$form_state) {
    // Get the list of options to populate the first dropdown.
    $options_first = mymodule_first_dropdown_options();

    // If we have a value for the first dropdown from $form_state['values'] we use
    // this both as the default value for the first dropdown and also as a
    // parameter to pass to the function that retrieves the options for the
    // second dropdown.
    $value_dropdown_first = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);

    $form['dropdown_first'] = array(
        '#type' => 'select',
        '#title' => 'First Dropdown',
        '#options' => $options_first,
        '#default_value' => $value_dropdown_first,

        // Bind an ajax callback to the change event (which is the default for the
        // select form type) of the first dropdown. It will replace the second
        // dropdown when rebuilt
        '#ajax' => array(
            // When 'event' occurs, Drupal will perform an ajax request in the
            // background. Usually the default value is sufficient (eg. change for
            // select elements), but valid values include any jQuery event,
            // most notably 'mousedown', 'blur', and 'submit'.
            'event' => 'change',
            'callback' => 'mymodule_ajax_callback',
            'wrapper' => 'dropdown_second_replace',
        ),
    );
    $form['dropdown_second'] = array(
        '#type' => 'select',
        '#title' => 'Second Dropdown',
        // The entire enclosing div created here gets replaced when dropdown_first
        // is changed.
        '#prefix' => '<div id="dropdown_second_replace">',
        '#suffix' => '</div>',
        // when the form is rebuilt during ajax processing, the $value_dropdown_first variable
        // will now have the new value and so the options will change
        '#options' => mymodule_second_dropdown_options($value_dropdown_first),
        '#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
    );
    return $form;
}

/**
 * Selects just the second dropdown to be returned for re-rendering
 *
 * Since the controlling logic for populating the form is in the form builder
 * function, all we do here is select the element and return it to be updated.
 *
 * @return renderable array (the second dropdown)
 */
function mymodule_ajax_callback($form, $form_state) {
    return $form['dropdown_second'];
}


/**
 * Helper function to populate the first dropdown. This would normally be
 * pulling data from the database.
 *
 * @return array of options
 */
function mymodule_first_dropdown_options() {
    return array(
        'colors' => 'Names of colors',
        'cities' => 'Names of cities',
        'animals' => 'Names of animals',
    );
}


/**
 * Helper function to populate the second dropdown. This would normally be
 * pulling data from the database.
 *
 * @param key. This will determine which set of options is returned.
 *
 * @return array of options
 */
function mymodule_second_dropdown_options($key = '') {
    $options = array(
        'colors' => array(
            'red' => 'Red',
            'green' => 'Green',
            'blue' => 'Blue'
        ),
        'cities' => array(
            'paris' => 'Paris, France',
            'tokyo' => 'Tokyo, Japan',
            'newyork' => 'New York, US'
        ),
        'animals' => array(
            'dog' => 'Dog',
            'cat' => 'Cat',
            'bird' => 'Bird'
        ),  
    );
    if (isset($options[$key])) {
        return $options[$key];
    }
    else {
        return array();
    }
}

这篇关于Drupal 7:Drupal中动态选择列表的最佳实践7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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