Skip to content

Commit

Permalink
Added fixes to indicators indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
artemboyko43 committed Mar 4, 2024
1 parent 58b35d7 commit a263183
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 31 deletions.
9 changes: 7 additions & 2 deletions modules/quanthub_indicator/src/Entity/QuanthubIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down Expand Up @@ -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]);
}
}
}
}

}

0 comments on commit a263183

Please sign in to comment.