Issue #2829990 by Krzysztof Domański, idebr, lamp5: Image formatter does not support URL/Link options

merge-requests/1119/head
Lee Rowlands 2019-06-28 07:16:01 +10:00
parent de56b25930
commit 3e8c2b34a6
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
4 changed files with 29 additions and 3 deletions

View File

@ -14,7 +14,7 @@
*/
#}
{% if url %}
<a href="{{ url }}">{{ image }}</a>
{{ link(image, url) }}
{% else %}
{{ image }}
{% endif %}

View File

@ -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('<a href="' . $entity->toUrl()->toString() . '" data-attributes-test="test123"', (string) $output);
}
/**
* Asserts that a renderable array has a set of cache tags.
*

View File

@ -13,7 +13,7 @@
*/
#}
{% if url %}
<a href="{{ url }}">{{ image }}</a>
{{ link(image, url) }}
{% else %}
{{ image }}
{% endif %}

View File

@ -12,7 +12,7 @@
*/
#}
{% if url %}
<a href="{{ url }}">{{ image }}</a>
{{ link(image, url) }}
{% else %}
{{ image }}
{% endif %}