【问题标题】:Add allowed values list programmatically in drupal 7 CCK field "list_text"在 drupal 7 CCK 字段“list_text”中以编程方式添加允许值列表
【发布时间】:2012-11-20 14:11:51
【问题描述】:

我想知道是否可以以编程方式创建 CCK 字段实例并在单个阶段中插入“allowed_values”。所以我尝试了这个:

 field_create_instance(array(
  'field_name' => 'card number',
  'entity_type' => 'payment_method',
  'bundle' => 'debit_card',
  'label' => t('Debit/Credit card'),
  'description' => t('Add card\'s number '),
  'widget' => array(
      'type' => 'options_select',
      'weight' => 0,
      'settings' => array('size' => 50),
   ),
  'required' => TRUE,
 ));

我已经尝试过一些情况,即在 'setting' => array( 'allowed_values' => array( 1, 2, 3 ) ) 中设置,但什么也没发生。有什么建议吗?

【问题讨论】:

    标签: drupal drupal-7 drupal-modules cck


    【解决方案1】:

    解决方案:

    function MY_MODULE_install() {
      field_create_field(array(
        'field_name' => 'months',
        'type' => 'list_text',
        'cardinality' => 1,
        'settings' => array('allowed_values_function' => 'get_months'),
      'entity_types' => array('user', 'node'),
    ));
    }
    
    function get_months() {
      $months = array( '01', '02', '03',...'12');
      return $months;
    }
    

    警告:回调函数必须始终位于自定义模块的 *.module 文件中。

    【讨论】:

    • 来自link这个钩子会在模块第一次启用时被调用。因此,你的列表将被冻结直到下一个禁用/启用模块
    • 启用模块时会调用钩子。每当呈现字段时都会调用该函数,因此您可以更改列表。
    猜你喜欢
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多