Skip to content

Instantly share code, notes, and snippets.

@AllieRays
Last active November 14, 2021 17:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AllieRays/4f88b51dc1a72c55322d8d7acba31687 to your computer and use it in GitHub Desktop.
Save AllieRays/4f88b51dc1a72c55322d8d7acba31687 to your computer and use it in GitHub Desktop.
Programmatically create multiple taxonomy terms in Drupal 8
<?php
// Programmatically create taxonomy terms for taxonomy module on install.
// This is the bar.install file included in a module.
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\Yaml\Yaml;
function foo_taxonomy_install() {
$vocabularies = [
'bar',
'bazz',
];
foreach ($vocabularies as $vocabulary) {
foo_taxonomy_import_term_data_for_vocabulary($vocabulary);
}
}
function foo_taxonomy_get_term_data($vocabulary) {
$terms_file_path = file_get_contents(drupal_get_path('module', 'foo_taxonomy') . '/vocabulary_data/' . $vocabulary . '.terms.yml');
return Yaml::parse($terms_file_path);
}
function foo_clean_term_for_url($term_name_url) {
$term_name_url = strtolower(preg_replace('/-+/', '-', preg_replace('/([^a-zA-Z0-9\-]+)/', '-', $term_name_url)));
return $term_name_url;
}
function foo_taxonomy_import_term_data_for_vocabulary($vocabulary) {
$terms_info = foo_taxonomy_get_term_data($vocabulary);
$preexisting_terms = [];
foreach ($terms_info as $term_name => $termInfo) {
$term = array_pop(taxonomy_term_load_multiple_by_name($term_name, $vocabulary));
if (!$term) {
$data = [
'vid' => $vocabulary,
'name' => $term_name,
'description' => [
'value' => 'The term: ' . $term_name . ' belongs to the ' . $vocabulary . ' taxonomy vocabulary.',
'format' => 'full_html',
],
];
if (!empty($termInfo)) {
if (!empty($termInfo['parent'])) {
if ($preexisting_terms[$termInfo['parent']]) {
$data['parent'] = [$preexisting_terms[$termInfo['parent']]];
}
else {
$message = "The parent term {$termInfo['parent']} did not exist when trying to create {$term_name} in vocabulary {$vocabulary}.";
drupal_set_message($message, 'error');
\Drupal::logger('foo_taxonomy')->error($message);
}
}
if (!empty($termInfo['fields'])) {
foreach ($termInfo['fields'] as $field_name => $field_value) {
$data[$field_name] = $field_value;
}
}
}
$term = Term::create($data);
$term->save();
}
$preexisting_terms[$term->getName()] = $term->id();
//clean and save taxonomy term name for url.
$term_name_url = foo_clean_term_for_url($term_name);
$vocabulary_url = foo_clean_term_for_url($vocabulary);
\Drupal::service('path.alias_storage')
->save("/taxonomy/term/" . $term->id(), "/" . $vocabulary_url . "/" . $term_name_url, "en");
}
}
/*
* Just in case we want to delete terms. Do not delete terms on uninstall unless you specifically know you want to.
* Be careful of deleting taxonomy terms, because they provide structure of the site. Change 'department' to the vocabulary you wish to delete.
*/
//function foo_taxonomy_remove_created_terms() {
// $terms_info = foo_taxonomy_get_term_data('department');
// foreach ($terms_info as $term_name => $termInfo) {
// $terms = taxonomy_term_load_multiple_by_name($term_name);
// foreach ($terms as $term) {
// $term->delete();
// }
// }
//}
//
//function foo_taxonomy_uninstall() {
// foo_taxonomy_remove_created_terms();
//}
# Subject Specialities Taxonomy terms.
'Area Studies':
'African Studies':
parent: 'Area Studies'
'East Asian Studies':
parent: 'Area Studies'
'Chinese Studies':
parent: 'Area Studies'
@esod
Copy link

esod commented Feb 4, 2020

Very nice. Thanks for sharing.

@mogbril
Copy link

mogbril commented Jan 23, 2021

Very very nice. Thanks so much for sharing this.

Just a small note. the file name bar.yaml should be bar.terms.yml

Thanks

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