Skip to content

Commit

Permalink
COMP-145 Complete taxonomy copy script + try url key fix
Browse files Browse the repository at this point in the history
  • Loading branch information
camilocodes committed Jan 4, 2024
1 parent 0f936eb commit 784582b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
62 changes: 42 additions & 20 deletions custom/modules/comp_core/scripts/type2taxo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion custom/modules/comp_crud/comp_crud.module
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 784582b

Please sign in to comment.