From aa51685fa5c8fdba70b8235ff2deff85cff27af1 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 3 Oct 2024 15:00:17 -0300 Subject: [PATCH 1/3] Theoretically over to the event subscriber flow. --- dgi_image_discovery.services.yml | 4 ++ .../ModelDefaultFallbackSubscriber.php | 59 +++++++++++++++++++ .../processor/DgiImageDiscovery.php | 43 -------------- 3 files changed, 63 insertions(+), 43 deletions(-) create mode 100644 src/EventSubscriber/ModelDefaultFallbackSubscriber.php diff --git a/dgi_image_discovery.services.yml b/dgi_image_discovery.services.yml index d6d6b30..303ff4c 100644 --- a/dgi_image_discovery.services.yml +++ b/dgi_image_discovery.services.yml @@ -25,6 +25,10 @@ services: class: '\Drupal\dgi_image_discovery\EventSubscriber\DiscoverRepresentativeImageSubscriber' tags: - name: event_subscriber + dgi_image_discovery.model_fallback_subscriber: + class: '\Drupal\dgi_image_discovery\EventSubscriber\ModelDefaultFallbackSubscriber' + tags: + - name: event_subscriber dgi_image_discovery.deferred_resolution_controller: class: '\Drupal\dgi_image_discovery\Controller\DeferredResolutionController' factory: [null, 'create'] diff --git a/src/EventSubscriber/ModelDefaultFallbackSubscriber.php b/src/EventSubscriber/ModelDefaultFallbackSubscriber.php new file mode 100644 index 0000000..ac9262b --- /dev/null +++ b/src/EventSubscriber/ModelDefaultFallbackSubscriber.php @@ -0,0 +1,59 @@ +getEntity(); + if (!$entity instanceof NodeInterface) { + return; + } + if (!$entity->hasField('field_model')) { + return; + } + /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $term_item */ + foreach ($entity->get('field_model') as $term_item) { + /** @var \Drupal\taxonomy\TermInterface $term */ + $term = $term_item?->get('entity')?->getTarget(); + $term_access = $term?->access('view', NULL, TRUE); + if (!$term_access?->isAllowed()) { + continue; + } + if (!$term?->hasField('field_default_image')) { + continue; + } + /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $candidate_item */ + foreach ($term->get('field_default_image') as $candidate_item) { + /** @var \Drupal\media\MediaInterface $candidate */ + if (!$candidate = $candidate_item?->get('entity')?->getTarget()) { + continue; + } + $candidate_access = $candidate?->access('view', NULL, TRUE); + if (!$candidate_access->isAllowed()) { + continue; + } + + $event->addCacheableDependency($term_access) + ->addCacheableDependency($candidate_access) + ->addCacheableDependency($term) + ->addCacheableDependency($candidate) + ->setMedia($candidate) + ->stopPropagation(); + return; + } + } + } + +} diff --git a/src/Plugin/search_api/processor/DgiImageDiscovery.php b/src/Plugin/search_api/processor/DgiImageDiscovery.php index f412486..f56c8b6 100644 --- a/src/Plugin/search_api/processor/DgiImageDiscovery.php +++ b/src/Plugin/search_api/processor/DgiImageDiscovery.php @@ -135,51 +135,8 @@ public function addFieldValues(ItemInterface $item) { if ($generated_url) { $field->addValue($generated_url->getGeneratedUrl()); } - else { - // Fallback to default image if URL generation fails. - $default_image_url = $this->getDefaultImageFromTaxonomy($entity, $image_style); - if ($default_image_url) { - $field->addValue($default_image_url); - } - } } } } - /** - * Gets the default image URL from the taxonomy term. - * - * @param \Drupal\node\NodeInterface $node - * The node to get the default image from. - * @param \Drupal\image\ImageStyleInterface $image_style - * The image style to use. - * - * @return string|null - * The default image URL or null if not found. - */ - protected function getDefaultImageFromTaxonomy(NodeInterface $node, ImageStyleInterface $image_style) { - if (!$node->hasField('field_model')) { - return NULL; - } - - $model_terms = $node->get('field_model')->referencedEntities(); - - foreach ($model_terms as $term) { - if ($term instanceof Term) { - // Load the media entity referenced by the field_default_image. - $media = $term->get('field_default_image')->entity; - if ($media instanceof Media) { - // Load the file entity from the media entity. - $file = $media->get('field_media_image')->entity; - if ($file instanceof File) { - // Use the provided image style. - return $image_style->buildUrl($file->getFileUri()); - } - } - } - } - - return NULL; - } - } From 8d1a6c8443b89c468b907617e76691c04dd6ed6a Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Thu, 3 Oct 2024 15:06:12 -0300 Subject: [PATCH 2/3] Coding standards. --- src/Plugin/search_api/processor/DgiImageDiscovery.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Plugin/search_api/processor/DgiImageDiscovery.php b/src/Plugin/search_api/processor/DgiImageDiscovery.php index f56c8b6..ec837c4 100644 --- a/src/Plugin/search_api/processor/DgiImageDiscovery.php +++ b/src/Plugin/search_api/processor/DgiImageDiscovery.php @@ -7,14 +7,10 @@ use Drupal\dgi_image_discovery\ImageDiscoveryInterface; use Drupal\dgi_image_discovery\Plugin\search_api\processor\Property\DgiImageDiscoveryProperty; use Drupal\dgi_image_discovery\UrlGeneratorPluginManagerInterface; -use Drupal\file\Entity\File; -use Drupal\image\ImageStyleInterface; -use Drupal\media\Entity\Media; use Drupal\node\NodeInterface; use Drupal\search_api\Datasource\DatasourceInterface; use Drupal\search_api\Item\ItemInterface; use Drupal\search_api\Processor\ProcessorPluginBase; -use Drupal\taxonomy\Entity\Term; use Symfony\Component\DependencyInjection\ContainerInterface; /** From a34452842d544daf737814a2884acd8a8d863ae4 Mon Sep 17 00:00:00 2001 From: Morgan Dawe Date: Tue, 7 Jan 2025 15:54:32 -0400 Subject: [PATCH 3/3] DDST-922 : WIP (#34) * Refactor WIP. * Modifying field_default_image retrieval. * Removing development statements. * Coding standards. --- .../ModelDefaultFallbackSubscriber.php | 11 ++++++++--- src/Plugin/Field/FieldType/DIDImageItem.php | 2 +- src/Plugin/search_api/processor/DgiImageDiscovery.php | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/EventSubscriber/ModelDefaultFallbackSubscriber.php b/src/EventSubscriber/ModelDefaultFallbackSubscriber.php index ac9262b..e1b7a83 100644 --- a/src/EventSubscriber/ModelDefaultFallbackSubscriber.php +++ b/src/EventSubscriber/ModelDefaultFallbackSubscriber.php @@ -26,20 +26,25 @@ public function discoverImage(ImageDiscoveryEvent $event) : void { /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $term_item */ foreach ($entity->get('field_model') as $term_item) { /** @var \Drupal\taxonomy\TermInterface $term */ - $term = $term_item?->get('entity')?->getTarget(); + $term = $term_item?->get('entity')?->getTarget()?->getEntity(); $term_access = $term?->access('view', NULL, TRUE); + if (!$term_access?->isAllowed()) { continue; } + if (!$term?->hasField('field_default_image')) { continue; } + /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $candidate_item */ foreach ($term->get('field_default_image') as $candidate_item) { - /** @var \Drupal\media\MediaInterface $candidate */ - if (!$candidate = $candidate_item?->get('entity')?->getTarget()) { + + /** @var \Drupal\media\Entity\Media $candidate */ + if (!$candidate = $candidate_item?->get('entity')?->getTarget()?->getEntity()) { continue; } + $candidate_access = $candidate?->access('view', NULL, TRUE); if (!$candidate_access->isAllowed()) { continue; diff --git a/src/Plugin/Field/FieldType/DIDImageItem.php b/src/Plugin/Field/FieldType/DIDImageItem.php index 08c769b..15ea326 100644 --- a/src/Plugin/Field/FieldType/DIDImageItem.php +++ b/src/Plugin/Field/FieldType/DIDImageItem.php @@ -45,7 +45,7 @@ class DIDImageItem extends EntityReferenceItem implements RefinableCacheableDepe public function __construct( DataDefinitionInterface $definition, $name = NULL, - TypedDataInterface $parent = NULL, + ?TypedDataInterface $parent = NULL, ) { parent::__construct($definition, $name, $parent); diff --git a/src/Plugin/search_api/processor/DgiImageDiscovery.php b/src/Plugin/search_api/processor/DgiImageDiscovery.php index ec837c4..e9bd2b8 100644 --- a/src/Plugin/search_api/processor/DgiImageDiscovery.php +++ b/src/Plugin/search_api/processor/DgiImageDiscovery.php @@ -77,7 +77,7 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ - public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { + public function getPropertyDefinitions(?DatasourceInterface $datasource = NULL) { $properties = []; if (!$datasource) {