Skip to content

Commit

Permalink
DDST-416: Add fall-back logic for default image
Browse files Browse the repository at this point in the history
  • Loading branch information
prashant-dgi authored Aug 15, 2024
1 parent aa595a9 commit 80d608d
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Plugin/search_api/processor/DgiImageDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit 80d608d

Please sign in to comment.