| @@ 32-79 (lines=48) @@ | ||
| 29 | * deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver" |
|
| 30 | * ) |
|
| 31 | */ |
|
| 32 | class ImageDerivative extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
| 33 | ||
| 34 | use DependencySerializationTrait; |
|
| 35 | ||
| 36 | /** |
|
| 37 | * The image factory. |
|
| 38 | * |
|
| 39 | * @var \Drupal\Core\Image\ImageFactory |
|
| 40 | */ |
|
| 41 | protected $imageFactory; |
|
| 42 | ||
| 43 | /** |
|
| 44 | * {@inheritdoc} |
|
| 45 | */ |
|
| 46 | public function __construct(array $configuration, $pluginId, $pluginDefinition, ImageFactory $imageFactory) { |
|
| 47 | parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
| 48 | $this->imageFactory = $imageFactory; |
|
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * {@inheritdoc} |
|
| 53 | */ |
|
| 54 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
| 55 | return new static($configuration, $pluginId, $pluginDefinition, $container->get('image.factory')); |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * {@inheritdoc} |
|
| 60 | */ |
|
| 61 | protected function resolveValues($value, array $args, ResolveInfo $info) { |
|
| 62 | if ($value instanceof ImageItem && $value->entity->access('view') && $style = ImageStyle::load($args['style'])) { |
|
| 63 | $file = $value->entity; |
|
| 64 | // Determine the dimensions of the styled image. |
|
| 65 | $dimensions = [ |
|
| 66 | 'width' => $value->width, |
|
| 67 | 'height' => $value->height, |
|
| 68 | ]; |
|
| 69 | $style->transformDimensions($dimensions, $file->getFileUri()); |
|
| 70 | ||
| 71 | yield [ |
|
| 72 | 'url' => $style->buildUrl($file->getFileUri()), |
|
| 73 | 'width' => $dimensions['width'], |
|
| 74 | 'height' => $dimensions['height'], |
|
| 75 | ]; |
|
| 76 | } |
|
| 77 | } |
|
| 78 | ||
| 79 | } |
|
| 80 | ||
| @@ 31-78 (lines=48) @@ | ||
| 28 | * } |
|
| 29 | * ) |
|
| 30 | */ |
|
| 31 | class ImageDerivative extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
| 32 | ||
| 33 | use DependencySerializationTrait; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * The image factory. |
|
| 37 | * |
|
| 38 | * @var \Drupal\Core\Image\ImageFactory |
|
| 39 | */ |
|
| 40 | protected $imageFactory; |
|
| 41 | ||
| 42 | /** |
|
| 43 | * {@inheritdoc} |
|
| 44 | */ |
|
| 45 | public function __construct(array $configuration, $pluginId, $pluginDefinition, ImageFactory $imageFactory) { |
|
| 46 | parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
| 47 | $this->imageFactory = $imageFactory; |
|
| 48 | } |
|
| 49 | ||
| 50 | /** |
|
| 51 | * {@inheritdoc} |
|
| 52 | */ |
|
| 53 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
| 54 | return new static($configuration, $pluginId, $pluginDefinition, $container->get('image.factory')); |
|
| 55 | } |
|
| 56 | ||
| 57 | /** |
|
| 58 | * {@inheritdoc} |
|
| 59 | */ |
|
| 60 | protected function resolveValues($value, array $args, ResolveInfo $info) { |
|
| 61 | if ($value instanceof ImageItem && $value->entity->access('view') && $style = ImageStyle::load($args['style'])) { |
|
| 62 | $file = $value->entity; |
|
| 63 | // Determine the dimensions of the styled image. |
|
| 64 | $dimensions = [ |
|
| 65 | 'width' => $value->width, |
|
| 66 | 'height' => $value->height, |
|
| 67 | ]; |
|
| 68 | $style->transformDimensions($dimensions, $file->getFileUri()); |
|
| 69 | ||
| 70 | yield [ |
|
| 71 | 'url' => $style->buildUrl($file->getFileUri()), |
|
| 72 | 'width' => $dimensions['width'], |
|
| 73 | 'height' => $dimensions['height'], |
|
| 74 | ]; |
|
| 75 | } |
|
| 76 | } |
|
| 77 | ||
| 78 | } |
|
| 79 | ||