FederatedTerms.php in Search API Federated Solr 8.3
File
src/Plugin/search_api/processor/FederatedTerms.php
View source
<?php
namespace Drupal\search_api_federated_solr\Plugin\search_api\processor;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api_federated_solr\Plugin\search_api\processor\Property\FederatedTermsProperty;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\taxonomy\Entity\Term;
class FederatedTerms extends ProcessorPluginBase {
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];
if (!$datasource) {
$definition = [
'label' => $this
->t('Federated terms'),
'description' => $this
->t('Normalize multiple taxonomy terms into federated terms.'),
'type' => 'string',
'processor_id' => $this
->getPluginId(),
];
$properties['federated_terms'] = new FederatedTermsProperty($definition);
}
return $properties;
}
public function addFieldValues(ItemInterface $item) {
$federated_terms = $this
->getFieldsHelper()
->filterForPropertyPath($item
->getFields(), NULL, 'federated_terms');
$entity = $item
->getOriginalObject()
->getValue();
if (!$entity) {
return;
}
$federated_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 ($federated_terms as $federated_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']);
$field_definitions = $term_entity
->getFieldDefinitions();
$federated_term_definitions = array_filter($field_definitions, function ($field_definition) {
return $field_definition
->getType() === "federated_terms";
});
$federated_term_field_names = array_map(function ($federated_term_definition) {
return $federated_term_definition
->getName();
}, $federated_term_definitions);
foreach ($federated_term_field_names as $field_name) {
$federated_term_values = $term_entity->{$field_name}
->getValue();
if (!empty($federated_term_values)) {
foreach ($federated_term_values as $federated_term_value) {
$federated_terms_destination_values[] = $federated_term_value['value'];
}
}
}
}
}
$federated_terms_destination_values = array_unique($federated_terms_destination_values);
foreach ($federated_terms_destination_values as $value) {
$existing_values = $federated_term
->getValues();
if (!array_search($value, $existing_values)) {
$federated_term
->addValue($value);
}
}
}
}
}