From a263183bf2c55fc7335a76b2aefca93ab32124f3 Mon Sep 17 00:00:00 2001 From: Artem Boiko Date: Mon, 4 Mar 2024 23:11:46 +0200 Subject: [PATCH] Added fixes to indicators indexing --- .../src/Entity/QuanthubIndex.php | 9 +- .../processor/QuanthubIndicatorProcessor.php | 92 +++++++++++++------ 2 files changed, 70 insertions(+), 31 deletions(-) diff --git a/modules/quanthub_indicator/src/Entity/QuanthubIndex.php b/modules/quanthub_indicator/src/Entity/QuanthubIndex.php index 41747bb..9c1e3b7 100644 --- a/modules/quanthub_indicator/src/Entity/QuanthubIndex.php +++ b/modules/quanthub_indicator/src/Entity/QuanthubIndex.php @@ -292,8 +292,13 @@ protected function trackItemsInsertedOrUpdated($datasource_id, array $ids, $trac if ($this->hasValidTracker() && $this->status()) { $item_ids = []; foreach ($ids as $id) { - if (str_contains($id, 'entity:node')) { - $item_ids[] = $id; + if (str_contains($id, 'indicator')) { + if (!str_contains($id, 'entity:node')) { + $item_ids[] = 'entity:node/' . $id; + } + else { + $item_ids[] = $id; + } } else { $item_ids[] = Utility::createCombinedId($datasource_id, $id); diff --git a/modules/quanthub_indicator/src/Plugin/search_api/processor/QuanthubIndicatorProcessor.php b/modules/quanthub_indicator/src/Plugin/search_api/processor/QuanthubIndicatorProcessor.php index c8487ac..d86ee5e 100644 --- a/modules/quanthub_indicator/src/Plugin/search_api/processor/QuanthubIndicatorProcessor.php +++ b/modules/quanthub_indicator/src/Plugin/search_api/processor/QuanthubIndicatorProcessor.php @@ -22,6 +22,7 @@ * label = @Translation("Quanthub Indicator Processor"), * description = @Translation(""), * stages = { + * "alter_items" = 0, * "pre_index_save" = 0, * "preprocess_index" = -10, * "preprocess_query" = -10, @@ -205,37 +206,40 @@ protected function processField(FieldInterface $field) { } if ($this->indicatorId && $dataset_entity_localized) { - switch ($field->getFieldIdentifier()) { - case 'rendered_item': - $this->processRenderedItemField($field, $dataset_entity_localized); - break; - - case 'title': - if ($this->langcode && !empty($this->loadedIndicators[$this->datasetUrn][$this->indicatorId]['names'][$this->langcode])) { - $new_field_values[] = new TextValue($this->loadedIndicators[$this->datasetUrn][$this->indicatorId]['names'][$this->langcode]); - $field->setValues($new_field_values); - } - break; - - case 'field_topics': - $topics = []; - foreach ($dataset_entity_localized->field_topics->referencedEntities() as $referencedEntity) { - if ($referencedEntity->hasTranslation($this->langcode)) { - $topics[] = $referencedEntity->getTranslation($this->langcode)->id(); + if (method_exists($field, 'getFieldIdentifier')) { + switch ($field->getFieldIdentifier()) { + case 'rendered_item': + $this->processRenderedItemField($field, $dataset_entity_localized); + break; + + case 'title': + if ($this->langcode && !empty($this->loadedIndicators[$this->datasetUrn][$this->indicatorId]['names'][$this->langcode])) { + $new_field_values[] = new TextValue($this->loadedIndicators[$this->datasetUrn][$this->indicatorId]['names'][$this->langcode]); + $field->setValues($new_field_values); } - } - $field->setValues($topics); - break; - - case 'topics_name': - $topic_names = []; - foreach ($dataset_entity_localized->field_topics->referencedEntities() as $referencedEntity) { - if ($referencedEntity->hasTranslation($this->langcode)) { - $topic_names[] = new TextValue($referencedEntity->getTranslation($this->langcode)->name->getString()); + break; + + case 'field_topics': + $topics = []; + foreach ($dataset_entity_localized->field_topics->referencedEntities() as $referencedEntity) { + if ($referencedEntity->hasTranslation($this->langcode)) { + $topics[] = $referencedEntity->getTranslation($this->langcode) + ->id(); + } } - } - $field->setValues($topic_names); - break; + $field->setValues($topics); + break; + + case 'topics_name': + $topic_names = []; + foreach ($dataset_entity_localized->field_topics->referencedEntities() as $referencedEntity) { + if ($referencedEntity->hasTranslation($this->langcode)) { + $topic_names[] = new TextValue($referencedEntity->getTranslation($this->langcode)->name->getString()); + } + } + $field->setValues($topic_names); + break; + } } } } @@ -386,4 +390,34 @@ public function loadIndicators($items) { } } + /** + * Need to remove items with not existed indicators, on indexing. + */ + public function alterIndexedItems(array &$items) { + parent::alterIndexedItems($items); + $this->loadIndicators($items); + + foreach ($items as $key => $item) { + $entity = $item->getOriginalObject()->getValue(); + + if ($entity->getType() == 'indicator') { + $dataset_urn = $entity + ->field_dataset + ->first() + ->get('entity') + ->getTarget() + ->getValue() + ->field_quanthub_urn + ->getString(); + + if ( + empty($this->loadedIndicators) || + empty($this->loadedIndicators[$dataset_urn][$item->getExtraData('indicator_id')]) + ) { + unset($items[$key]); + } + } + } + } + }