如何在Drupal 8中以编程方式创建节点?

时间:2014-06-11 21:21:20

标签: php drupal drupal-8

我在Drupal 8中设计了一个新模块。这是一个长期项目,至少几个月不会上市,所以我用它作为找出新的东西的方法。

在这个模块中,我希望能够以编程方式创建节点。在Drupal 7中,我会通过创建对象,然后调用" node_submit"来实现此目的。和" node_save"。

Drupal 8中不再存在这些函数。相反,根据文档,"模块和脚本可以使用通常的表单API模式以编程方式提交节点。"我不知所措。这是什么意思?我已经使用Form API在Drupal 7中创建表单,但我不知道文档在这里说的是什么。

我要做的是根据不是直接从用户呈现的表单中获取的信息,以编程方式创建至少一个,可能还有多个新节点。我需要能够:

1)指定内容类型

2)指定URL路径

3)设置以前过时的node_object_prepare()

之前已经处理过的任何其他必要变量

4)提交新节点对象

我希望能够在一个独立的,高度抽象的功能中执行此操作,而不依赖于特定的块或形式。

那么我错过了什么?

5 个答案:

答案 0 :(得分:15)



use Drupal\node\Entity\Node;

$node = Node::create(array(
    'type' => 'your_content_type',
    'title' => 'your title',
    'langcode' => 'en',
    'uid' => '1',
    'status' => 1,
    'field_fields' => array(),
));

$node->save();




答案 1 :(得分:5)

devel / devel_generate模块的D8版本有一个很好的例子。

来自devel_generate

  $edit_node = array(
    'nid' => NULL, 
    'type' => $node_type, 
    'uid' => $users[array_rand($users)], 
    'revision' => mt_rand(0, 1), 
    'status' => TRUE, 
    'promote' => mt_rand(0, 1), 
    'created' => REQUEST_TIME - mt_rand(0, $results['time_range']), 
    'langcode' => devel_generate_get_langcode($results),
  );
  if ($type->has_title) {
    // We should not use the random function if the value is not random
    if ($results['title_length'] < 2) {
      $edit_node['title'] = devel_create_greeking(1, TRUE);
    }
    else {
      $edit_node['title'] = devel_create_greeking(mt_rand(1, $results['title_length']), TRUE);
    }
  }
  else {
    $edit_node['title'] = '';
  }
  // @todo Remove once comment become field. http://drupal.org/node/731724
  if (Drupal::moduleHandler()->moduleExists('comment')) {
    $edit_node['comment'] = variable_get('comment_' . $node_type, COMMENT_NODE_OPEN);
  }
  $node = entity_create('node', $edit_node);

使用格式化文本

在代码行之前/之后使用grep帮助我弄清楚如何使用'full_html'添加节点。

使用以下方法搜索Drupal核心代码:

$ cd drupal/core
$ grep -B 5 -A 5 -r entity_create.*node * > /tmp/temp-grep.txt

然后,在文本编辑器中打开/tmp/temp-grep.txt。在那里捅了一下,你会看到这个:

--
modules/editor/src/Tests/EditorFileUsageTest.php-    $body_value .= '<img src="awesome-llama.jpg" data-editor-file-uuid="invalid-editor-file-uuid-value" />';
modules/editor/src/Tests/EditorFileUsageTest.php-    // Test handling of a non-existing UUID.
modules/editor/src/Tests/EditorFileUsageTest.php-    $body_value .= '<img src="awesome-llama.jpg" data-editor-file-uuid="30aac704-ba2c-40fc-b609-9ed121aa90f4" />';
modules/editor/src/Tests/EditorFileUsageTest.php-    // Test editor_entity_insert(): increment.
modules/editor/src/Tests/EditorFileUsageTest.php-    $this->createUser();
modules/editor/src/Tests/EditorFileUsageTest.php:    $node = entity_create('node', array(
modules/editor/src/Tests/EditorFileUsageTest.php-      'type' => 'page',
modules/editor/src/Tests/EditorFileUsageTest.php-      'title' => 'test',
modules/editor/src/Tests/EditorFileUsageTest.php-      'body' => array(
modules/editor/src/Tests/EditorFileUsageTest.php-        'value' => $body_value,
modules/editor/src/Tests/EditorFileUsageTest.php-        'format' => 'filtered_html',
--

请注意'body'现在变成了一个带有'value'和'format'的数组。

答案 2 :(得分:4)

RE:已弃用的实体创建

以下是没有弃用功能的简短使用示例。这对动态创建特别有用:

//define entity type and bundle
$entity_type="node";
$bundle="article";

//get definition of target entity type
$entity_def = \Drupal::entityManager()->getDefinition($entity_type);

//load up an array for creation
$new_node=array(
  //set title
  'title' => 'test node',

  //set body
  'body' => 'this is a test body, can also be set as an array with "value" and "format" as keys I believe',

  //use the entity definition to set the appropriate property for the bundle
  $entity_def->get('entity_keys')['bundle']=>$bundle
);

$new_post = \Drupal::entityManager()->getStorage($entity_type)->create($new_node);
$new_post->save();

答案 3 :(得分:2)

想出来。对于有此问题的任何其他人,节点现在被视为实体,而实体模块现在是核心的一部分。所以我的代码最终看起来像这样:

$new_page_values = array();
$new_page_values['type'] = 'my_content_type';
$new_page_values['title'] = $form_state['values']['page_title'];
$new_page_values['path'] = $new_page_path;

$new_page = entity_create('node', $new_page_values);
$new_page->save();

答案 4 :(得分:0)

使用核心服务在Drupal 8中创建节点的最佳方法

$node = \Drupal::entityTypeManager()->getStorage('node')->create([
  'type'       => 'content_type_machine_name',
  'field_text' => 'Foo',
  'title'      => 'Text Title',
]);
$node->save();
相关问题