Created
October 24, 2017 09:43
-
-
Save gwagroves/5a0bd024d39d28e6164fd777a90fec5a to your computer and use it in GitHub Desktop.
Exclude nodes from search API based on field value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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