diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php index ce6d331..b75cb46 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php @@ -7,6 +7,7 @@ namespace Drupal\image\Plugin\Field\FieldFormatter; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldDefinitionInterface; @@ -14,6 +15,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; +use Drupal\image\Entity\ImageStyle; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Cache\Cache; @@ -117,12 +119,16 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#access' => $this->currentUser->hasPermission('administer image styles') ], ]; - $link_types = array( + $link_types = [ 'content' => t('Content'), 'file' => t('Original image'), - ); + ]; + $options_group = (string) $this->t('Image style'); foreach ($image_styles as $name => $style) { - $link_types[$name] = t('!style image style', array('!style' => $style)); + if (!isset($link_types[$options_group])) { + $link_types[$options_group] = []; + } + $link_types[$options_group]["image_style.$name"] = $style; } $element['image_link'] = array( '#title' => t('Link image to'), @@ -154,19 +160,19 @@ public function settingsSummary() { $summary[] = t('Original image'); } - $link_types = array( - 'content' => t('Linked to content'), - 'file' => t('Linked to source file'), - ); - foreach ($image_styles as $name => $style) { - $link_types[$name] = t('Linked to !style image', array('!style' => $style)); - } - // Display this setting only if image is linked. - $image_link_setting = $this->getSetting('image_link'); - if (isset($link_types[$image_link_setting])) { - $summary[] = $link_types[$image_link_setting]; + if ($image_link = $this->getSetting('image_link')) { + $link_types = [ + 'content' => t('Linked to content'), + 'file' => t('Linked to original image'), + ]; + foreach ($image_styles as $name => $style) { + $link_types["image_style.$name"] = t('Linked to image style %style', ['%style' => $style]); + } + // Display this setting only if image is linked. + if (isset($link_types[$image_link])) { + $summary[] = $link_types[$image_link]; + } } - return $summary; } @@ -183,20 +189,15 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } $url = NULL; - $image_link_setting = $this->getSetting('image_link'); - // Check if the formatter involves a link. - if ($image_link_setting == 'content') { - $entity = $items->getEntity(); - if (!$entity->isNew()) { - $url = $entity->urlInfo(); + if ($image_link = $this->getSetting('image_link')) { + // Link to content means that each image will link to the same URL. + if ($image_link == 'content') { + $entity = $items->getEntity(); + if (!$entity->isNew()) { + $url = $entity->urlInfo(); + } } } - elseif ($image_link_setting == 'file') { - $link_source_file = TRUE; - } - elseif (!empty($image_link_setting)) { - $link_style_file = $image_link_setting; - } $image_style_setting = $this->getSetting('image_style'); @@ -208,10 +209,17 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } foreach ($files as $delta => $file) { - if (isset($link_source_file)) { - $image_uri = $file->getFileUri(); - $url = Url::fromUri(file_create_url($image_uri)); + // Link to original image. + if ($image_link == 'file') { + $url = Url::fromUri(file_create_url($file->getFileUri())); + } + // Link to an image style. + elseif (strpos($image_link, 'image_style.') === 0) { + $name = Unicode::substr($image_link, 12); + $style = ImageStyle::load($name); + $url = Url::fromUri($style->buildUrl($file->getFileUri())); } + $cache_tags = Cache::mergeTags($cache_tags, $file->getCacheTags()); // Extract field item attributes for the theme function, and unset them @@ -220,11 +228,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $item_attributes = $item->_attributes; unset($item->_attributes); - if (isset($link_style_file)) { - $style = entity_load('image_style', $image_link_setting); - $url = Url::fromUri($style->buildUrl($image_uri)); - } - $elements[$delta] = array( '#theme' => 'image_formatter', '#item' => $item, diff --git a/core/modules/image/src/Tests/ImageFieldDisplayTest.php b/core/modules/image/src/Tests/ImageFieldDisplayTest.php index b26c838..89002d9 100644 --- a/core/modules/image/src/Tests/ImageFieldDisplayTest.php +++ b/core/modules/image/src/Tests/ImageFieldDisplayTest.php @@ -204,11 +204,10 @@ function _testImageFieldFormatters($scheme) { } // Test if image_styles are linked correctly. - $display_options['settings']['image_link'] = 'thumbnail'; + $display_options['settings']['image_link'] = 'image_style.thumbnail'; $display_options['settings']['image_style'] = 'medium'; - $display->setComponent($field_name, $display_options) - ->save(); + $display->setComponent($field_name, $display_options)->save(); if ($scheme == 'private') { $this->drupalLogin($this->adminUser); @@ -219,13 +218,13 @@ function _testImageFieldFormatters($scheme) { $this->assertTrue(in_array('config:image.style.medium', $cache_tags)); $elements = $this->xpath( - '//a[@href=:path]/img[@src=:url and @alt="" and @width=:width and @height=:height]', - array( - ':path' => entity_load('image_style', 'thumbnail')->buildUrl($image_uri), - ':url' => entity_load('image_style', 'medium')->buildUrl($image_uri), + '//a[@href=:path]/img[@src=:url and @width=:width and @height=:height]', + [ + ':path' => ImageStyle::load('thumbnail')->buildUrl($image_uri), + ':url' => ImageStyle::load('medium')->buildUrl($image_uri), ':width' => 40, ':height' => 20, - ) + ] ); $this->assertEqual(count($elements), 1, 'Image linked to image style formatter displaying correctly on full node view.'); }