Skip to content

Instantly share code, notes, and snippets.

@Mykola-Veryha
Last active February 19, 2022 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mykola-Veryha/4fc9e40c8b1c425f35f08b999fe368b5 to your computer and use it in GitHub Desktop.
Save Mykola-Veryha/4fc9e40c8b1c425f35f08b999fe368b5 to your computer and use it in GitHub Desktop.
Deploy vocabulary with default terms
<?php
/**
* @file
* Contains install, update and uninstall functions for the User module.
*/
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Site\Settings;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Create a field if it doesn't exist.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function deploy_helper_ensure_field(string $entity_type, string $bundle, string $field) {
$config_directory = new FileStorage(Settings::get('config_sync_directory'));
$field_storage_name = 'field.storage.' . $entity_type . '.' . $field;
$config_record = $config_directory->read($field_storage_name);
if (!FieldStorageConfig::loadByName($config_record['entity_type'], $config_record['field_name'])) {
FieldStorageConfig::create($config_record)->save();
}
$field_config_name = 'field.field.' . $entity_type . '.' . $bundle . '.' . $field;
$config_record = $config_directory->read($field_config_name);
if (!FieldConfig::loadByName($config_record['entity_type'], $config_record['bundle'], $config_record['field_name'])) {
FieldConfig::create($config_record)->save();
}
}
/**
* Create vocabulary from configurations.
*/
function deploy_helper_ensure_vocabulary($config_name) {
$config_path = Settings::get('config_sync_directory');
$source = new FileStorage($config_path);
$config_storage = Drupal::service('config.storage');
$config_storage->write($config_name, $source->read($config_name));
}
/**
* Implements hook_update_N().
*/
function pfncsmpfcomau_user_update_9309() {
try {
deploy_helper_ensure_vocabulary('taxonomy.vocabulary.australia_states');
deploy_helper_ensure_vocabulary('taxonomy.vocabulary.new_zealand_states');
deploy_helper_ensure_field('taxonomy_term', 'australia_states', 'field_state_code');
deploy_helper_ensure_field('taxonomy_term', 'new_zealand_states', 'field_state_code');
$term_storage = Drupal::entityTypeManager()->getStorage('taxonomy_term');
$states = [
'australia_states' => [
'NSW' => 'New South Wales',
'QLD' => 'Queensland',
'SA' => 'South Australia',
'TAS' => 'Tasmania',
'VIC' => 'Victoria',
'WA' => 'Western Australia',
'ACT' => 'Australian Capital Territory',
'NT' => 'Northern Territory',
],
'new_zealand_states' => [
'NZ-NTL' => 'Northland',
'NZ-AUK' => 'Auckland',
'NZ-WKO' => 'Waikato',
'NZ-BOP' => 'Bay of Plenty',
'NZ-GIS' => 'Gisborne',
'NZ-HKB' => "Hawke's Bay",
'NZ-TKI' => 'Taranaki',
'NZ-MWT' => 'Manawatū-Whanganui',
'NZ-WGN' => 'Wellington',
'NZ-TAS' => 'Tasman',
'NZ-NSN' => 'Nelson',
'NZ-MBH' => 'Marlborough',
'NZ-WTC' => 'West Coast',
'NZ-CAN' => 'Canterbury',
'NZ-OTA' => 'Otago',
'NZ-STL' => 'Southland',
],
];
foreach ($states as $vocabulary_id => $country_states) {
foreach ($country_states as $code => $name) {
$term_storage->create([
'vid' => $vocabulary_id,
'name' => $name,
'field_state_code' => $code,
])->save();
}
}
}
catch (Exception $e) {
watchdog_exception('deploy_state_terms', $e);
}
}
@Mykola-Veryha
Copy link
Author

Drupal. You can use the above script if you need to deploy vocabulary with default terms. The script imports configurations and executes code after the config import to create the default terms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment