From b93d7914195adf90dfcb7443ddec1b72c447af1f Mon Sep 17 00:00:00 2001 From: bach Date: Mon, 16 Nov 2026 21:14:32 +0100 Subject: [PATCH] materio_ai: requeue nodes when a referenced term changes (V7) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM --- .../custom/materio_ai/materio_ai.module | 26 +++++++ .../custom/materio_ai/src/Vectorizer.php | 72 +++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/web/modules/custom/materio_ai/materio_ai.module b/web/modules/custom/materio_ai/materio_ai.module index b1b19b60..bb60484a 100644 --- a/web/modules/custom/materio_ai/materio_ai.module +++ b/web/modules/custom/materio_ai/materio_ai.module @@ -8,6 +8,7 @@ declare(strict_types=1); use Drupal\node\NodeInterface; +use Drupal\taxonomy\TermInterface; /** * Implements hook_ENTITY_TYPE_insert() for node. @@ -40,3 +41,28 @@ function materio_ai_node_translation_delete(NodeInterface $translation): void { function materio_ai_node_delete(NodeInterface $node): void { \Drupal::service('materio_ai.embedding_storage')->delete((int) $node->id()); } + +/** + * Implements hook_ENTITY_TYPE_update() for taxonomy_term. + * + * A renamed tag, thesaurus term or company changes the text of its nodes. + */ +function materio_ai_taxonomy_term_update(TermInterface $term): void { + \Drupal::service('materio_ai.vectorizer')->enqueueForTerm($term); +} + +/** + * Implements hook_ENTITY_TYPE_translation_delete() for taxonomy_term. + */ +function materio_ai_taxonomy_term_translation_delete(TermInterface $translation): void { + \Drupal::service('materio_ai.vectorizer')->enqueueForTerm($translation, TRUE); +} + +/** + * Implements hook_ENTITY_TYPE_predelete() for taxonomy_term. + * + * Predelete: the node references still exist, so the nodes can be found. + */ +function materio_ai_taxonomy_term_predelete(TermInterface $term): void { + \Drupal::service('materio_ai.vectorizer')->enqueueForTerm($term, TRUE); +} diff --git a/web/modules/custom/materio_ai/src/Vectorizer.php b/web/modules/custom/materio_ai/src/Vectorizer.php index 1ee00308..bae9ce9c 100644 --- a/web/modules/custom/materio_ai/src/Vectorizer.php +++ b/web/modules/custom/materio_ai/src/Vectorizer.php @@ -11,6 +11,7 @@ use Drupal\materio_ai\Corpus\CorpusBuilder; use Drupal\materio_ai\Embedding\EmbeddingClientInterface; use Drupal\materio_ai\Storage\EmbeddingStorage; use Drupal\node\NodeInterface; +use Drupal\taxonomy\TermInterface; /** * Keeps the vectors of a node in step with its texts. @@ -50,6 +51,77 @@ final class Vectorizer { } } + /** + * Node fields whose referenced terms go into the text (CorpusBuilder). + */ + public const TERM_FIELDS = ['field_tags', 'field_thesaurus', 'field_manufacturer', 'field_distributor']; + + /** + * Queues the nodes that use a term, when its text changed (V7). + * + * A renamed term, a new synonym or a company that moved changes the text of + * every node referencing it, in each language. Unchanged languages are then + * skipped by their hash, without calling the model. + * + * @return int + * Number of queued nodes. + */ + public function enqueueForTerm(TermInterface $term, bool $force = FALSE): int { + if (!$this->isEnabled() || (!$force && !$this->termTextChanged($term))) { + return 0; + } + $query = $this->entityTypeManager->getStorage('node')->getQuery() + ->accessCheck(FALSE) + ->condition('type', CorpusBuilder::BUNDLES, 'IN'); + $or = $query->orConditionGroup(); + foreach (self::TERM_FIELDS as $field) { + $or->condition($field, $term->id()); + } + $nids = $query->condition($or)->execute(); + $queue = $this->queueFactory->get(self::QUEUE); + foreach ($nids as $nid) { + $queue->createItem(['nid' => (int) $nid]); + } + return count($nids); + } + + /** + * Tells whether what the corpus reads from a term changed on save. + * + * Compares, for every translation, the name, the synonyms and the country + * of the public address with the original term. + */ + private function termTextChanged(TermInterface $term): bool { + $original = method_exists($term, 'getOriginal') ? $term->getOriginal() : ($term->original ?? NULL); + if (!$original instanceof TermInterface) { + return TRUE; + } + $langcodes = array_unique(array_merge(array_keys($term->getTranslationLanguages()), array_keys($original->getTranslationLanguages()))); + foreach ($langcodes as $langcode) { + if (!$term->hasTranslation($langcode) || !$original->hasTranslation($langcode)) { + return TRUE; + } + if (self::termText($term->getTranslation($langcode)) !== self::termText($original->getTranslation($langcode))) { + return TRUE; + } + } + return FALSE; + } + + /** + * What the corpus reads from one term translation. + */ + private static function termText(TermInterface $term): array { + $text = [$term->label()]; + if ($term->hasField('field_synonyms')) { + $text[] = array_column($term->get('field_synonyms')->getValue(), 'value'); + } + if ($term->hasField('field_public_address')) { + $text[] = $term->get('field_public_address')->first()?->getCountryCode(); + } + return $text; + } + /** * Updates the vectors of a node. *