ImageStyleRenderer.php in UIkit Components 8.3
File
src/ImageStyleRenderer.php
View source
<?php
namespace Drupal\uikit_components;
use Drupal\file\Entity\File;
class ImageStyleRenderer {
public static function loadImageManagedFile($build) {
$entity_manager = \Drupal::entityTypeManager()
->getStorage('file');
$query = $entity_manager
->getQuery();
$query
->condition('uri', $build['uri']);
$fids = $query
->execute();
if (count($fids) && ($file = File::load(reset($fids)))) {
$variables = array(
'style_name' => $build['style_name'],
'uri' => $file
->getFileUri(),
);
$image = \Drupal::service('image.factory')
->get($file
->getFileUri());
if ($image
->isValid()) {
$variables['width'] = $image
->getWidth();
$variables['height'] = $image
->getHeight();
}
else {
$variables['width'] = $variables['height'] = NULL;
}
$build = [
'#theme' => 'image_style',
'#width' => $variables['width'],
'#height' => $variables['height'],
'#style_name' => $variables['style_name'],
'#uri' => $variables['uri'],
];
$renderer = \Drupal::service('renderer');
$renderer
->addCacheableDependency($build, $file);
return $build;
}
else {
return [];
}
}
public static function loadImageFile($build) {
return [
'#theme' => 'image_style',
'#style_name' => $build['style_name'],
'#uri' => $build['uri'],
];
}
}