【问题标题】:Drupal 9 - Why is my body not saved with Node::create()Drupal 9 - 为什么我的身体没有用 Node::create() 保存
【发布时间】:2020-11-04 12:10:06
【问题描述】:

感谢您花时间阅读我的问题。

我正在尝试编写一个模块,将内容从旧版本的 Drupal 导入到新的 Drupal 9 网站。

我设法从旧数据库中提取了所有内容。它存储在一个数组中,我将它传递给一个负责在新数据库中创建节点的函数。我遇到的问题是节点的主体没有保存。在表节点、node_field_data、node_revision、node_field_revision 中创建记录,但在 node__body 中没有创建任何内容。

我尝试了两种不同的方法:

1-

$node = Node::create(['type' => 'article_epingle']);
$node->langcode = $a["lang"];
$node->title = $a["title"];
// ... status, promote, ...
$node->body = array("value" => $a["body"], "format" => 'full_html');
// ... a few custom fields
$node->enforceIsNew();
$node->save();

或者

$node = Node::create(['type' => 'article_epingle']);
$node->langcode = $a["lang"];
$node->title = $a["title"];
// ... status, promote, ...
$node->body->value = $a["body"];
$node->body->format = 'full_html';
// ... a few custom fields
$node->enforceIsNew();
$node->save();

还有 2 -

$node = \Drupal::entityTypeManager()
                 ->getStorage('node')
                 ->create(['type' => 'article_epingle',
                           'title' => $a["title"],
                           'body' => $a["body"],
                           // ... other fields
                           ]);
$node->save();

每次结果都是一样的,body 没有保存在 DB 中。 save() 方法返回“1”,新创建的节点出现在 /admin/content 中但无法显示。如果我尝试显示节点,则会返回以下错误:

错误:在 template_preprocess_node() 中调用成员函数 displaySubmitted() on null(core/modules/node/node.module 的第 528 行)。

有人遇到过同样的问题吗?

提前感谢您的意见!

【问题讨论】:

    标签: drupal drupal-modules drupal-9


    【解决方案1】:

    我解决了我的问题。

    我想我在安装 Drupal 时做错了,因为我的代码在再次安装所有内容后运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 2011-01-09
      • 1970-01-01
      相关资源
      最近更新 更多