在 Drupal 中动态创建内容类型

发布于 2024-12-28 07:30:46 字数 540 浏览 2 评论 0原文

我们网站的要求之一是拥有一种内容类型,让您可以动态决定内容类型的总量。

例如,如果我指定数字 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>";
} 
?>

如果我涉足简单的 PHP 文件,这非常简单,但对于 Drupal 7 的内容类型 (CCK),它提出的挑战比应有的挑战更大。我尝试过探索允许您动态创建内容类型的模块,并考虑以编程方式创建自定义内容类型,这似乎完全是另一个挑战。

我很好奇是否有人有替代方案并且之前曾尝试过这一挑战。非常感谢您的回答。

谢谢大家

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.

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. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2025-01-04 07:30:46

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

更新 *

1) 使用 hook_menu() 创建菜单路径,该菜单路径使用 drupal_get_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);

4) 使用node_type_save() 保存/声明您的内容类型。

node_type_save($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);
}

To create content dynamic types in drupal 7 you will need to follow the below process:

Updated *

1) Create a menu path using hook_menu() which uses drupal_get_form(). 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:

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.

// 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.

node_type_save($content_type);

5) Create fields and then attach to your 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文