หากคุณต้องการสร้างประเภทเนื้อหาใหม่ที่มีบางฟิลด์คุณสามารถใช้รหัสด้านล่าง
รหัสนี้เหมาะกับฉันอย่างสมบูรณ์
function HOOK_install() {
/* CREATE THE CONTENT TYPE */
$t = get_t();
$node_example = array(
'type' => 'slider',
'name' => $t('Slider Content'),
'base' => 'node_content',
'description' => $t('Add slider content.'),
'body_label' => $t('Slider Description')
);
$content_type = node_type_set_defaults($node_example);
// Create a custom Field with our required field-type.
$field = array(
'field_slider_images' => array (
'field_name' => 'field_slider_images',
'type' => 'image',
),
'field_slider_links' => array (
'field_name' => 'field_slider_links',
'type' => 'text',
'entity_types' => array('node'),
),
);
foreach ($field as $fields) {
field_create_field($fields);
}
// Create a instances of that Field.
$instance = array(
'field_slider_images' => array (
'field_name' => 'field_slider_images',
'entity_type' => 'node',
'bundle' => 'slider',
'label' => t('Slider Image'),
'description' => 'Add Slider Image.',
'settings' => array(
'file_directory' => 'field/document',
'file_extensions' => 'png PNG jpg jpeg JPG JPEG',
'max_filesize' => '10MB',
'title_field' => '',
),
'widget' => array(
'type' => 'image_image',
'weight'=> 10,
),
'formatter' => array(
'label' => t('label'),
'format' => 'image'
),
'settings' => array(
'file_directory' => 'slider-image', // save inside "public://photos"
'max_filesize' => '4M',
'preview_image_style' => 'thumbnail',
'title_field' => TRUE,
'alt_field' => FALSE,
)
),
'field_slider_links' => array (
'field_name' => 'field_slider_links',
'entity_type' => 'node',
'bundle' => 'slider',
'label' => t('Slider Link'),
'widget' => array('type' => 'text_textfield'),
),
);
foreach ($instance as $fieldinstance) {
field_create_instance($fieldinstance);
}
$status = node_type_save($content_type);
node_add_body_field($content_type);
// Replacement rule for the messages.
$t_args = array('%name' => $content_type->name);
if ($status == SAVED_UPDATED) { // update case
drupal_set_message($t('The content type %name has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) { // create case
drupal_set_message($t('The content type %name has been added.', $t_args));
watchdog('node', 'Added content type %name.', $t_args, WATCHDOG_NOTICE, l($t('view'), 'admin/structure/types'));
}
}