hook_update_N 내부의 구성에서 새 필드를 추가하려면 어떻게해야합니까? [복제]

Nov 23 2020

PHP 코드로 분류를 추가해야합니다. 분류에는 2 개의 필드가 있어야합니다. 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
 

문제는 어휘에 필드를 추가하려고 할 때입니다. 2 개의 파일을 만들었습니다.

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: false

field.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에 대한 누락 된 번들이 있습니다.

답변

3 leymannx Nov 23 2020 at 15:36

예, 의존 구성을 가져 오기 전에는 콘텐츠를 만들 수 없습니다. 그것은 최근에 새로운 훅으로 해결 된 고전적인 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 구성 가져 오기에서 신뢰 구성이 생성 된 후 배포 루틴의 마지막에이 후크가 선택되도록합니다.