Skip to content

Instantly share code, notes, and snippets.

@gwagroves
Created October 24, 2017 09:43
Show Gist options
  • Save gwagroves/5a0bd024d39d28e6164fd777a90fec5a to your computer and use it in GitHub Desktop.
Save gwagroves/5a0bd024d39d28e6164fd777a90fec5a to your computer and use it in GitHub Desktop.
Exclude nodes from search API based on field value
<?php
namespace Drupal\my_module\Plugin\search_api\processor;
use Drupal\search_api\Processor\ProcessorPluginBase;
/**
* Excludes entities marked as 'excluded' from being indexes.
*
* @SearchApiProcessor(
* id = "my_module_exclude_processor",
* label = @Translation("My module exclude processor"),
* description = @Translation("Exclude nodes from being indexed."),
* stages = {
* "alter_items" = -50
* },
* locked = true,
* )
*/
class NodeExcludeProcessor extends ProcessorPluginBase {
/**
* {@inheritdoc}
*/
public function alterIndexedItems(array &$items) {
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach ($items as $item_id => $item) {
$object = $item->getOriginalObject()->getValue();
if ($object->getEntityTypeId() === 'node' && $object->getType() === 'my_node_type') {
if ($object->field_my_field->value === 'a value') {
unset($items[$item_id]);
continue;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment