-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMappedFields.php
85 lines (72 loc) · 2.8 KB
/
MappedFields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Drupal\search_api_field_map\Plugin\search_api\processor;
use Drupal\Core\Entity\EntityInterface;
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;
/**
* Normalize multiple content types into a single mapped field.
*
* @see \Drupal\search_api_field_map\Plugin\search_api\processor\Property\MappedFieldProperty
*
* @SearchApiProcessor(
* id = "mapped_field",
* label = @Translation("Mapped fields"),
* description = @Translation("Normalize multiple content types into a single mapped field."),
* stages = {
* "add_properties" = 20,
* },
* locked = true,
* hidden = true,
* )
*/
class MappedFields extends ProcessorPluginBase {
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function addFieldValues(ItemInterface $item) {
// Get all of the mapped fields on our item.
$mapped_fields = $this->getFieldsHelper()
->filterForPropertyPath($item->getFields(), NULL, 'mapped_field');
// Get the entity object, bail if there's somehow not one.
$entity = $item->getOriginalObject()->getValue();
if (!$entity || !$entity instanceof EntityInterface) {
// Apparently we were active for a wrong item.
return;
}
// Set some helper vars for the entity and bundle type.
$entity_type = $entity->getEntityTypeId();
$bundle_type = $entity->bundle();
// Process and set values for each mapped field on the item.
foreach ($mapped_fields as $mapped_field) {
// Get configuration for the field.
$configuration = $mapped_field->getConfiguration();
// If there's a config item for the entity and bundle type we're in, set the value for the field.
if(!empty($configuration['field_data'][$entity_type][$bundle_type])) {
$token = \Drupal::token();
// If the token replacement produces a value, add to this item.
if ($value = $token->replace($configuration['field_data'][$entity_type][$bundle_type], [$entity_type => $entity], ['clear' => true])) {
// Do not use setValues(), since that doesn't preprocess the values according to their data type.
$mapped_field->addValue($value);
}
}
}
}
}