Skip to content

Commit 2283ab0

Browse files
committed
Attempt at re-doing DI and creating a schema.
1 parent f6ee055 commit 2283ab0

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/Plugin/search_api/processor/EntityReferenceWithUri.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Drupal\islandora\Plugin\search_api\processor;
44

5+
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
6+
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
7+
use Drupal\Core\Entity\EntityTypeManager;
58
use Drupal\islandora\Plugin\search_api\processor\Property\EntityReferenceWithUriProperty;
69
use Drupal\search_api\Datasource\DatasourceInterface;
710
use Drupal\search_api\Item\ItemInterface;
@@ -32,6 +35,13 @@ class EntityReferenceWithUri extends ProcessorPluginBase {
3235
*/
3336
protected IslandoraUtils $utils;
3437

38+
/**
39+
* Entity Type Manager.
40+
*
41+
* @var \Drupal\Core\Entity\EntityTypeManager
42+
*/
43+
protected EntityTypeManager $entityTypeManager;
44+
3545
/**
3646
* Constructor.
3747
*
@@ -46,15 +56,19 @@ class EntityReferenceWithUri extends ProcessorPluginBase {
4656
* The plugin implementation definition.
4757
* @param \Drupal\islandora\IslandoraUtils $utils
4858
* Islandora utils.
59+
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
60+
* Drupal Entity Type Manager.
4961
*/
5062
public function __construct(
5163
array $configuration,
5264
$plugin_id,
5365
$plugin_definition,
54-
IslandoraUtils $utils
66+
IslandoraUtils $utils,
67+
EntityTypeManager $entityTypeManager
5568
) {
5669
parent::__construct($configuration, $plugin_id, $plugin_definition);
5770
$this->utils = $utils;
71+
$this->entityTypeManager = $entityTypeManager;
5872
}
5973

6074
/**
@@ -66,6 +80,7 @@ public static function create(ContainerInterface $container, array $configuratio
6680
$plugin_id,
6781
$plugin_definition,
6882
$container->get('islandora.utils'),
83+
$container->get('entity_type.manager'),
6984
);
7085
}
7186

@@ -81,7 +96,7 @@ public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
8196
$entity_type = $datasource->getEntityTypeId();
8297

8398
// Get all configured Entity Relation fields for this entity type.
84-
$fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties([
99+
$fields = $this->entityTypeManager->getStorage('field_config')->loadByProperties([
85100
'entity_type' => $entity_type,
86101
'field_type' => 'entity_reference',
87102
]);
@@ -97,7 +112,7 @@ public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
97112
'@label' => $field->label(),
98113
'@bundle' => $field->getTargetBundle(),
99114
]),
100-
'description' => $this->t('Index the target entity, but only if the target entity has a taxonomy term.'),
115+
'description' => $this->t('Index the related entity, but only if the target entity has a taxonomy term.'),
101116
'type' => 'string',
102117
'processor_id' => $this->getPluginId(),
103118
'settings' => [
@@ -114,6 +129,7 @@ public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
114129

115130
/**
116131
* {@inheritdoc}
132+
* @throws \Drupal\search_api\SearchApiException
117133
*/
118134
public function addFieldValues(ItemInterface $item) {
119135
// Skip if no Entity Reference with URI fields are configured.
@@ -144,9 +160,13 @@ public function addFieldValues(ItemInterface $item) {
144160
foreach ($content_entity->get($field_name)->getValue() as $values) {
145161
foreach ($values as $value) {
146162
// Load the entity stored in our field.
147-
$target_entity = \Drupal::entityTypeManager()
148-
->getStorage($entity_type)
149-
->load($value);
163+
try {
164+
$target_entity = $this->entityTypeManager
165+
->getStorage($entity_type)
166+
->load($value);
167+
} catch (InvalidPluginDefinitionException $e) {
168+
} catch (PluginNotFoundException $e) {
169+
}
150170
// Load the taxonomy terms on the entity stored in our field.
151171
$referenced_terms = array_merge($referenced_terms, array_filter($target_entity->referencedEntities(), function ($entity) {
152172
if ($entity->getEntityTypeId() != 'taxonomy_term') {

0 commit comments

Comments
 (0)