You are here

public static function ImageStyleRenderer::loadImageManagedFile in UIkit Components 8.3

@inheritdoc

1 call to ImageStyleRenderer::loadImageManagedFile()
UIkitComment::preRenderUIkitComment in src/Element/UIkitComment.php
Pre-render callback: Sets the comment attributes.

File

src/ImageStyleRenderer.php, line 18

Class

ImageStyleRenderer
Builds render arrays for image styles.

Namespace

Drupal\uikit_components

Code

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(),
    );

    // The image.factory service will check if our image is valid.
    $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'],
    ];

    // Add the file entity to the cache dependencies.
    // This will clear our cache when this entity updates.
    $renderer = \Drupal::service('renderer');
    $renderer
      ->addCacheableDependency($build, $file);

    // Return the render array.
    return $build;
  }
  else {

    // Image not found, return empty array.
    return [];
  }
}