这个片段应该让你开始。它是在 Drupal Stackexchange 上找到的。我建议你以后先检查那里。
https://drupal.stackexchange.com/questions/8284/programmatically-create-fields-in-drupal-7
$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!");
}
您可以通过无数种方式执行此代码。我不了解您项目的要求,因此我很难提出建议。您可以将其挂接到更新/安装函数中,或者您可以将其构建到模块中的页面挂钩中,或者您可以使用以下命令引导根目录中的任何新 php 文件:
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);