.../src/Plugin/DataType/ComputedImageStyleList.php | 92 ++++++++++++++++++++++ .../image/src/Plugin/DataType/ImageStyle.php | 54 +++++++++++++ .../image/src/Plugin/Field/FieldType/ImageItem.php | 7 ++ .../image/tests/src/Kernel/ImageItemTest.php | 2 +- .../EntityResource/Media/MediaResourceTestBase.php | 19 +++++ .../src/Normalizer/ListNormalizer.php | 4 +- 6 files changed, 175 insertions(+), 3 deletions(-) diff --git a/core/modules/image/src/Plugin/DataType/ComputedImageStyleList.php b/core/modules/image/src/Plugin/DataType/ComputedImageStyleList.php new file mode 100644 index 0000000..17f7647 --- /dev/null +++ b/core/modules/image/src/Plugin/DataType/ComputedImageStyleList.php @@ -0,0 +1,92 @@ +getParent(); + + /** @var \Drupal\file\FileInterface $file */ + $file = $image_item->entity; + $width = $image_item->width; + $height = $image_item->height; + + foreach (ImageStyle::loadMultiple() as $style) { + $this->list[$style->getName()] = $this->createItem($style->getName(), $this->computeImageStyleMetadata($file, $width, $height, $style)); + } + } + + /** + * @param \Drupal\file\FileInterface $file + * @param $width + * @param $height + * @param \Drupal\image\ImageStyleInterface $style + * @return array|bool + * + * @todo rather than returning an array, return a value object that implements CacheableDependencyInterface + * + * @see \Drupal\image\Entity\ImageStyle::buildUrl + * -> tag: config:image.settings + * + * -> tag: $style->getCacheTags() + */ + protected function computeImageStyleMetadata(FileInterface $file, $width, $height, ImageStyleInterface $style) { + $file_uri = $file->getFileUri(); + + if (!$style->supportsUri($file_uri)) { + return NULL; + } + + $dimensions = [ + 'width' => $width, + 'height' => $height, + ]; + $style->transformDimensions($dimensions, $file_uri); + + return [ + 'url' => file_url_transform_relative($style->buildUrl($file_uri)), + 'width' => $dimensions['width'], + 'height' => $dimensions['height'], + ]; + } + + /** + * {@inheritdoc} + */ + public function get($index) { + $this->computedListProperty(); + return isset($this->list[$index]) ? $this->list[$index] : NULL; + } + + /** + * {@inheritdoc} + */ + public function getIterator() { + $this->computedListProperty(); + return parent::getIterator(); + } + + /** + * {@inheritdoc} + */ + public function getValue() { + $this->computedListProperty(); + return parent::getValue(); + } + +} diff --git a/core/modules/image/src/Plugin/DataType/ImageStyle.php b/core/modules/image/src/Plugin/DataType/ImageStyle.php new file mode 100644 index 0000000..88fd180 --- /dev/null +++ b/core/modules/image/src/Plugin/DataType/ImageStyle.php @@ -0,0 +1,54 @@ +url = $value['url']; + $this->width = $value['width']; + $this->height = $value['height']; + // Notify the parent of any changes. + if ($notify && isset($this->parent)) { + $this->parent->onChange($this->name); + } + } + + /** + * {@inheritdoc} + */ + public function getValue() { + return [ + 'url' => $this->url, + 'width' => $this->width, + 'height' => $this->height, + ]; + } + +} diff --git a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php index 9e58ccd..d516a5a 100644 --- a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php +++ b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php @@ -9,6 +9,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\StreamWrapper\StreamWrapperInterface; use Drupal\Core\TypedData\DataDefinition; +use Drupal\Core\TypedData\ListDataDefinition; use Drupal\file\Entity\File; use Drupal\file\Plugin\Field\FieldType\FileItem; @@ -161,6 +162,12 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel ->setLabel(t('Height')) ->setDescription(t('The height of the image in pixels.')); + $properties['image_styles'] = ListDataDefinition::create('image_styles') + ->setLabel(t('Image style metadata')) + ->setDescription(t('The URL, width and height for this image in every available image style.')) + ->setComputed(TRUE) + ->setInternal(FALSE); + return $properties; } diff --git a/core/modules/image/tests/src/Kernel/ImageItemTest.php b/core/modules/image/tests/src/Kernel/ImageItemTest.php index 951d190..c889537 100644 --- a/core/modules/image/tests/src/Kernel/ImageItemTest.php +++ b/core/modules/image/tests/src/Kernel/ImageItemTest.php @@ -119,7 +119,7 @@ public function testImageItem() { $entity->save(); // Test image item properties. - $expected = ['target_id', 'entity', 'alt', 'title', 'width', 'height']; + $expected = ['target_id', 'entity', 'alt', 'title', 'width', 'height', 'image_styles']; $properties = $entity->getFieldDefinition('image_test')->getFieldStorageDefinition()->getPropertyDefinitions(); $this->assertEqual(array_keys($properties), $expected); diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php index 475a1ca..77d3560 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php @@ -104,6 +104,8 @@ protected function createEntity() { ->setRevisionUserId(static::$auth ? $this->account->id() : 0) ->save(); + $this->config('image.settings')->set('suppress_itok_output', TRUE)->save(TRUE); + return $media; } @@ -167,6 +169,23 @@ protected function getExpectedNormalizedEntity() { 'target_uuid' => $thumbnail->uuid(), 'title' => 'Llama', 'url' => $thumbnail->url(), + 'image_styles' => [ + 'large' => [ + 'url' => file_url_transform_relative(file_create_url('public://styles/large/public/media-icons/generic/generic.png')), + 'height' => '180', + 'width' => '180', + ], + 'medium' => [ + 'url' => file_url_transform_relative(file_create_url('public://styles/medium/public/media-icons/generic/generic.png')), + 'height' => '180', + 'width' => '180', + ], + 'thumbnail' => [ + 'url' => file_url_transform_relative(file_create_url('public://styles/thumbnail/public/media-icons/generic/generic.png')), + 'height' => '100', + 'width' => '100', + ], + ], ], ], 'status' => [ diff --git a/core/modules/serialization/src/Normalizer/ListNormalizer.php b/core/modules/serialization/src/Normalizer/ListNormalizer.php index 471886e..b486098 100644 --- a/core/modules/serialization/src/Normalizer/ListNormalizer.php +++ b/core/modules/serialization/src/Normalizer/ListNormalizer.php @@ -25,8 +25,8 @@ class ListNormalizer extends NormalizerBase { */ public function normalize($object, $format = NULL, array $context = []) { $attributes = []; - foreach ($object as $fieldItem) { - $attributes[] = $this->serializer->normalize($fieldItem, $format, $context); + foreach ($object as $key => $value) { + $attributes[$key] = $this->serializer->normalize($value, $format, $context); } return $attributes; }