在Drupal中动态创建内容类型

在Drupal中动态创建内容类型,drupal,drupal-7,drupal-modules,Drupal,Drupal 7,Drupal Modules,我们的网站要求之一是要有一个内容类型,让你决定的内容类型总量的飞行 例如,如果我指定一个数字10,那么它应该连续生成内容类型,其中一个类型为“textarea”,另一个类型为“radio”创建10次 基本上,为了以编程方式断开它,它将创建: <?php for(i=0;i<10;i++) { echo "<input type = 'textarea'></input>"; echo "<select><option>1</opt

我们的网站要求之一是要有一个内容类型,让你决定的内容类型总量的飞行

例如,如果我指定一个数字10,那么它应该连续生成内容类型,其中一个类型为“textarea”,另一个类型为“radio”创建10次

基本上,为了以编程方式断开它,它将创建:

<?php
for(i=0;i<10;i++)
{
echo "<input type = 'textarea'></input>";
echo "<select><option>1</option><option>2</option></select>";
} 
?>

要在drupal 7中创建内容动态类型,您需要遵循以下过程:

更新*

1) 使用使用drupal\u get\u form()的钩子菜单()创建菜单路径。这将允许您为用户输入的动态内容创建收集所有数据

例如:

$items['newpost'] = array(
'title' => 'Create Post',
'description' => 'The main noticeboard',
'page callback' => 'drupal_get_form',
'page arguments' => array('customvishal_create_content'),
'access callback' => TRUE,
);
return $items;
2) 然后使用:

function customvishal_create_content($form, &$form_submit) // To create your form on that page
function customvishal_create_content_validate($form, &$form_state) // for any kind of validation


function customvishal_create_content_submit($form, &$form_state)
  • 在此函数中,您可以将值提交到新的内容类型中
  • 在这里您将调用以下函数
3) 创建一个数组,用于保存有关内容类型的元数据

// Define the node type.
$mystuff = array(
'type' => 'mystuff',
'name' => $t('my new Stuff'),
'base' => 'node_content',
'description' => $t('This is an example node type.'),
'body_label' => $t('Content')
 );

 // Set defaults.
$content_type = node_type_set_defaults($mystuff);
node_type_save($content_type);
foreach (_mystuff_installed_fields() as $field) {
field_create_field($field);
}

// Create instances of fields.
foreach (_mystuff_installed_instances() as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = $mystuff['type'];
field_create_instance($instance);
}
4) 使用node_type_save()保存/声明内容类型

// Define the node type.
$mystuff = array(
'type' => 'mystuff',
'name' => $t('my new Stuff'),
'base' => 'node_content',
'description' => $t('This is an example node type.'),
'body_label' => $t('Content')
 );

 // Set defaults.
$content_type = node_type_set_defaults($mystuff);
node_type_save($content_type);
foreach (_mystuff_installed_fields() as $field) {
field_create_field($field);
}

// Create instances of fields.
foreach (_mystuff_installed_instances() as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = $mystuff['type'];
field_create_instance($instance);
}
5) 创建字段,然后附加到内容类型

// Define the node type.
$mystuff = array(
'type' => 'mystuff',
'name' => $t('my new Stuff'),
'base' => 'node_content',
'description' => $t('This is an example node type.'),
'body_label' => $t('Content')
 );

 // Set defaults.
$content_type = node_type_set_defaults($mystuff);
node_type_save($content_type);
foreach (_mystuff_installed_fields() as $field) {
field_create_field($field);
}

// Create instances of fields.
foreach (_mystuff_installed_instances() as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = $mystuff['type'];
field_create_instance($instance);
}

Vishal,谢谢你的帮助,但这并不能完全解决我面临的挑战。其思想是创建动态内容类型,创建内容类型不是这里的挑战。关键是使用durpal_get_forms(),我在文章中进一步解释了这一点。