diff --git a/src/Plugin/search_api/processor/DgiImageDiscovery.php b/src/Plugin/search_api/processor/DgiImageDiscovery.php index ec837c4..1498dc0 100644 --- a/src/Plugin/search_api/processor/DgiImageDiscovery.php +++ b/src/Plugin/search_api/processor/DgiImageDiscovery.php @@ -131,8 +131,51 @@ 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_name); + 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 string $image_style_name + * 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; + $model_terms = $node->get('field_model')->referencedEntities(); + + foreach ($model_terms as $term) { + if ($term instanceof \Drupal\taxonomy\Entity\Term) { + // Load the media entity referenced by the field_defaultimage. + $media = $term->get('field_defaultimage')->entity; + if ($media instanceof \Drupal\media\Entity\Media) { + // Load the file entity from the media entity. + $file = $media->get('field_media_image')->entity; + if ($file instanceof \Drupal\file\Entity\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 $default_image_url; + } + }