diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml index 4949fa3..110b755 100644 --- a/core/modules/image/config/schema/image.schema.yml +++ b/core/modules/image/config/schema/image.schema.yml @@ -131,6 +131,14 @@ field.formatter.settings.image: type: string label: 'Image style' +field.formatter.settings.image_url: + type: mapping + label: 'Image URL format settings' + mapping: + image_style: + type: string + label: 'Image style' + field.widget.settings.image_image: type: mapping label: 'Image field display format settings' diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php new file mode 100644 index 0000000..8902352 --- /dev/null +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php @@ -0,0 +1,121 @@ + '', + ]; + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $image_styles = image_style_options(FALSE); + $element['image_style'] = [ + '#title' => t('Image style'), + '#type' => 'select', + '#default_value' => $this->getSetting('image_style'), + '#empty_option' => t('None (original image)'), + '#options' => $image_styles, + '#description' => [ + '#markup' => $this->linkGenerator->generate($this->t('Configure Image Styles'), new Url('entity.image_style.collection')), + '#access' => $this->currentUser->hasPermission('administer image styles'), + ], + ]; + + return $element; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = []; + + $image_styles = image_style_options(FALSE); + // Unset possible 'No defined styles' option. + unset($image_styles['']); + // Styles could be lost because of enabled/disabled modules that defines + // their styles in code. + $image_style_setting = $this->getSetting('image_style'); + if (isset($image_styles[$image_style_setting])) { + $summary[] = t('Image style: @style', ['@style' => $image_styles[$image_style_setting]]); + } + else { + $summary[] = t('Original image'); + } + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items) { + $elements = []; + $images = $this->getEntitiesToView($items); + + // Early opt-out if the field is empty. + if (empty($images)) { + return $elements; + } + + $url = NULL; + + $image_style_setting = $this->getSetting('image_style'); + + // Collect cache tags to be added for each item in the field. + $cache_tags = []; + /** @var \Drupal\image\Entity\ImageStyle|false $image_style */ + $image_style = FALSE; + if (!empty($image_style_setting)) { + $image_style = $this->imageStyleStorage->load($image_style_setting); + $cache_tags = $image_style->getCacheTags(); + } + + foreach ($images as $delta => $image) { + /** @var \Drupal\file\Entity\File $image */ + $image_uri = $image->getFileUri(); + $url = $image_style + ? $image_style->buildUrl($image_uri) + : file_create_url($image_uri); + + $cache_tags = Cache::mergeTags($cache_tags, $image->getCacheTags()); + + $elements[$delta] = [ + '#markup' => $url, + '#cache' => [ + 'tags' => $cache_tags, + ], + ]; + } + + return $elements; + } +} diff --git a/core/modules/image/src/Tests/ImageFieldDisplayTest.php b/core/modules/image/src/Tests/ImageFieldDisplayTest.php index 1e1d798..d539771 100644 --- a/core/modules/image/src/Tests/ImageFieldDisplayTest.php +++ b/core/modules/image/src/Tests/ImageFieldDisplayTest.php @@ -202,6 +202,26 @@ function _testImageFieldFormatters($scheme) { $this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri)); $this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.'); } + + // Test the image URL formatter without an image style. + $display_options = [ + 'type' => 'image_url', + 'settings' => [], + ]; + + $expected_url = file_create_url($image_uri); + $this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']); + + // Test the image URL formatter with an image style. + $display_options = [ + 'type' => 'image_url', + 'settings' => [ + 'image_style' => 'thumbnail', + ], + ]; + + $expected_url = ImageStyle::load('thumbnail')->buildUrl($image_uri); + $this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']); } /**