created sapi processor to unindex unused taxo terms, added term id condition on sapi query

This commit is contained in:
2021-02-24 14:14:56 +01:00
parent 9f0b257c62
commit 182b61b08f
5 changed files with 145 additions and 16 deletions

View File

@@ -40,11 +40,25 @@ class Base extends ControllerBase {
// Set additional conditions.
// in case we search for material references like W0117
if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $this->keys, $matches)) {
$conditions = $this->query->createConditionGroup('OR');
$ref_conditions = $this->query->createConditionGroup('OR');
foreach ($matches[0] as $key => $value) {
$conditions->addCondition('field_reference', $value);
$ref_conditions->addCondition('field_reference', $value);
}
$this->query->addConditionGroup($conditions);
$this->query->addConditionGroup($ref_conditions);
}
// in case of term id provided restrict the keys to taxo fields
if ($this->term) {
$term_conditions = $this->query->createConditionGroup('OR');
$term = (int) $this->term;
foreach (['tag_tid', 'thesaurus_tid'] as $field) {
$term_conditions->addCondition($field, $term);
}
// foreach (['tag_name', 'thesaurus_name'] as $field) {
// $term_conditions->addCondition($field, $this->keys);
// }
// $term_conditions->addCondition('tag_name', $this->keys);
// $term_conditions->addCondition('thesaurus_name', $this->keys);
$this->query->addConditionGroup($term_conditions);
}
// Restrict the search to specific languages.

View File

@@ -0,0 +1,78 @@
<?php
namespace Drupal\materio_sapi\Plugin\search_api\processor;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\user\UserInterface;
/**
* Excludes unused terms from indexes.
*
* @SearchApiProcessor(
* id = "term_used",
* label = @Translation("Term used"),
* description = @Translation("Exclude unused taxonomy terms from being indexed."),
* stages = {
* "alter_items" = 0,
* },
* )
*/
class TermUsed extends ProcessorPluginBase {
/**
* {@inheritdoc}
*/
public static function supportsIndex(IndexInterface $index) {
$interface = EntityPublishedInterface::class;
foreach ($index->getDatasources() as $datasource) {
$entity_type_id = $datasource->getEntityTypeId();
if (!$entity_type_id) {
continue;
}
// We support users and any entities that implement
// \Drupal\Core\Entity\EntityPublishedInterface.
if ($entity_type_id === 'taxonomy_term') {
return TRUE;
}
// $entity_type = \Drupal::entityTypeManager()
// ->getDefinition($entity_type_id);
// if ($entity_type && $entity_type->entityClassImplements($interface)) {
// return TRUE;
// }
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function alterIndexedItems(array &$items) {
// Annoyingly, this doc comment is needed for PHPStorm. See
// http://youtrack.jetbrains.com/issue/WI-23586
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach ($items as $item_id => $item) {
$term = $item->getOriginalObject()->getValue();
$enabled = TRUE;
// $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
// $count_query = $entity_storage->getQuery()
// ->condition('type', 'materiau')
// ->accessCheck(TRUE)
// ->condition('status', '1')
// ->count();
//
// $this->count = $this->count_query->execute();
$query = \Drupal::database()->select('taxonomy_index', 'ti');
$query->fields('ti', array('nid'));
$query->condition('ti.tid', $term->id());
$query->distinct(TRUE);
$result = $query->execute();
$nodeIds = $result->fetchCol();
if (!$nodeIds) {
unset($items[$item_id]);
}
}
}
}