From 3e8c2b34a649c35fccf38c146645a73f65173605 Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Fri, 28 Jun 2019 07:16:01 +1000 Subject: [PATCH] =?UTF-8?q?Issue=20#2829990=20by=20Krzysztof=20Doma=C5=84s?= =?UTF-8?q?ki,=20idebr,=20lamp5:=20Image=20formatter=20does=20not=20suppor?= =?UTF-8?q?t=20URL/Link=20options?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../image/templates/image-formatter.html.twig | 2 +- .../tests/src/Kernel/ImageFormatterTest.php | 26 +++++++++++++++++++ .../templates/field/image-formatter.html.twig | 2 +- .../templates/field/image-formatter.html.twig | 2 +- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/core/modules/image/templates/image-formatter.html.twig b/core/modules/image/templates/image-formatter.html.twig index 63ca3c6fd71..b0d1aa2bd9b 100644 --- a/core/modules/image/templates/image-formatter.html.twig +++ b/core/modules/image/templates/image-formatter.html.twig @@ -14,7 +14,7 @@ */ #} {% if url %} - {{ image }} + {{ link(image, url) }} {% else %} {{ image }} {% endif %} diff --git a/core/modules/image/tests/src/Kernel/ImageFormatterTest.php b/core/modules/image/tests/src/Kernel/ImageFormatterTest.php index 56b552e59c9..5d97cdf77d2 100644 --- a/core/modules/image/tests/src/Kernel/ImageFormatterTest.php +++ b/core/modules/image/tests/src/Kernel/ImageFormatterTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\image\Kernel; use Drupal\Core\Field\FieldStorageDefinitionInterface; +use Drupal\Core\Url; use Drupal\entity_test\Entity\EntityTest; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; @@ -173,6 +174,31 @@ class ImageFormatterTest extends FieldKernelTestBase { $this->assertFalse(strpos($build[$this->fieldName][1]['#markup'], 'height')); } + /** + * Tests Image Formatter URL options handling. + */ + public function testImageFormatterUrlOptions() { + $this->display->setComponent($this->fieldName, ['settings' => ['image_link' => 'content']]); + + // Create a test entity with the image field set. + $entity = EntityTest::create([ + 'name' => $this->randomMachineName(), + ]); + $entity->{$this->fieldName}->generateSampleItems(2); + $entity->save(); + + // Generate the render array to verify URL options are as expected. + $build = $this->display->build($entity); + $this->assertInstanceOf(Url::class, $build[$this->fieldName][0]['#url']); + $build[$this->fieldName][0]['#url']->setOption('attributes', ['data-attributes-test' => 'test123']); + + /** @var \Drupal\Core\Render\RendererInterface $renderer */ + $renderer = $this->container->get('renderer'); + + $output = $renderer->renderRoot($build[$this->fieldName][0]); + $this->assertContains('{{ image }} + {{ link(image, url) }} {% else %} {{ image }} {% endif %} diff --git a/core/themes/stable/templates/field/image-formatter.html.twig b/core/themes/stable/templates/field/image-formatter.html.twig index d0390c041c8..512d7588e0f 100644 --- a/core/themes/stable/templates/field/image-formatter.html.twig +++ b/core/themes/stable/templates/field/image-formatter.html.twig @@ -12,7 +12,7 @@ */ #} {% if url %} - {{ image }} + {{ link(image, url) }} {% else %} {{ image }} {% endif %}