Commit fc8a5591 authored by alexpott's avatar alexpott

Issue #2358319 by hussainweb, Cottser, pbuyle, mgifford, geertvd, crasx,...

Issue #2358319 by hussainweb, Cottser, pbuyle, mgifford, geertvd, crasx, mahtoranjeet, markconroy: Alt tag missing on user images
parent a16db5ac
......@@ -7,6 +7,7 @@
namespace Drupal\user\Tests;
use Drupal\image\Entity\ImageStyle;
use Drupal\simpletest\WebTestBase;
use Drupal\file\Entity\File;
......@@ -97,9 +98,15 @@ function testPictureOnNodeComment() {
// Enable user pictures on nodes.
$this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save();
// Verify that the image is displayed on the user account page.
$image_style_id = $this->config('core.entity_view_display.user.user.compact')->get('content.user_picture.settings.image_style');
$style = ImageStyle::load($image_style_id);
$image_url = $style->buildUrl($file->getfileUri());
$alt_text = 'Profile picture for user ' . $this->webUser->getUsername();
// Verify that the image is displayed on the node page.
$this->drupalGet('node/' . $node->id());
$this->assertRaw(file_uri_target($file->getFileUri()), 'User picture found on node page.');
$elements = $this->cssSelect('.node__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
$this->assertEqual(count($elements), 1, 'User picture with alt text found on node page.');
// Enable user pictures on comments, instead of nodes.
$this->config('system.theme.global')
......@@ -111,7 +118,8 @@ function testPictureOnNodeComment() {
'comment_body[0][value]' => $this->randomString(),
);
$this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Save'));
$this->assertRaw(file_uri_target($file->getFileUri()), 'User picture found on comment.');
$elements = $this->cssSelect('.comment__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
$this->assertEqual(count($elements), 1, 'User picture with alt text found on the comment.');
// Disable user pictures on comments and nodes.
$this->config('system.theme.global')
......
......@@ -376,6 +376,23 @@ function user_user_view(array &$build, UserInterface $account, EntityViewDisplay
}
}
/**
* Implements hook_ENTITY_TYPE_view_alter() for user entities.
*
* This function adds a default alt tag to the user_picture field to maintain
* accessibility.
*/
function user_user_view_alter(array &$build, UserInterface $account, EntityViewDisplayInterface $display) {
if (user_picture_enabled() && !empty($build['user_picture'])) {
foreach (Element::children($build['user_picture']) as $key) {
$item = $build['user_picture'][$key]['#item'];
if (!$item->get('alt')->getValue()) {
$item->get('alt')->setValue(\Drupal::translation()->translate('Profile picture for user @username', ['@username' => $account->getUsername()]));
}
}
}
}
/**
* Implements hook_preprocess_HOOK() for block templates.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment