如何在drupal8中的cron作业中创建节点?
我需要在Drupal8中运行cronjob之后创建节点 我已经破解了这个密码。但不起作用如何在drupal8中的cron作业中创建节点?,cron,drupal-8,Cron,Drupal 8,我需要在Drupal8中运行cronjob之后创建节点 我已经破解了这个密码。但不起作用 function modulename_cron() { $node = entity_create('node', array( 'title' => 'New Article', 'body' => 'Article body', 'type' => 'article', )); $node->save(); }
function modulename_cron() {
$node = entity_create('node', array(
'title' => 'New Article',
'body' => 'Article body',
'type' => 'article',
));
$node->save();
}
您的代码也在工作…
但是,如果您在创建节点时遇到问题,那么也可以尝试此代码 您还可以在Drupal8中使用此代码创建节点
$node = \Drupal\node\Entity\Node::create([
'type' => 'article',
'title' => 'New Article',
'body' => 'Article body',
]);
$node->save();
更好的解决方案是使用Drupal的entity.manager服务:
// Get node storage.
$nodeStorage = \Drupal::service('entity.manager')->getStorage('node');
// Set node content.
$content = [
'type' => 'article',
'title' => 'title',
'body' => [
'value' => 'Lorem ipsum dolor sit amet...',
'format' => 'basic_html'
]
];
// Create a new node.
$node = $nodeStorage->create($content);
// Save the node.
$node->save();
检查您的数据库日志,可能它显示了有关此问题的一些信息。