<?php namespace Drupal\uw_cfg_common\Service; use Drupal\Core\Entity\EntityInterface; /** * Class UWService * * @package Drupal\uw_cfg_common\Service */ class UWService implements UWServiceInterface { /** * {@inheritDoc} */ 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; // 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 []; } }