You are here

public function SearchApiExcludeEntityProcessor::alterIndexedItems in Search API Exclude Entity 8

Alter the items to be indexed.

Parameters

\Drupal\search_api\Item\ItemInterface[] $items: An array of items to be indexed, passed by reference.

Overrides ProcessorPluginBase::alterIndexedItems

File

src/Plugin/search_api/processor/SearchApiExcludeEntityProcessor.php, line 178

Class

SearchApiExcludeEntityProcessor
Excludes entities marked as 'excluded' from being indexes.

Namespace

Drupal\search_api_exclude_entity\Plugin\search_api\processor

Code

public function alterIndexedItems(array &$items) {
  $config = $this
    ->getConfiguration()['fields'];

  /** @var \Drupal\search_api\Item\ItemInterface $item */
  foreach ($items as $item_id => $item) {
    $object = $item
      ->getOriginalObject()
      ->getValue();
    if (!$object instanceof EntityInterface) {
      continue;
    }
    $entity_type_id = $object
      ->getEntityTypeId();
    $bundle = $object
      ->bundle();
    if (isset($config[$entity_type_id]) && is_array($config[$entity_type_id])) {
      foreach ($config[$entity_type_id] as $field) {

        // We need to be sure that the field actually exists on the bundle
        // before getting the value to avoid InvalidArgumentException
        // exceptions.
        if ($this
          ->bundleHasField($entity_type_id, $bundle, $field)) {
          $value = $object
            ->get($field)
            ->getValue();
          if (isset($value[0]['value']) && $value[0]['value'] !== NULL) {
            $value = $value[0]['value'];
            if ($value) {
              unset($items[$item_id]);
              continue;
            }
          }
        }
      }
    }
  }
}