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 index 1e07538..8902352 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php @@ -25,23 +25,31 @@ */ class ImageUrlFormatter extends ImageFormatter { + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + return [ + 'image_style' => '', + ]; + } /** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { $image_styles = image_style_options(FALSE); - $element['image_style'] = array( + $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' => array( + '#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; } @@ -49,8 +57,28 @@ public function settingsForm(array $form, FormStateInterface $form_state) { /** * {@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 = array(); + $elements = []; $images = $this->getEntitiesToView($items); // Early opt-out if the field is empty. @@ -64,12 +92,15 @@ public function viewElements(FieldItemListInterface $items) { // 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) @@ -77,12 +108,12 @@ public function viewElements(FieldItemListInterface $items) { $cache_tags = Cache::mergeTags($cache_tags, $image->getCacheTags()); - $elements[$delta] = array( + $elements[$delta] = [ '#markup' => $url, - '#cache' => array( + '#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']); } /**