2

I have to add taxonomy by php code. Taxonomy has to got 2 fields. I added new function to test_taxonomy.install, and it seems to work:

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();
}

Added new vocabulary to taxonomy.

Content of file:

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
 

The problematic part is when i try to add fields to the vocabulary. I created 2 files

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

But i am not sure how can i load them in update_xxx function.

EDIT: I tried

    \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();

but i'm getting Missing bundle for entity type taxonomy_term while trying to update module

2
  • Uh, seems I misunderstood the problem in my answer below. Thought you wanted to create terms, after some fields were added to the terms. But you seem to try to create a vocabulary and fields from hook_update_N. Why's that? Why don't you create it from config?
    – leymannx
    Nov 23, 2020 at 19:31
  • 1
    It is requied from client. We have to update module and add requieded vocabulary with fields. I decided not to use file like shown above, and add fields programatically, using Naveed Quadri answer from here Nov 24, 2020 at 9:38

1 Answer 1

3

Yes, you can't create content before the relying config has been imported. That's a classic Drupal 8 paradox which has just recently been solved by a new hook: hook_deploy_NAME(&$sandbox).

Update to the latest Drush and from now on run drush deploy as part of your deployment routine, which replaces the old drush updb && drush cim routine. Then create a new file in your module: MYMODULE.deploy.php. In there implement MYMODULE_deploy_NAME(&$sandbox) where NAME can be any unique name (or an incrementing number as well). And use this hook to create the terms, same as in the hook_update_N(&$sandbox) in your question.

drush deploy will ensure that this hook will be picked up at the very last in the deployment routine, after the relying config has been created from configuration import.

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.