From 9874d11eb5b497aedf74af5f73ac6b3df8874ee7 Mon Sep 17 00:00:00 2001 From: Eric Bremner <ebremner@uwaterloo.ca> Date: Wed, 10 Nov 2021 14:14:55 +0000 Subject: [PATCH] ISTWCMS-5195: updating prepareResponsiveImage to ensure that entities are not null --- src/Service/UWService.php | 52 +++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/Service/UWService.php b/src/Service/UWService.php index 663a0025..de7600a2 100644 --- a/src/Service/UWService.php +++ b/src/Service/UWService.php @@ -81,31 +81,35 @@ class UWService implements UWServiceInterface { */ public function prepareResponsiveImage(EntityInterface $entity, string $image_style): array { - // Load in the file object if we have one. - if ($file = $entity->field_media_image->entity) { - - // Need to set these variables so that responsive image function, - // has all the necessary info to process the image style. - $variables['uri'] = $file->getFileUri(); - $variables['responsive_image_style_id'] = $image_style; - - // Set the alt for the image. - $variables['alt'] = $entity->field_media_image->alt; - - // These is a function from the responsive image module that sets all - // the variables for the sources of the responsive image. - template_preprocess_responsive_image($variables); - - // Step through each of the sources and setup our own sources array. - foreach ($variables['sources'] as $source) { - $variables['responsive_sources'][] = [ - 'srcset' => $source->storage()['srcset']->value(), - 'media' => $source->storage()['media']->value(), - 'type' => $source->storage()['type']->value(), - ]; - } + // Ensure that we can load an entity on the media. + if (isset($entity->field_media_image->entity)) { + + // Load in the file object if we have one. + if ($file = $entity->field_media_image->entity) { + + // Need to set these variables so that responsive image function, + // has all the necessary info to process the image style. + $variables['uri'] = $file->getFileUri(); + $variables['responsive_image_style_id'] = $image_style; + + // Set the alt for the image. + $variables['alt'] = $entity->field_media_image->alt; + + // These is a function from the responsive image module that sets all + // the variables for the sources of the responsive image. + template_preprocess_responsive_image($variables); + + // Step through each of the sources and setup our own sources array. + foreach ($variables['sources'] as $source) { + $variables['responsive_sources'][] = [ + 'srcset' => $source->storage()['srcset']->value(), + 'media' => $source->storage()['media']->value(), + 'type' => $source->storage()['type']->value(), + ]; + } - return $variables; + return $variables; + } } return []; -- GitLab