您的位置:首页 > 其它

在drupal7中动态的为某个内容类型添加字段

2013-08-16 13:07 387 查看
$myField_name = "my_new_field_name";
if(!field_info_field($myField_name)) // check if the field already exists.
{
$field = array(
'field_name'    => $myField_name,
'type'          => 'image',
);
field_create_field($field);

$field_instance = array(
'field_name'    => $myField_name,
'entity_type'   => 'node',
'bundle'        => 'CONTENT_TYPE_NAME',
'label'         => t('Select an image'),
'description'   => t(''),
'widget'        => array(
'type'      => 'image_image',
'weight'    => 10,
),
'formatter'     => array(
'label'     => t('label'),
'format'    => 'image'
),
'settings'      => array(
'file_directory'        => 'photos', // save inside "public://photos"
'max_filesize'          => '4M',
'preview_image_style'   => 'thumbnail',
'title_field'           => TRUE,
'alt_field'             => FALSE,
)
);
field_create_instance($field_instance);
drupal_set_message("Field created successfully!");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: