Exclude entities which have a redirect from the search api index
For a custom project, we use a taxonomy to tag nodes. Some terms have their own page, while others redirect to a node. The search on the site has one index which contains both nodes and terms, but the terms which are redirecting shouldn't show up when viewing a search results page. While it's possible to use hook_search_api_index_items_alter(), a nicer way to exclude them is by using a processor plugin so you can enable them in the UI per index. The relevant code is underneath. Adjust to your own likings - and maybe inject the service if you want to as well :)
<span style="color: #000000"><span style="color: #0000BB"><?php <br /></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">project</span><span style="color: #007700">\</span><span style="color: #0000BB">Plugin</span><span style="color: #007700">\</span><span style="color: #0000BB">search_api</span><span style="color: #007700">\</span><span style="color: #0000BB">processor</span><span style="color: #007700">;<br><br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">search_api</span><span style="color: #007700">\</span><span style="color: #0000BB">IndexInterface</span><span style="color: #007700">;<br>use </span><span style="color: #0000BB">Drupal</span><span style="color: #007700">\</span><span style="color: #0000BB">search_api</span><span style="color: #007700">\</span><span style="color: #0000BB">Processor</span><span style="color: #007700">\</span><span style="color: #0000BB">ProcessorPluginBase</span><span style="color: #007700">;<br><br></span><span style="color: #FF8000">/**<br> * Excludes entities which have a redirect.<br> *<br> * @SearchApiProcessor(<br> * id = "entity_redirect",<br> * label = @Translation("Entity redirect"),<br> * description = @Translation("Exclude entities which have a redirect from being indexed."),<br> * stages = {<br> * "alter_items" = 0,<br> * },<br> * )<br> */<br></span><span style="color: #007700">class </span><span style="color: #0000BB">EntityRedirect </span><span style="color: #007700">extends </span><span style="color: #0000BB">ProcessorPluginBase </span><span style="color: #007700">{<br><br> </span><span style="color: #FF8000">/**<br> * {@inheritdoc}<br> */<br> </span><span style="color: #007700">public static function </span><span style="color: #0000BB">supportsIndex</span><span style="color: #007700">(</span><span style="color: #0000BB">IndexInterface $index</span><span style="color: #007700">) {<br> foreach (</span><span style="color: #0000BB">$index</span><span style="color: #007700">-></span><span style="color: #0000BB">getDatasources</span><span style="color: #007700">() as </span><span style="color: #0000BB">$datasource</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$entity_type_id </span><span style="color: #007700">= </span><span style="color: #0000BB">$datasource</span><span style="color: #007700">-></span><span style="color: #0000BB">getEntityTypeId</span><span style="color: #007700">();<br> if (!</span><span style="color: #0000BB">$entity_type_id</span><span style="color: #007700">) {<br> continue;<br> }<br> if (</span><span style="color: #0000BB">$entity_type_id </span><span style="color: #007700">=== </span><span style="color: #DD0000">'node' </span><span style="color: #007700">|| </span><span style="color: #0000BB">$entity_type_id </span><span style="color: #007700">== </span><span style="color: #DD0000">'taxonomy_term'</span><span style="color: #007700">) {<br> return </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">;<br> }<br> }<br> return </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">;<br> }<br><br> </span><span style="color: #FF8000">/**<br> * {@inheritdoc}<br> */<br> </span><span style="color: #007700">public function </span><span style="color: #0000BB">alterIndexedItems</span><span style="color: #007700">(array &</span><span style="color: #0000BB">$items</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$repository </span><span style="color: #007700">= \</span><span style="color: #0000BB">Drupal</span><span style="color: #007700">::</span><span style="color: #0000BB">service</span><span style="color: #007700">(</span><span style="color: #DD0000">'redirect.repository'</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">$pathAliasmanager </span><span style="color: #007700">= \</span><span style="color: #0000BB">Drupal</span><span style="color: #007700">::</span><span style="color: #0000BB">service</span><span style="color: #007700">(</span><span style="color: #DD0000">'path.alias_manager'</span><span style="color: #007700">);<br> </span><span style="color: #FF8000">/** @var \Drupal\search_api\Item\ItemInterface $item */<br> </span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$items </span><span style="color: #007700">as </span><span style="color: #0000BB">$item_id </span><span style="color: #007700">=> </span><span style="color: #0000BB">$item</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$object </span><span style="color: #007700">= </span><span style="color: #0000BB">$item</span><span style="color: #007700">-></span><span style="color: #0000BB">getOriginalObject</span><span style="color: #007700">()-></span><span style="color: #0000BB">getValue</span><span style="color: #007700">();<br> try {<br> </span><span style="color: #0000BB">$path </span><span style="color: #007700">= </span><span style="color: #0000BB">$object</span><span style="color: #007700">-></span><span style="color: #0000BB">toUrl</span><span style="color: #007700">()-></span><span style="color: #0000BB">toString</span><span style="color: #007700">();<br> </span><span style="color: #0000BB">$path </span><span style="color: #007700">= </span><span style="color: #0000BB">$pathAliasmanager</span><span style="color: #007700">-></span><span style="color: #0000BB">getPathByAlias</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">$path </span><span style="color: #007700">= </span><span style="color: #0000BB">ltrim</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">, </span><span style="color: #DD0000">'/'</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">$redirect </span><span style="color: #007700">= </span><span style="color: #0000BB">$repository</span><span style="color: #007700">-></span><span style="color: #0000BB">findMatchingRedirect</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">);<br> if (!empty(</span><span style="color: #0000BB">$redirect</span><span style="color: #007700">)) {<br> unset(</span><span style="color: #0000BB">$items</span><span style="color: #007700">[</span><span style="color: #0000BB">$item_id</span><span style="color: #007700">]);<br> }<br> }<br> catch (\</span><span style="color: #0000BB">Exception $ignored</span><span style="color: #007700">) {}<br> }<br> }<br><br>}<br></span><span style="color: #0000BB">?></span></span>