Skip to content
Snippets Groups Projects
Commit b33513a3 authored by Eric Bremner's avatar Eric Bremner
Browse files

ISTWCMS-5206: fixing error when there is no image on the entity pre save

parent 5a14e12c
No related branches found
No related tags found
No related merge requests found
...@@ -113,30 +113,34 @@ function uw_cfg_common_entity_presave(EntityInterface $entity) { ...@@ -113,30 +113,34 @@ function uw_cfg_common_entity_presave(EntityInterface $entity) {
// Get the value of the image field. // Get the value of the image field.
$image = $entity->field_uw_hero_image->getValue(); $image = $entity->field_uw_hero_image->getValue();
// Load the file from the hero image. // Ensure that there actually is an image to process.
$file = \Drupal::entityTypeManager() if ($image) {
->getStorage('file')
->load($image[0]['target_id']); // Load the file from the hero image.
$file = \Drupal::entityTypeManager()
// Load the image styles that are needed for the hero. ->getStorage('file')
$uw_styles = \Drupal::service('uw_cfg_common.uw_service')->uwGetResponsiveImageStyles(); ->load($image[0]['target_id']);
// Step through each of the image styles and ensure that // Load the image styles that are needed for the hero.
// the derivative is created. $uw_styles = \Drupal::service('uw_cfg_common.uw_service')->uwGetResponsiveImageStyles();
foreach ($uw_styles as $uw_style) {
// Step through each of the image styles and ensure that
// Load the image style. // the derivative is created.
$style = \Drupal::entityTypeManager() foreach ($uw_styles as $uw_style) {
->getStorage('image_style')
->load($uw_style); // Load the image style.
$style = \Drupal::entityTypeManager()
// Get the styled image derivative. ->getStorage('image_style')
$destination = $style->buildUri($file->getFileUri()); ->load($uw_style);
// If the derivative doesn't exist yet (as the image style may have been // Get the styled image derivative.
// added post launch), create it. $destination = $style->buildUri($file->getFileUri());
if (!file_exists($destination)) {
$style->createDerivative($file->getFileUri(), $destination); // If the derivative doesn't exist yet (as the image style may have been
// added post launch), create it.
if (!file_exists($destination)) {
$style->createDerivative($file->getFileUri(), $destination);
}
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment