Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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 [];
}
}