You are here

public function ThunderImage::resolve in Thunder 6.2.x

Resolver.

Parameters

\Drupal\file\FileInterface $entity: The file entity.

array $field: Values of the field.

\Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata: The cacheable dependency interface.

Return value

array The image meta data

File

modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderImage.php, line 106

Class

ThunderImage
Returns image meta data.

Namespace

Drupal\thunder_gqls\Plugin\GraphQL\DataProducer

Code

public function resolve(FileInterface $entity, array $field, RefinableCacheableDependencyInterface $metadata) {
  $access = $entity
    ->access('view', NULL, TRUE);
  $metadata
    ->addCacheableDependency($access);
  if ($access
    ->isAllowed()) {
    $context = new RenderContext();
    $imageFactory = $this->imageFactory;
    $data = $this->renderer
      ->executeInRenderContext($context, function () use ($entity, $imageFactory, $field) {
      $uri = $entity
        ->getFileUri();
      $image = $imageFactory
        ->get($uri);
      $current_field = reset($field);
      return [
        'src' => file_create_url($uri),
        'width' => $image
          ->getWidth(),
        'height' => $image
          ->getHeight(),
        'alt' => $current_field['alt'],
        'title' => $current_field['title'],
      ];
    });
    if (!$context
      ->isEmpty()) {
      $metadata
        ->addCacheableDependency($context
        ->pop());
    }
    return $data;
  }
  return [];
}