Skip to content

Commit

Permalink
Avoid unnecessarily reloading of the image style entity.
Browse files Browse the repository at this point in the history
Also, simplify return logic, with an early return.
  • Loading branch information
adam-vessey committed Sep 20, 2024
1 parent e93592b commit df9bfdd
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 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,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;
}
Expand All @@ -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;
}

}

0 comments on commit df9bfdd

Please sign in to comment.