/
UmamiSearchCommonImageField.php
173 lines (154 loc) · 4.8 KB
/
UmamiSearchCommonImageField.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace Drupal\umami_site_search\Plugin\search_api\processor;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\media\Entity\Media;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Processor\ProcessorProperty;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Adds 'common_image_field' field.
*
* @SearchApiProcessor(
* id = "common_image_field",
* label = @Translation("Common image field"),
* description = @Translation("Common field for all content types."),
* stages = {
* "add_properties" = 0,
* },
* )
*/
class UmamiSearchCommonImageField extends ProcessorPluginBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var static $plugin */
$plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$plugin->setEntityTypeManager($container->get('entity_type.manager'));
return $plugin;
}
/**
* Sets entity type manager service.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*
* @return $this
*/
public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
return $this;
}
/**
* Retrieves the entity type manager service.
*
* @return \Drupal\Core\Entity\EntityTypeManagerInterface
* The entity type manager service.
*/
protected function getEntityTypeManager() {
return $this->entityTypeManager ?: \Drupal::service('entity_type.manager');
}
/**
* {@inheritdoc}
*/
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];
if (!$datasource) {
$definition = [
'label' => $this->t('Common image field'),
'description' => $this->t('Common field for all content types'),
'type' => 'string',
'processor_id' => $this->getPluginId(),
];
$properties['common_image_field'] = new ProcessorProperty($definition);
}
return $properties;
}
/**
* {@inheritdoc}
*/
public function addFieldValues(ItemInterface $item) : void {
$node = $item->getOriginalObject()->getValue();
$uri = '';
switch ($node->bundle()) {
case 'page':
// Skip Basic page contents as they are not included.
break;
case 'article':
case 'recipe':
// Image will be present in “field_media_image” for both Article and
// Recipe content types.
$media = $this->getMediaEntity($node, 'field_media_image');
if ($media instanceof Media) {
$uri = $this->getUrlFromMedia($media);
}
break;
case 'blog':
// Image will be present in paragraphs added in "field_banner" field in
// blog content type.
if ($node->hasField('field_banner') && !empty($node->get('field_banner')->referencedEntities())) {
$paragraph = $node->get('field_banner')->referencedEntities()[0];
// Get the media entity from banner paragraphs.
$media = $this->getMediaEntity($paragraph, 'field_media');
if ($media instanceof Media) {
$uri = $this->getUrlFromMedia($media);
}
break;
}
}
// Save the URL to the field.
if ($uri) {
$fields = $this->getFieldsHelper()
->filterForPropertyPath($item->getFields(), NULL, 'common_image_field');
foreach ($fields as $field) {
$field->addValue($uri);
}
}
}
/**
* Helper function to get the URI of of the media item.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
* @param string $field_name
* The media reference field name.
*
* @return \Drupal\media\Entity\Media|null
* The media entity.
*/
public function getMediaEntity(EntityInterface $entity, string $field_name) {
if ($entity->hasField($field_name)) {
return $entity->$field_name->entity;
}
return NULL;
}
/**
* Helper function to get the media URL from media entity.
*
* @param \Drupal\media\Entity\Media $media
* The media item.
*
* @return string
* URI of the media.
*/
public function getUrlFromMedia(Media $media) {
$url = '';
if (!empty($media)) {
switch ($media->bundle()) {
case 'image':
$url = $media->field_media_image->entity?->createFileUrl();
break;
}
return $url;
}
}
}