materio_ai: requeue nodes when a referenced term changes (V7)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user