14

Drupal 8 で新しいモジュールを設計しています。これは長期的なプロジェクトであり、少なくとも数か月間は公開されないため、新しいものを把握する方法として使用しています。

このモジュールでは、プログラムでノードを作成できるようにしたいと考えています。Drupal 7 では、オブジェクトを作成してから「node_submit」と「node_save」を呼び出すことでこれを行います。

これらの関数は Drupal 8 には存在しなくなりました。代わりに、ドキュメントによると、「モジュールとスクリプトは、通常のフォーム API パターンを使用してプログラムでノードを送信できます。」私は途方に暮れています。これは何を意味するのでしょうか?Drupal 7 で Form API を使用してフォームを作成しましたが、ここでドキュメントが何を言っているのかわかりません。

私が探しているのは、ユーザーが提示したフォームから直接取得されたものではない情報に基づいて、少なくとも 1 つ、場合によっては複数の新しいノードをプログラムで作成することです。次のことができる必要があります。

1) コンテンツ タイプを指定する

2) URL パスを指定する

3) 以前は廃止された node_object_prepare() によって処理されていたその他の必要な変数を設定します。

4) 新しいノード オブジェクトをコミットします。

特定のブロックやフォームに結び付けられていない、独立した高度に抽象化された関数でこれを実行できるようにしたいと考えています。

それで、私は何が欠けていますか?

4

5 に答える 5

22

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

于 2015-08-05T04:56:59.723 に答える
6

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();
于 2016-03-08T17:31:03.987 に答える
6

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」を持つ配列になることに注意してください。

于 2014-11-18T22:36:27.297 に答える
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();
于 2014-06-12T16:18:32.753 に答える