假設要創造一個新的"article" entity(安裝standard profile時所設定的entity type)
$array = array (
'nid' => '45', // can designate node id
'type' => 'article',
'title' => 'article title',
'uid' => '1', // author
'promote' => '1', // promoted to front page
'sticky' => '0', // Sticky at top of lists
'status' => '0', // 0: unpublished, 1: publish
'created' => '1483228800', // timestamp
'changed' => '1483228800', // timestamp
'body' => array(
array(
'value' => 'text',
'summary' => 'summary of body',
'format' => 'full_html',
),
),
'field_tags' => array('1', '2', '3'), // taxonomy term id
'revision_timestamp' => '1483228800', // timestamp
'revision_uid' => '1', // who create this revision
'revision_log' => 'revision log', // revision log message
);
$object = $this->entityTypeManager()->getStorage('node')->create($array);
$this->entityTypeManager()->getStorage('node')->save($object);
修改"article" entity(並創造一個新revision)
$object = $this->entityTypeManager()->getStorage('node')->load(intval('45'));
$object->setNewRevision();
$object->setRevisionCreationTime('1483228800');
$object->setRevisionAuthorId(1);
$object->setRevisionLogMessage('revision log');
$object->setTitle('article title');
$object->setCreatedTime('1483228800');
$object->setChangedTime('1483228800');
$object->setPublished(false);
//$object->setPublished(true);
$object->set('body', array(
'value' => 'text',
'summary' => 'summary of body',
'format' => 'full_html',
));
$object->set('field_tags', array('1', '2', '3'));
$this->entityTypeManager()->getStorage('node')->save($object);
創造一個新的"comment" entity
$array = array (
'comment_type' => 'comment',
'pid' => array(),
'entity_id' => array('45'),
'subject' => 'subject',
'uid' => '0', // comment from unregistered user
'name' => 'name',
'mail' => 'example@example.com',
'homepage' => 'http://www.example.com',
'hostname' => '192.168.0.1',
'created' => array('1483228800'),
'changed' => '1483228800',
'status' => '1',
'entity_type' => 'node',
'field_name' => 'comment',
'comment_body' => array(
'value' => 'text',
'format' => 'basic_html',
),
);
$object = $this->entityTypeManager()->getStorage('comment')->create($array);
$this->entityTypeManager()->getStorage('comment')->save($object);
創造一個新的"file" entity
$array = array(
'uid' => '1',
'status' => '1',
'filename' => 'image01.jpg',
'uri' => 'public://inline-images/image01.jpg',
'filemime' => 'image/jpeg',
'filesize' => '20000',
'created' => '1483228800',
'changed' => '1483228800',
);
$object = $this->entityTypeManager()->getStorage('file')->create($array);
$this->entityTypeManager()->getStorage('file')->save($object);
Add new comment