【问题标题】:Best technique for creating sample content on Drupal 9 programatically within module在模块内以编程方式在 Drupal 9 上创建示例内容的最佳技术
【发布时间】:2021-12-13 17:37:40
【问题描述】:

我创建了一个带有新内容类型和字段的 drupal 9 自定义模块。我想在安装模块时创建一个示例节点。

以编程方式创建示例节点的最佳方法是什么?

我研究了使用 migrate 和 migrate_tools 以及构建 YAML 文件。这是有希望的,但在当前版本的 drush 10.6 和 migrate 5 中存在问题。看起来 dev 中有修复,但我想知道其他人都在做什么。我的偏好是不依赖于模块,除非有令人信服的理由(例如易于维护测试用例或样本数据) 谢谢

【问题讨论】:

标签: drupal drush


【解决方案1】:

你必须使用钩子:

  1. 安装模块时,使用这个钩子hook_install
  2. 更新模块时,使用这个钩子hook_update_N

在钩子中,你必须实现正确的逻辑:

$node = Node::create([
    'type'        => 'article',
    'title'       => 'Druplicon test',
    'field_image' => [
      'target_id' => $file->id(),
      'alt' => 'Hello world',
      'title' => 'Goodbye world'
    ],
]);
$node->save();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 2011-12-28
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    相关资源
    最近更新 更多