在drupal 7 CCK字段中以编程方式添加允许的值列表;列表“文本”;
我想知道是否可以通过编程方式创建一个CCK字段实例,并在单个阶段中插入“允许的_值”。所以我试了一下:在drupal 7 CCK字段中以编程方式添加允许的值列表;列表“文本”;,drupal,drupal-7,drupal-modules,cck,Drupal,Drupal 7,Drupal Modules,Cck,我想知道是否可以通过编程方式创建一个CCK字段实例,并在单个阶段中插入“允许的_值”。所以我试了一下: field_create_instance(array( 'field_name' => 'card number', 'entity_type' => 'payment_method', 'bundle' => 'debit_card', 'label' => t('Debit/Credit card'), 'description' =>
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,
));
我试过一些例子,比如在“设置”=>array(“允许的_值”=>array(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文件中。From:首次启用模块时将调用此挂钩。因此,您的列表将被冻结,直到下一个禁用/启用模块。当模块启用时,将调用钩子。只要呈现字段,就会调用该函数,因此您可以更改列表。