Skip to content
Snippets Groups Projects
Unverified Commit 8b005ec9 authored by Matt Glaman's avatar Matt Glaman Committed by GitHub
Browse files

DIT-1310 Ensure alt text in responsive image styles (#211)

parent 47b646c7
No related branches found
Tags 1.0.8
No related merge requests found
{
"name": "drupal/acquia_dam",
"type": "drupal-module",
"description": "Integrates with Acquia DAM",
"license": "GPL-2.0-or-later",
"type": "drupal-module",
"require": {
"php": "^7.4 || ^8",
"drupal/views_remote_data": "^1.0.1",
"guzzlehttp/psr7": "^1.0 || ^2.0"
},
"conflict": {
"acquia/cohesion": "<6.9.3"
},
"require-dev": {
"acquia/cohesion": "^7.0",
"acquia/cohesion-theme": "^7.0",
......@@ -19,5 +16,8 @@
"drupal/focal_point": "^1.5 || ^2.0",
"drupal/media_acquiadam": "^2.0",
"drupal/token": "^1.10"
},
"conflict": {
"acquia/cohesion": "<6.9.3"
}
}
{
"name": "drupal/acquia_dam_integration_links",
"type": "drupal-module",
"description": "Provides enhanced integration links for Acquia DAM.",
"license": "GPL-2.0-or-later",
"type": "drupal-module",
"require": {}
}
......@@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace Drupal\acquia_dam\Plugin\Field\FieldFormatter;
use Drupal\acquia_dam\Entity\ImageAltTextField;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
......@@ -226,6 +227,8 @@ final class ResponsiveImageFormatter extends FormatterBase {
$uri .= "/$asset_field->version_id";
}
$alt_text = $media->get(ImageAltTextField::IMAGE_ALT_TEXT_FIELD_NAME)->value ?? '';
$elements[] = [
'#theme' => 'responsive_image',
'#responsive_image_style_id' => $responsive_image_style ? $responsive_image_style->id() : '',
......@@ -235,6 +238,9 @@ final class ResponsiveImageFormatter extends FormatterBase {
],
'#width' => $image_attributes['width'],
'#height' => $image_attributes['height'],
'#attributes' => [
'alt' => $alt_text,
],
];
return $elements;
......
......@@ -67,6 +67,7 @@ final class ResponsiveImageStyleTest extends AcquiaDamKernelTestBase {
'version_id' => $version_id,
'external_id' => $external_id,
],
'acquia_dam_alt_text' => "alt text for $external_id!",
]);
$media->save();
assert($media instanceof MediaInterface);
......@@ -105,6 +106,9 @@ final class ResponsiveImageStyleTest extends AcquiaDamKernelTestBase {
],
'#width' => 157,
'#height' => 120,
'#attributes' => [
'alt' => "alt text for $external_id!",
],
], $field[0]);
// Render the field to assert the <picture> element exists.
......@@ -116,7 +120,8 @@ final class ResponsiveImageStyleTest extends AcquiaDamKernelTestBase {
"https://laser.widen.net/content/$external_id/web/Wheel%20Illustration.ai?w=1300 1x",
"https://laser.widen.net/content/$external_id/web/Wheel%20Illustration.ai?w=650 1x",
"https://laser.widen.net/content/$external_id/web/Wheel%20Illustration.ai?w=325 1x",
]
],
"alt text for $external_id!"
);
}
......@@ -138,12 +143,15 @@ final class ResponsiveImageStyleTest extends AcquiaDamKernelTestBase {
* The expected fallback URL.
* @param array $expected_srcsets
* The expected srcsets.
* @param string $expected_alt_text
* The expected alt text.
*/
private function assertResponsiveImageOutput(string $expected_fallback, array $expected_srcsets): void {
private function assertResponsiveImageOutput(string $expected_fallback, array $expected_srcsets, string $expected_alt_text): void {
self::assertCount(1, $this->cssSelect('picture'));
$picture_img = $this->cssSelect('picture img');
self::assertEquals($expected_alt_text, $picture_img[0]->attributes()->alt[0] ?? '');
// Verify fallback formatter is transformed by `max_325x325`.
self::assertEquals((string) $picture_img[0]->attributes()->src[0], $expected_fallback);
self::assertEquals($expected_fallback, $picture_img[0]->attributes()->src[0] ?? '');
self::assertCount(1, $picture_img);
$picture_source = $this->cssSelect('picture source');
self::assertCount(4, $picture_source);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment