Skip to content
Snippets Groups Projects
Commit 88a5dd8e authored by Igor Biki's avatar Igor Biki
Browse files

ISTWCMS-4027 Adding UW Service, and using it on Blog teaser.

parent 8a9f0e56
No related branches found
No related tags found
No related merge requests found
<?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 [];
}
}
<?php
namespace Drupal\uw_cfg_common\Service;
use Drupal\Core\Entity\EntityInterface;
interface UWServiceInterface {
/**
* Prepares responsive image.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Image entity.
* @param string $image_style
* Image style to be used for responsive image.
*
* @return array
* Array with details for responsive image.
*/
public function prepareResponsiveImage(EntityInterface $entity, string $image_style): array;
}
services:
uw_cfg_common.uw_service:
class: Drupal\uw_cfg_common\Service\UWService
arguments: []
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