From e93592b98f097e640fc402bda0ba8765c3b8d555 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 20 Sep 2024 15:52:55 -0300 Subject: [PATCH 1/2] Avoid attempting to use potentially non-existent field. --- src/Plugin/search_api/processor/DgiImageDiscovery.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Plugin/search_api/processor/DgiImageDiscovery.php b/src/Plugin/search_api/processor/DgiImageDiscovery.php index 327063e..23170b3 100644 --- a/src/Plugin/search_api/processor/DgiImageDiscovery.php +++ b/src/Plugin/search_api/processor/DgiImageDiscovery.php @@ -158,6 +158,11 @@ public function addFieldValues(ItemInterface $item) { */ protected function getDefaultImageFromTaxonomy(NodeInterface $node, string $image_style_name) { $default_image_url = NULL; + + if (!$node->hasField('field_model')) { + return NULL; + } + $model_terms = $node->get('field_model')->referencedEntities(); foreach ($model_terms as $term) { From df9bfddec5bc23d9e31576f9c83131b95d3dac6b Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Fri, 20 Sep 2024 15:56:11 -0300 Subject: [PATCH 2/2] Avoid unnecessarily reloading of the image style entity. Also, simplify return logic, with an early return. --- .../search_api/processor/DgiImageDiscovery.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Plugin/search_api/processor/DgiImageDiscovery.php b/src/Plugin/search_api/processor/DgiImageDiscovery.php index 23170b3..f412486 100644 --- a/src/Plugin/search_api/processor/DgiImageDiscovery.php +++ b/src/Plugin/search_api/processor/DgiImageDiscovery.php @@ -8,6 +8,7 @@ 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; @@ -136,7 +137,7 @@ public function addFieldValues(ItemInterface $item) { } else { // Fallback to default image if URL generation fails. - $default_image_url = $this->getDefaultImageFromTaxonomy($entity, $image_style->getName()); + $default_image_url = $this->getDefaultImageFromTaxonomy($entity, $image_style); if ($default_image_url) { $field->addValue($default_image_url); } @@ -150,15 +151,13 @@ public function addFieldValues(ItemInterface $item) { * * @param \Drupal\node\NodeInterface $node * The node to get the default image from. - * @param string $image_style_name + * @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, string $image_style_name) { - $default_image_url = NULL; - + protected function getDefaultImageFromTaxonomy(NodeInterface $node, ImageStyleInterface $image_style) { if (!$node->hasField('field_model')) { return NULL; } @@ -174,16 +173,13 @@ protected function getDefaultImageFromTaxonomy(NodeInterface $node, string $imag $file = $media->get('field_media_image')->entity; if ($file instanceof File) { // Use the provided image style. - $default_image_url = $this->entityTypeManager->getStorage('image_style')->load($image_style_name) - ->buildUrl($file->getFileUri()); - // Return the first default image found, if applicable. - break; + return $image_style->buildUrl($file->getFileUri()); } } } } - return $default_image_url; + return NULL; } }