Skip to content

Commit

Permalink
Attempt at re-doing DI and creating a schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
rosiel committed Feb 22, 2023
1 parent f6ee055 commit 2283ab0
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/Plugin/search_api/processor/EntityReferenceWithUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Drupal\islandora\Plugin\search_api\processor;

use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\islandora\Plugin\search_api\processor\Property\EntityReferenceWithUriProperty;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
Expand Down Expand Up @@ -32,6 +35,13 @@ class EntityReferenceWithUri extends ProcessorPluginBase {
*/
protected IslandoraUtils $utils;

/**
* Entity Type Manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected EntityTypeManager $entityTypeManager;

/**
* Constructor.
*
Expand All @@ -46,15 +56,19 @@ class EntityReferenceWithUri extends ProcessorPluginBase {
* The plugin implementation definition.
* @param \Drupal\islandora\IslandoraUtils $utils
* Islandora utils.
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
* Drupal Entity Type Manager.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
IslandoraUtils $utils
IslandoraUtils $utils,
EntityTypeManager $entityTypeManager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->utils = $utils;
$this->entityTypeManager = $entityTypeManager;
}

/**
Expand All @@ -66,6 +80,7 @@ public static function create(ContainerInterface $container, array $configuratio
$plugin_id,
$plugin_definition,
$container->get('islandora.utils'),
$container->get('entity_type.manager'),
);
}

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

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

/**
* {@inheritdoc}
* @throws \Drupal\search_api\SearchApiException
*/
public function addFieldValues(ItemInterface $item) {
// Skip if no Entity Reference with URI fields are configured.
Expand Down Expand Up @@ -144,9 +160,13 @@ public function addFieldValues(ItemInterface $item) {
foreach ($content_entity->get($field_name)->getValue() as $values) {
foreach ($values as $value) {
// Load the entity stored in our field.
$target_entity = \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($value);
try {
$target_entity = $this->entityTypeManager
->getStorage($entity_type)
->load($value);
} catch (InvalidPluginDefinitionException $e) {
} catch (PluginNotFoundException $e) {
}
// Load the taxonomy terms on the entity stored in our field.
$referenced_terms = array_merge($referenced_terms, array_filter($target_entity->referencedEntities(), function ($entity) {
if ($entity->getEntityTypeId() != 'taxonomy_term') {
Expand Down

0 comments on commit 2283ab0

Please sign in to comment.