Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDST-602 : Fix/field safety #27

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

}
Loading