Skip to content

Commit

Permalink
Merge pull request #27 from discoverygarden/fix/field-safety
Browse files Browse the repository at this point in the history
DDST-602 : Fix/field safety
  • Loading branch information
MorganDawe authored Sep 23, 2024
2 parents 9ccac04 + df9bfdd commit f5dab0a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Plugin/search_api/processor/DgiImageDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -150,14 +151,17 @@ 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;
}

$model_terms = $node->get('field_model')->referencedEntities();

foreach ($model_terms as $term) {
Expand All @@ -169,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;
}

}

0 comments on commit f5dab0a

Please sign in to comment.