首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从hook_update_N内部的配置中添加一个新字段?

如何从hook_update_N内部的配置中添加一个新字段?
EN

Drupal用户
提问于 2020-11-23 15:11:21
回答 1查看 735关注 0票数 2

我必须通过php代码添加分类法。分类必须有两个字段。我向test_taxonomy.install添加了新函数,它似乎有效:

代码语言:javascript
复制
function taxonomy_update_8805() {

    $config_path = 'modules/feature/test_taxonomy/config/update/';
    $source = new FileStorage($config_path);

    \Drupal::entityManager()->getStorage('taxonomy_vocabulary')
        ->create($source->read('taxonomy.vocabulary.regulation'))
        ->save();
}

为分类学增加了新的词汇表。

档案内容:

代码语言:javascript
复制
langcode: pl
status: true
dependencies:
  module:
    - test_remote_vocabulary
third_party_settings:
  test_remote_vocabulary:
    is_remote: 0
name: 'Test'
vid: regulation
description: 'Test desc'
hierarchy: 0
weight: 0

有问题的部分是当我试图在词汇表中添加字段时。我创建了两个文件

field.storage.taxonomy_term.field_regulation_test.yml

代码语言:javascript
复制
langcode: pl
status: true
dependencies:
  module:
    - taxonomy
    - text
id: taxonomy_term.regulation.test
field_name: test_field
entity_type: taxonomy_term
type: text_long
settings: {  }
module: text
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

field.field.taxonomy_term.field_regulation_test.yml

代码语言:javascript
复制
langcode: pl
status: true
dependencies:
  config:
    - field.storage.taxonomy_term.field_regulation_test
    - taxonomy.vocabulary.regulation
id: taxonomy_term.regulation.test
field_name: test_content
entity_type: taxonomy_term
bundle: regulation
label: TEST
description: 'Tekst test'
required: true
translatable: false
default_value: {  }
default_value_callback: ''
settings: {  }
field_type: text_long

但我不确定如何在update_xxx函数中加载它们。

编辑:我试过了

代码语言:javascript
复制
    \Drupal::entityManager()->getStorage('taxonomy_term')
        ->create($source->read('field.storage.taxonomy_term.field_regulation_abo'))
        ->save();
        
    \Drupal::entityManager()->getStorage('taxonomy_term')
        ->create($source->read('field.field.taxonomy_term.field_regulation_abo'))
        ->save();

但是,在尝试更新模块时,实体类型taxonomy_term的包丢失了。

EN

回答 1

Drupal用户

发布于 2020-11-23 15:36:27

是的,您不能在导入依赖配置之前创建内容。这是一个经典的Drupal 8悖论,最近被一个新的钩子解决了:hook_deploy_NAME(&$sandbox)

更新到最新的Drush,从现在开始运行drush deploy作为您的部署例程的一部分,它取代了旧的drush updb && drush cim例程。然后在模块中创建一个新文件:MYMODULE.deploy.php。在这里,实现MYMODULE_deploy_NAME(&$sandbox),其中NAME可以是任何唯一的名称(也可以是递增的数字)。并使用此钩子创建术语,与问题中的hook_update_N(&$sandbox)相同。

drush deploy将确保在从配置导入创建依赖配置之后,在部署例程的最后一次捕获这个钩子。

票数 3
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/298364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档