From 784582b8ccd4cba832259cdaf9bbe93d026cabec Mon Sep 17 00:00:00 2001 From: Camilo Villamizar Date: Thu, 4 Jan 2024 14:53:07 -0400 Subject: [PATCH] COMP-145 Complete taxonomy copy script + try url key fix --- .../comp_cms_extend.libraries.yml | 1 - .../modules/comp_core/scripts/type2taxo.php | 62 +++++++++++++------ custom/modules/comp_crud/comp_crud.module | 2 +- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/custom/modules/comp_cms_extend/comp_cms_extend.libraries.yml b/custom/modules/comp_cms_extend/comp_cms_extend.libraries.yml index 4af67d12..1af15f6f 100644 --- a/custom/modules/comp_cms_extend/comp_cms_extend.libraries.yml +++ b/custom/modules/comp_cms_extend/comp_cms_extend.libraries.yml @@ -2,7 +2,6 @@ openseadragon: remote: https://github.com/openseadragon/openseadragon version: 2.2.1 license: - url: https://raw.githubusercontent.com/openseadragon/openseadragon/master/LICENSE.txt name: BSD 3-clause gpl-compatible: true js: diff --git a/custom/modules/comp_core/scripts/type2taxo.php b/custom/modules/comp_core/scripts/type2taxo.php index 534c1d8a..a38362d8 100644 --- a/custom/modules/comp_core/scripts/type2taxo.php +++ b/custom/modules/comp_core/scripts/type2taxo.php @@ -6,8 +6,10 @@ * * Copies Type field text to taxonomy for composites in for composites.lib.unb.ca. */ +use Drupal\node\Entity\Node; +use Drupal\taxonomy\Entity\Term; - type2taxo(); +type2taxo(); /** * Copy Type field text to taxonomy. @@ -24,37 +26,57 @@ function type2taxo() { $composite = Node::load($id) ?? NULL; if ($composite) { - $title = $composite->getName(); + $title = $composite->title->getValue()[0]['value']; + $type = $composite->field_type->getValue()[0]['value']; + $type = $type == "Encoenia" ? "Encaenia" : $type; + $type = $type == "Canadian Officers Training Corps" ? + "Canadian Officers' Training Corps" : $type; + $composite->field_composite_type->setvalue(find_add_term('composite_types', $type)); + echo dump($composite->field_composite_type->getvalue()); $composite->save(); - echo "[-] [$title]->[Updated]\n"; + echo "[-] [$title]->[Processed]\n"; } } } } /** - * Add multiple terms to a given vocabulary. + * Find or create taxonomy term. * * @param string $vid - * A string indicating the id of the vocabulary to update. - * @param array $terms - * An array containing the names of the terms to add. - * @param int $parent_id - * The ID of the parent term, if any. + * The ID of the vocabulary for the term. + * @param string $name + * The name for the term. + * + * @return int + * The ID of the term (int). */ -function add_terms(string $vid, array $terms, int $parent_id = NULL) { +function find_add_term(string $vid, string $name) { + $name = trim($name); + + $terms = Drupal::entityQuery('taxonomy_term') + ->condition('vid', $vid) + ->condition('name', $name, 'LIKE') + ->accesscheck(false) + ->execute(); - foreach ($terms as $term) { - $new_term = Term::create([ - 'vid' => $vid, - 'name' => $term, - ]); + reset($terms); + $tid = key($terms); - $new_term->set('parent', $parent_id); - $new_term->save(); + if (empty($tid)) { + if ($name != NULL) { + $term = Term::create([ + 'name' => $name, + 'vid' => $vid, + ]); - echo "[+] [$term]->[$vid]\n"; - $new_terms[] = $new_term->id(); + $term->save(); + $tid = $term->id(); + } + else { + $tid = NULL; + } } - return $new_terms; + return $tid; +} \ No newline at end of file diff --git a/custom/modules/comp_crud/comp_crud.module b/custom/modules/comp_crud/comp_crud.module index 551dfbc8..b2f6e99b 100644 --- a/custom/modules/comp_crud/comp_crud.module +++ b/custom/modules/comp_crud/comp_crud.module @@ -22,7 +22,7 @@ function comp_crud_entity_presave($entity) { // Process and add decade value. // Retrieve year. $year = $entity->get('field_comp_year')->getValue()[0]['value'] ?? NULL; - $decade = intdiv($year, 10) * 10; + $decade = $year ? intdiv($year, 10) * 10 : 0; $decade = $decade >= 1870 ? $decade : 0; $entity->set('field_comp_decade', $decade);