我必须通过php代码添加分类法。分类必须有两个字段。我向test_taxonomy.install添加了新函数,它似乎有效:
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();
}为分类学增加了新的词汇表。
档案内容:
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
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: falsefield.field.taxonomy_term.field_regulation_test.yml
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函数中加载它们。
编辑:我试过了
\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的包丢失了。
发布于 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将确保在从配置导入创建依赖配置之后,在部署例程的最后一次捕获这个钩子。
https://drupal.stackexchange.com/questions/298364
复制相似问题