简体   繁体   English

在Drupal中即时创建内容类型

[英]Create content types on the fly in Drupal

One of our website requirements is to have a content type that let's you decide on the total amount of content types on the fly. 我们网站的一项要求是拥有一种内容类型,让您即时确定所有内容类型。

For instance, if I specify a number 10, then it ought to generate the content types consecutively, one of type 'textarea' and another of type 'radio' are created 10 times. 例如,如果我指定一个数字10,那么它应该连续生成内容类型,其中一个类型为“ textarea”的类型,另一种类型为“ radio”的类型则创建了10次。

essentially to break it programmatically, it will create: 本质上是要以编程方式破坏它,它将创建:

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

This is pretty straightforward if I was dabbling with simple PHP files, but with Drupal 7's content types (CCK), it is posing a bigger challenge than what it ought to be IMHO. 如果我涉猎简单的PHP文件,这非常简单,但是对于Drupal 7的内容类型(CCK),它构成的挑战比恕我直言的要大。 I have tried exploring modules that let you create content types on the fly and considered creating a custom content type programmatically which seems like another challenge altogether. 我尝试过探索一些模块,这些模块可让您即时创建内容类型,并考虑过以编程方式创建自定义内容类型,这似乎是另一个挑战。

I am curious if anybody has an alternative for this and has dabbled with this challenge before. 我很好奇是否有人对此有选择,并且以前曾涉足这一挑战。 Your answers are most appreciated. 非常感谢您的回答。

Thanks guys 多谢你们

To create content dynamic types in drupal 7 you will need to follow the below process: 要在drupal 7中创建内容动态类型,您将需要执行以下过程:

Updated * 更新 *

1) Create a menu path using hook_menu() which uses drupal_get_form(). 1)使用使用drupal_get_form()的hook_menu()创建菜单路径。 This will allow you to gather all data for your users input for the dynamic content creation. 这将允许您收集所有数据,以供用户输入以创建动态内容。

Example: 例:

$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) Then use: 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)
  • In this function you can submit the values into your new content type. 在此功能中,您可以将值提交到新的内容类型中。
  • Here is where you will call the below functions. 在这里您将调用以下功能。

3) Create an array which will hold the meta data about your content type. 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);

4) use node_type_save() to save/declare your content type. 4)使用node_type_save()保存/声明您的内容类型。

node_type_save($content_type);

5) Create fields and then attach to your content type. 5)创建字段,然后附加到您的内容类型。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM