Skip to content
Snippets Groups Projects
Commit c6bacbfd authored by Stephen Mustgrave's avatar Stephen Mustgrave
Browse files

Resolve #3055325 "Exclude entity based"

parent 35700d98
Branches
Tags
1 merge request!14Resolve #3055325 "Exclude entity based"
Pipeline #387521 passed
......@@ -25,6 +25,7 @@ Submit bug reports and feature suggestions, or track changes in the
- Requirements
- Installation
- Sub-modules
- Configuration
- Maintainers
......@@ -41,6 +42,11 @@ information, see
[Installing Drupal Modules](https://www.drupal.org/docs/extending-drupal/installing-drupal-modules).
## Sub-modules
* Search API Exclude Entity By Field
* Exclude one or more entities from being indexed in Search API by a field value.
## Configuration
- Add a `"Search API Exclude Entity"` field to the entity types / bundles that
......
......@@ -26,6 +26,7 @@
"php": ">=8.1.0"
},
"require-dev": {
"drupal/metatag": "^2.1",
"drupal/single_content_sync": "^1.4"
}
}
plugin.plugin_configuration.search_api_processor.search_api_exclude_entity_by_field_processor:
type: mapping
label: 'Search Api Exclude Entity settings'
mapping:
fields:
type: sequence
label: 'Fields'
sequence:
label: 'Field name'
type: string
name: Search API Exclude Entity By Field
type: module
description: Exclude one or more entities from being indexed in Search API by a field value.
core_version_requirement: ^10 || ^11
package: Search
dependencies:
- search_api_exclude_entity:search_api_exclude_entity
<?php
namespace Drupal\search_api_exclude_entity_by_field\Plugin\search_api\processor;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\search_api\Plugin\PluginFormTrait;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Excludes entities marked as 'excluded' from being indexes.
*
* @SearchApiProcessor(
* id = "search_api_exclude_entity_by_field_processor",
* label = @Translation("Search API Exclude Entity By Field"),
* description = @Translation("Exclude some items from being indexed depends of the field values."),
* stages = {
* "alter_items" = -50
* }
* )
*/
class SearchApiExcludeEntityByField extends ProcessorPluginBase implements PluginFormInterface {
use PluginFormTrait;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var static $processor */
$processor = parent::create($container, $configuration, $plugin_id, $plugin_definition);
return $processor;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$config = $this->getConfiguration();
$fields = $this->index->getFields();
foreach ($fields as $field) {
$fieldName = $field->getFieldIdentifier();
$form['fields'][$fieldName] = [
'#type' => 'textfield',
'#title' => $field->getLabel(),
'#description' => $this->t("The item won't be in the index if the field has this value"),
'#default_value' => $config['fields'][$fieldName] ?? '',
'#multiple' => TRUE,
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
$values = $form_state->getValues();
$indexName = $this->index->getOriginalId();
// Remove empty values.
if (isset($values[$indexName]['fields']) && is_array($values[$indexName]['fields'])) {
$values[$indexName]['fields'] = array_filter($values[$indexName]['fields']);
}
$this->setConfiguration($values);
}
/**
* {@inheritdoc}
*/
public function alterIndexedItems(array &$items): void {
$config = $this->getConfiguration();
$indexName = $this->index->getOriginalId();
$configuredField = $config[$indexName]['fields'];
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach ($items as $item_id => $item) {
try {
$object = $item->getOriginalObject()->getValue();
$fieldList = $object->getFields();
foreach ($configuredField as $fieldName => $fieldValue) {
if (isset($fieldList[$fieldName])) {
$value = $fieldList[$fieldName]->getValue();
$value = $value[0]['value'];
if ($value == $this->sanitizeFilterString($fieldValue)) {
unset($items[$item_id]);
}
}
}
}
catch (\Exception) {
$this->messenger->addError($this->t('Error getting item object from Search API Entity Exclude By Field plugin.'));
}
}
}
/**
* Sanitize the values of the field depends on the comparison type.
*
* @param mixed $value
* The value to be sanitized.
*
* @return mixed
* The sanitized value.
*/
protected function sanitizeFilterString(mixed $value): mixed {
if ($value == 'false') {
return FALSE;
}
if ($value == 'true') {
return TRUE;
}
return $value;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment