MappedTerms.php in Search API Field Map 8
File
src/Plugin/search_api/processor/MappedTerms.php
View source
<?php
namespace Drupal\search_api_field_map\Plugin\search_api\processor;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api_field_map\Plugin\search_api\processor\Property\MappedTermsProperty;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\taxonomy\Entity\Term;
class MappedTerms extends ProcessorPluginBase {
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];
if (!$datasource) {
$definition = [
'label' => $this
->t('Mapped terms'),
'description' => $this
->t('Normalize multiple taxonomy terms into mapped terms.'),
'type' => 'string',
'processor_id' => $this
->getPluginId(),
];
$properties['mapped_terms'] = new MappedTermsProperty($definition);
}
return $properties;
}
public function addFieldValues(ItemInterface $item) {
$mapped_terms = $this
->getFieldsHelper()
->filterForPropertyPath($item
->getFields(), NULL, 'mapped_terms');
$entity = $item
->getOriginalObject()
->getValue();
if (!$entity) {
return;
}
$mapped_terms_destination_values = [];
$entity_type = $entity
->getEntityTypeId();
$bundle_type = $entity
->bundle();
$entityManager = \Drupal::service('entity_field.manager');
$bundle_fields = $entityManager
->getFieldDefinitions($entity_type, $bundle_type);
$bundle_taxonomy_fields = [];
foreach ($mapped_terms as $mapped_term) {
foreach ($bundle_fields as $bundle_field) {
$bundle_field_type = $bundle_field
->getType();
if ($bundle_field_type === "entity_reference") {
$bundle_field_settings = $bundle_field
->getSettings();
if ($bundle_field_settings['target_type'] == 'taxonomy_term') {
$bundle_taxonomy_fields[$bundle_field
->getName()] = $bundle_field
->getLabel();
}
}
}
foreach ($bundle_taxonomy_fields as $taxonomy_field_id => $taxonomy_field_name) {
$entity_terms = $entity->{$taxonomy_field_id}
->getValue();
if (empty($entity_terms)) {
continue;
}
foreach ($entity_terms as $term) {
$term_entity = Term::load($term['target_id']);
if (!empty($term_entity)) {
$field_definitions = $term_entity
->getFieldDefinitions();
$mapped_term_definitions = array_filter($field_definitions, function ($field_definition) {
return $field_definition
->getType() === "mapped_terms";
});
$mapped_term_field_names = array_map(function ($mapped_term_definitions) {
return $mapped_term_definitions
->getName();
}, $mapped_term_definitions);
foreach ($mapped_term_field_names as $field_name) {
$mapped_term_values = $term_entity->{$field_name}
->getValue();
if (!empty($mapped_term_values)) {
foreach ($mapped_term_values as $mapped_term_value) {
if (strlen(trim($mapped_term_value['value']))) {
$mapped_terms_destination_values[] = $mapped_term_value['value'];
}
}
}
}
}
}
}
$mapped_terms_destination_values = array_unique($mapped_terms_destination_values);
foreach ($mapped_terms_destination_values as $value) {
$existing_values = $mapped_term
->getValues();
if (!array_search($value, $existing_values)) {
$mapped_term
->addValue($value);
}
}
}
}
}
Classes
Name |
Description |
MappedTerms |
Normalize multiple taxonomy terms into mapped terms. |