From b33513a397db4c624efdbd8ef97db9599d8717f8 Mon Sep 17 00:00:00 2001 From: Eric Bremner <ebremner@uwaterloo.ca> Date: Wed, 1 Dec 2021 13:19:43 +0000 Subject: [PATCH] ISTWCMS-5206: fixing error when there is no image on the entity pre save --- uw_cfg_common.module | 52 ++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/uw_cfg_common.module b/uw_cfg_common.module index 3001295f..2041219d 100644 --- a/uw_cfg_common.module +++ b/uw_cfg_common.module @@ -113,30 +113,34 @@ function uw_cfg_common_entity_presave(EntityInterface $entity) { // Get the value of the image field. $image = $entity->field_uw_hero_image->getValue(); - // Load the file from the hero image. - $file = \Drupal::entityTypeManager() - ->getStorage('file') - ->load($image[0]['target_id']); - - // Load the image styles that are needed for the hero. - $uw_styles = \Drupal::service('uw_cfg_common.uw_service')->uwGetResponsiveImageStyles(); - - // Step through each of the image styles and ensure that - // the derivative is created. - foreach ($uw_styles as $uw_style) { - - // Load the image style. - $style = \Drupal::entityTypeManager() - ->getStorage('image_style') - ->load($uw_style); - - // Get the styled image derivative. - $destination = $style->buildUri($file->getFileUri()); - - // 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); + // Ensure that there actually is an image to process. + if ($image) { + + // Load the file from the hero image. + $file = \Drupal::entityTypeManager() + ->getStorage('file') + ->load($image[0]['target_id']); + + // Load the image styles that are needed for the hero. + $uw_styles = \Drupal::service('uw_cfg_common.uw_service')->uwGetResponsiveImageStyles(); + + // Step through each of the image styles and ensure that + // the derivative is created. + foreach ($uw_styles as $uw_style) { + + // Load the image style. + $style = \Drupal::entityTypeManager() + ->getStorage('image_style') + ->load($uw_style); + + // Get the styled image derivative. + $destination = $style->buildUri($file->getFileUri()); + + // 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); + } } } } -- GitLab