create node in drupal 8

参考网址:https://drupal.stackexchange.com/questions/185442/programmatically-create-nodes

主要代码:

use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;

// Create file object from remote URL.
$data = file_get_contents('http://pics.sc.chinaz.com/files/pic/pic9/201508/apic14052.jpg');
$file = file_save_data($data, 'public://druplicon.jpg', FILE_EXISTS_REPLACE);

// Create node object with attached file.
$node = Node::create([
'type' => 'article',
'title' => 'Druplicon test',
'field_image' => [
'target_id' => $file->id(),
'alt' => 'Hello world',
'title' => 'Goodbye world'
],
]);
$node->save();

你可能感兴趣的:(create node in drupal 8)