如何在drupal中以编程方式添加实体内容

时间:2018-02-04 23:27:07

标签: entity backend drupal-8

晚安:我以编程方式创建节点,代码类似于:

use Drupal\node\Entity\Node;

$nodeObj = Node::create([
  'type' => 'article',
  'title' => 'Programatically created Article',
  'body' => "CodeExpertz is contains best Blogs.",
  'field_date' => '2017-10-24',
  'field_category' => 'Study',
  'uid' => 1,
]);

$nodeObj->save(); // Saving the Node object.
$nid = $nodeObj->id(); // Get Nid from the node  object.
Print "Node Id is " . $nid;

现在我想创建实体内容(没有节点),但我找不到有关此内容的内容。我试图改编下一个片段:

$term = \Drupal\taxonomy\Entity\Term::create([
          'vid' => 'test_vocabulary',
          'name' => 'My tag',
    ]);
$term->save();

到此(车辆是我的实体):

$newvehicle = \Drupal\vehicle\Entity\Vehicle::create([
          'title' => 'Ferrari'
    ]);
$newvehicle->save();

结果是死亡的白页。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我可以用这段代码来完成

use Drupal\customModule\Entity\entityCustom;

$entityCustom = entityCustom::create([
    'type' => 'custom_entity',
    'uid' => 1,
    'status' => TRUE,
    'promote' => 0,
    'created' => time(),
    'langcode' => 'en',
    'name' => 'NAME',
  ]);
  $entityCustom->save();