欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal 7 form type radio 和 radios 示例 AG chatgpt 有大用 有大大用

Drupal 7 Form: Radio 和 Radios 示例

在 Drupal 7 中,#type 为 radio 和 radios 的表单元素用于创建单选按钮。以下是两者的示例和区别说明:

1. 单个 Radio 按钮 (#type => 'radio')

单个 radio 按钮通常用于二进制选择(是/否):

php
function mymodule_form($form, &$form_state) {  $form['agree'] = array(    '#type' => 'radio',    '#title' => t('I agree to the terms and conditions'),    '#return_value' => 'yes', // 选中时返回的值    '#default_value' => 'no', // 默认值  );    return $form; }

2. Radio 按钮组 (#type => 'radios')

radios 类型创建一组互斥的单选按钮:

php
function mymodule_form($form, &$form_state) {  $form['favorite_color'] = array(    '#type' => 'radios',    '#title' => t('Choose your favorite color'),    '#options' => array(      'red' => t('Red'),      'green' => t('Green'),      'blue' => t('Blue'),    ),    '#default_value' => 'green', // 默认选中的选项    '#description' => t('Select your preferred color.'),  );    return $form; }

3. 高级示例 - 动态 Radio 选项

php
function mymodule_form($form, &$form_state) {  // 从数据库或其他来源获取选项  $options = array(    'us' => t('United States'),    'ca' => t('Canada'),    'uk' => t('United Kingdom'),  );    $form['country'] = array(    '#type' => 'radios',    '#title' => t('Select your country'),    '#options' => $options,    '#default_value' => variable_get('mymodule_default_country', 'us'),    '#required' => TRUE,  );    // 提交按钮  $form['submit'] = array(    '#type' => 'submit',    '#value' => t('Submit'),  );    return $form; }

关键区别

  1. 单个 radio (radio):

  • 单独使用

  • 需要额外的逻辑来确保互斥行为

  • 适合简单的二元选择

  • Radio 组 (radios):

    • 自动处理互斥行为

    • 提供更清晰的选项组织

    • 适合多个选项中选择一个

    在大多数情况下,当有多个互斥选项时,使用 radios 更为方便和安全。


    '#default_value' => variable_get('mymodule_default_country', 'us'), 是什么意思

    在 Drupal 7 中,#default_value' => variable_get('mymodule_default_country', 'us') 是用来设置表单元素的默认值(default value)的。它的具体含义如下:

    解释:

    variable_get('mymodule_default_country', 'us') 是一个 Drupal 7 函数调用,它的作用是:

    1. 尝试从 Drupal 变量系统获取 'mymodule_default_country' 的值

    • 这个变量可能是之前通过 variable_set() 存储的默认选项。

  • 如果变量不存在,则返回默认值 'us'(美国)

    • 这是一种 fallback 机制,确保表单始终有一个合理的默认选项。

    通俗理解

    php
    '#default_value' => variable_get('mymodule_default_country', 'us')

    =

    "请读取之前存储的默认国家设置(mymodule_default_country),如果没有存储过,就默认选中 'us'(美国)。"


    Drupal 变量系统(Drupal 7)

    variable_get() 和 variable_set() 是 Drupal 7 键值存储(Key-Value Store) 的 API:

    • variable_set($name, $value):存储一个变量(例如 variable_set('mymodule_default_country', 'ca') 会把默认国家设为加拿大)。

    • variable_get($name, $default):读取一个变量,如果不存在则返回默认值 $default

    • variable_del($name):删除一个变量。


    示例场景

    假设你想让管理员在后台设置默认国家,并让表单自动选中它:

    1. 在某个地方存储默认国家(比如在 hook_install() 或系统设置表单):

      php
      variable_set('mymodule_default_country', 'ca'); // 默认国家设为加拿大
    2. 在表单中使用 variable_get 加载默认值

      php
      $form['country'] = array(  '#type' => 'radios',  '#title' => t('Select your country'),  '#options' => array(    'us' => t('United States'),    'ca' => t('Canada'),    'uk' => t('United Kingdom'),  ),  '#default_value' => variable_get('mymodule_default_country', 'us'), // 默认选中 'us',除非之前设置过 );
    • 如果之前运行过 variable_set('mymodule_default_country', 'ca'),这里会选中 'ca'(加拿大)。

    • 如果没有设置过,则默认选中 'us'(美国)。


    Drupal 8+ 改变

    在 Drupal 8+,variable_get() 和 variable_set() 已被抛弃,改用 Configuration API\Drupal::config())或 State API\Drupal::state())。但 Drupal 7 仍然广泛使用 variable_* 函数。


    总结

    • variable_get('mymodule_default_country', 'us') 表示 "读取变量 mymodule_default_country,如果没有存储过,就返回 'us'"

    • 它使表单可以动态加载默认值,而不需要硬编码。

    • 适用于 全局选项、默认配置、用户偏好设置 等场景。

    希望这能帮你理解!如果有其他问题,欢迎继续提问。



    普通分类: