在 drupal 7 CCK 字段"list_text"中以编程方式添加允许的值列表



我想知道我是否可以以编程方式创建一个 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,
 ));

我已经尝试了一些情况,即在"设置"=>数组("allowed_values"=>数组(1,2,3)中设置,但没有任何反应。有什么建议吗?

解决方案:

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 文件中。

最新更新