MappedFields.php in Search API Field Map 8
File
src/Plugin/search_api/processor/MappedFields.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\MappedFieldProperty;
use Drupal\search_api\Processor\ProcessorPluginBase;
class MappedFields extends ProcessorPluginBase {
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];
if (!$datasource) {
$definition = [
'label' => $this
->t('Mapped field'),
'description' => $this
->t('Normalize multiple content types into a single mapped field.'),
'type' => 'string',
'processor_id' => $this
->getPluginId(),
];
$properties['mapped_field'] = new MappedFieldProperty($definition);
}
return $properties;
}
public function addFieldValues(ItemInterface $item) {
$mapped_fields = $this
->getFieldsHelper()
->filterForPropertyPath($item
->getFields(), NULL, 'mapped_field');
$entity = $item
->getOriginalObject()
->getValue();
if (!$entity) {
return;
}
$entity_type = $entity
->getEntityTypeId();
$bundle_type = $entity
->bundle();
foreach ($mapped_fields as $mapped_field) {
$configuration = $mapped_field
->getConfiguration();
if (!empty($configuration['field_data'][$entity_type][$bundle_type])) {
$token = \Drupal::token();
if ($value = $token
->replace($configuration['field_data'][$entity_type][$bundle_type], [
$entity_type => $entity,
], [
'clear' => true,
])) {
$mapped_field
->addValue($value);
}
}
}
}
}
Classes
Name |
Description |
MappedFields |
Normalize multiple content types into a single mapped field. |