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) {
// 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);
}
}
}
}
......
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