drupal9:在sql中作为节点保存形式(自定义模块示例)

发布于 2025-01-26 19:09:29 字数 484 浏览 2 评论 0原文

我开发了一个具有表单的自定义模块,以将数据保存在SQL-datebase中。我想用于节点结构。

正常的SQL保存示例表可工作,但对于节点表不适。 知道怎么了?

这是我的保存代码,它可以在非节点表中工作:

public function submitForm(array &$form, FormStateInterface $form_state) { $connection = \Drupal::service('database');
$result = $connection->insert('node.node__body')
  ->fields(['body_value'])
  ->values([
    'body_value' => 'text for body',
    ])
  ->execute();

$form_state->setRedirect('modulname.form'); 
}

i develop a custom module with forms to save data in SQL-Datebase. I want to use for that the node-structure.

Normal SQL-savings for example table works but not for the node-tables.
Any idea what is going wrong?

This ist my Code for saving, which works in non-node-tables:

public function submitForm(array &$form, FormStateInterface $form_state) { $connection = \Drupal::service('database');
$result = $connection->insert('node.node__body')
  ->fields(['body_value'])
  ->values([
    'body_value' => 'text for body',
    ])
  ->execute();

$form_state->setRedirect('modulname.form'); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2025-02-02 19:09:30

使用Drupal中的实体API来操纵或创建节点。

在您的情况下

$node = \Drupal::entityTypeManager()->getStorage('node')->create(
      [
        'type'  => 'page',
        'title' => 'New Basic Page',
        'body'  => 'text for body'
      ]
    );

类型是内容类型的机器名称。不要忘记自己更新。另外,您可能要注入entity_type.manager服务并在代码中使用。

在此处获取更多信息:与实体合作在Drupal

Use Entity API in Drupal to manipulate or create a node.

In your case,

$node = \Drupal::entityTypeManager()->getStorage('node')->create(
      [
        'type'  => 'page',
        'title' => 'New Basic Page',
        'body'  => 'text for body'
      ]
    );

Here, type is the content type machine name. Don't forget to update with your own. Also you probably want to inject the entity_type.manager service and use in the code.

Get more info here: Working with entities in Drupal

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文