When a user uploads an image in Core, that image should include alt text.

There are ways to work around that in some contexts, but I do think in this case, there should probably be:

<img width="74" height="85" class="image-style-thumbnail" typeof="foaf:Image" alt="Profile image for user admin" src="http://s547d5cfd6ab8acb.s3.simplytest.me/sites/default/files/styles/thumbnail/public/pictures/drupal_ngo.png?itok=5YiK_Iel">

Rather than alt="".

Missing alt tag in user profile.

After

After patch, has alt tag

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

RobLoach’s picture

Is alt required by the HTML5 spec? I'd recommend:

  1. Having it generate one from the src if it doesn't exist
  2. ... and passing that default one for user pictures
mgifford’s picture

It's required by WCAG 2.0 AA.

Generating an alt text from the source is almost always a bad idea. Way better to leave it as alt="", however a user photo is almost always going to be a representation of the user. I used the Drupal logo, but that's a representation of me, so the alt text should just be alt="Profile image for user admin" for the example I provided above.

A good alt text page from WebAim - http://webaim.org/techniques/alttext/

A discussion about how to do this in D7 on Stack Overflow:
http://drupal.stackexchange.com/questions/60059/how-to-add-alt-and-title...

Definitely just wanting to set a default alt text for user pictures. I really don't think this is something it makes sense to expose to the user.

serundeputy’s picture

I believe you can do this here: /admin/config/people/accounts/fields/user.user.user_picture?bundle=user

Set the alt text to required.

mgifford’s picture

You can enable the alt text there for sure.

However, that would just mean that every user would have to enter in alt text for their photos (which they won't do unless you make it required in which case they will just be annoyed and go away).

Without tokens though I don't know how we can spit out something like alt="Profile image for user admin" though automatically.

I'm really not even sure if this is something you'd want to give users the ability to edit through the UI. It's a profile photo and really should be clearly there to describe the user.

dshields’s picture

This is also an a11y fail in Drupal 7 core.

mgifford’s picture

Issue tags: +Needs backport to 7.x
mgifford’s picture

Status: Active » Needs review
FileSize
707 bytes

Ok, this seems to work, but doesn't look pretty. Also, it really should just assign the username by default.

xjm’s picture

Issue tags: -Needs backport to 7.x +Needs backport to D7
BarisW’s picture

Not sure if this is the best way, but we definately need a default alt tag on profile pictures. Thanks Mike!

mgifford’s picture

Status: Needs review » Needs work

@BarisW - I was mostly trying to get this moving. Hopefully we can find a more elegant way to do this. In the interm, moving this to "Needs work".

mahtoranjeet’s picture

mahtoranjeet’s picture

Status: Needs work » Needs review
mahtoranjeet’s picture

mahtoranjeet’s picture

Status: Needs review » Needs work
markconroy’s picture

@mgifford - Mike, three issues I'm finding with your solution (and I appreciate it's not a finished product, but a work in progress):

1) It enables the alt field which allows people to edit it - we could hide it with js or css, I guess
2) For some reason it's only showing the alt text on the frontend (as in, the Bartik theme) not on the backend (as in, the Seven theme)
3) The 'name' variable is not being passed in - the alt tag's default is blank when I tried this approach.

So, looks like we need to keep alt_field: false and come up with some way of dynamically injecting the info in there.

Back to the drawing thinking board for me.

Status: Needs work » Needs review
mgifford’s picture

Status: Needs review » Needs work

Thanks @markconroy - it's definitely a work in progress.

1) Interesting. It's certainly something that shouldn't be editable.
2) Odd with the difference between Bartik & Seven. Definitely it should work on all themes.
3) Not having the variable name defeats the point.

The patch needs to be re-done as it no longer applies to Core.

Did you use the patch in #7 or #11 - they are very different. @mahtoranjeet - I don't understand your patch.

Just editing the core/profiles/standard/config/install/field.field.user.user.user_picture.yml seems a bit to minimal.

The last submitted patch, 11: Not-include-leading-and-trailing-spaces-2254235-12.patch, failed testing.

markconroy’s picture

Hi Mike,

I didn't apply either patch, I just edited the necessary file to check, but you're right, we need to edit more than that.

I can't make sense of the patch in #11. I don't think it should belong in this thread.

crasx’s picture

Looks like patch #11 was posted in the wrong thread.

I'm not sure that we should use the alt field for this because we should pass the alt text through translation. Another issue is that the user's photo field can be named anything since it is done through the field api, so setting it through a form alter won't be reliable. If we do only target the "user_picture" field maybe we could to it when the field is loaded?

pbuyle’s picture

Targeting the user_picture field seems fine, the goal is to provide proper behaviour out of the box. If a site builder is not using the existing, default, user_picture field then it is up to that site builder to ensure the behaviour of the used custom field.

pbuyle’s picture

I'm at #DrupalNorth sprint, I'll attempt a patch to alter the alt value for user_picture in a hook_user_load() implementation and ensure a non-empty value.

pbuyle’s picture

The attached patch ensure there is always an alt text for the user_picture field in an implementation of hook_ENTITY_TYPE_view_alter() for user entities.

pbuyle’s picture

Status: Needs work » Needs review
Issue tags: +#DrupalNorth

Status: Needs review » Needs work

The last submitted patch, 23: drupal-alt_for_user_picture-2358319-23.patch, failed testing.

pbuyle’s picture

Status: Needs work » Needs review
FileSize
1.3 KB
Anonymous’s picture

Issue tags: +DrupalNorth2015

Updating the issue tag to include a hashless DrupalNorth2015 on behalf of the Drupal North sprinting group.

markconroy’s picture

Hi pbuyle,

I haven't applied your patch yet, but looks very good. And Crassx, well done to you for coming up with this as a solution.

crasx’s picture

FileSize
1.11 KB
48.2 KB

added documentation to the function so there is no confusion in the future about why it exists.

crasx’s picture

Issue summary: View changes
mgifford’s picture

Issue summary: View changes
FileSize
36.8 KB

This looks a lot better. alt="Profile picture for user m fish" works well here:

with alt text

Thanks for the patch @pbuyle & the docs @crasx

Do we need to have tests here before it is RTBC?

BarisW’s picture

+++ b/core/modules/user/user.module
@@ -378,6 +378,22 @@ function user_user_view(array &$build, UserInterface $account, EntityViewDisplay
+  if (user_picture_enabled() && !empty($build['user_picture'])) {

Just wondering is a call to user_picture_enabled() is needed here. Wouldn't a check on empty($build['user_picture]) suffice?
Other than that: great fix.

++

crasx’s picture

I think we should keep user_picture_enabled so that that logic is centralized. Interestingly that function is only used in one other place.
We should add tests for this so that if someone decides to change the "User_picture" field name the alt text is not lost.

mparker17’s picture

Issue tags: -#DrupalNorth

Removing old hashed tag.

mgifford’s picture

Issue tags: +Needs tests
mgifford’s picture

Status: Needs review » Needs work
Berdir’s picture

+++ b/core/modules/user/user.module
@@ -378,6 +378,22 @@ 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. See issue 2358319 for more details.
+ */

Missing empty line after the first sentence.

I also wouldn't put in the issue reference and certainly not like this, just leave that sentence out.

We just added something like this to a custom theme, and we did it in preprocess_image_formatter(), there it can be changed directly on the resulting render array instead of indirectly through changing the data.

This could actually end up being saved in case someone views a user and then saves it.

hussainweb’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
2.33 KB
2.24 KB

Fixing for comments in #37 and added a couple of asserts in an existing test.

mgifford’s picture

FileSize
201.12 KB

This looks great. It's got tests. I think all outstanding concerns have been addressed. Here's a screenshot for the record.

mgifford’s picture

Status: Needs review » Reviewed & tested by the community
Wim Leers’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/user/src/Tests/UserPictureTest.php
@@ -100,6 +100,7 @@ function testPictureOnNodeComment() {
+    $this->assertRaw(t('Profile picture for user @username', ['@username' => $this->webUser->getUsername()]));

@@ -112,6 +113,7 @@ function testPictureOnNodeComment() {
+    $this->assertRaw(t('Profile picture for user @username', ['@username' => $this->webUser->getUsername()]));

Why not have this check alt="Profile picture […]"? Then it'd be actually checking if it was in an alt attribute.

hussainweb’s picture

Status: Needs work » Needs review
FileSize
1.43 KB
2.28 KB

Fair point. Changing the test.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/user/src/Tests/UserPictureTest.php
@@ -100,6 +100,7 @@ function testPictureOnNodeComment() {
+    $this->assertRaw(t('alt="Profile picture for user @username"', ['@username' => $this->webUser->getUsername()]));

@@ -112,6 +113,7 @@ function testPictureOnNodeComment() {
+    $this->assertRaw(t('alt="Profile picture for user @username"', ['@username' => $this->webUser->getUsername()]));

alt is not part of the translated text

hussainweb’s picture

Status: Needs work » Needs review
FileSize
1.43 KB
2.3 KB

Second time's the charm. :)

mgifford’s picture

Status: Needs review » Reviewed & tested by the community

This should be good to go now. I checked the interdiff and applied it once again.

Wim Leers’s picture

Yeah, in fact, we can just drop the t() call altogether, because this is a test, so it's never going to be translated.

markconroy’s picture

Well done folks. Looks very good. One step closer to Drupal 8

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/user/src/Tests/UserPictureTest.php
@@ -100,6 +100,7 @@ function testPictureOnNodeComment() {
+    $this->assertRaw('alt="' . t('Profile picture for user @username', ['@username' => $this->webUser->getUsername()]) . '"');

@@ -112,6 +113,7 @@ function testPictureOnNodeComment() {
+    $this->assertRaw('alt="' . t('Profile picture for user @username', ['@username' => $this->webUser->getUsername()]) . '"');

This is not actually testing the alt text on the user image - it is testing any alt on the page. Seems brittle. I think we should be using xpath - look at Drupal\image\Tests\ImageThemeFunctionTest for examples.

hussainweb’s picture

@alexpott: That's true. However, all the code in that test is in the same fashion. It checks for the file URI's in the response using assertRaw, which is just as brittle. If we were to do the check with xpath here, we should change the test to use it throughout. Does that make sense as a followup or should we do it in this issue itself?

star-szr’s picture

Taking a look at this. I don't think we should balloon the scope to change the whole test (that could be a follow-up task), but let's make sure what we add is solid.

star-szr’s picture

Status: Needs work » Needs review
FileSize
2.85 KB
2 KB

This is more specific.

geertvd’s picture

Status: Needs review » Needs work

I think the test looks a lot better now. Just 1 nitpick.

+++ b/core/modules/user/src/Tests/UserPictureTest.php
@@ -7,6 +7,7 @@
+use Drupal\Component\Utility\SafeMarkup;

@@ -100,6 +101,9 @@ function testPictureOnNodeComment() {
+    $alt_text = SafeMarkup::format('Profile picture for user @username', ['@username' => $this->webUser->getUsername()]);

@@ -112,6 +116,9 @@ function testPictureOnNodeComment() {
+    $alt_text = SafeMarkup::format('Profile picture for user @username', ['@username' => $this->webUser->getUsername()]);

We should avoid using SafeMarkup::format() in tests now. We can just replace it by a concatenated string #2549805: Remove all usage of FormattableMarkup in tests apart from explicit tests of that API

geertvd’s picture

+++ b/core/modules/user/src/Tests/UserPictureTest.php
@@ -100,6 +101,9 @@ function testPictureOnNodeComment() {
+    $this->assertEqual(count($elements), 1, 'Found alt text for user image on user account page.');

Also shouldn't this say Found alt text for user image on node page.

star-szr’s picture

Good points, thanks @geertvd! I will also try to update the img src checking to be more specific. The 'user account page' assertion text was based on this code comment but I'll check it closer:

// Verify that the image is displayed on the user account page.

star-szr’s picture

Status: Needs work » Needs review
FileSize
3.21 KB
2.79 KB

This checks that the image is in the correct spot with the correct image style and the correct alt text, and the assertion messages should be more accurate.

geertvd’s picture

This looks good, will RTBC when it's green.

geertvd’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 56: alt_tag_missing_on_user-2358319-56.patch, failed testing.

geertvd’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
1.44 KB
3.18 KB

This broke because #2214241: Field default markup - removing the divitis I still consider this RTBC

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 60: alt_tag_missing_on_user-2358319-60.patch, failed testing.

Status: Needs work » Needs review
hussainweb’s picture

Status: Needs review » Reviewed & tested by the community

These are permission denied errors. It seems random. Tentatively setting back to RTBC as per #60.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 60: alt_tag_missing_on_user-2358319-60.patch, failed testing.

borisson_’s picture

The test failures of EntityFileTest are the same here as they are in in #2553661: KernelTestBase fails to set up FileCache, so I don't think these are related.

hussainweb’s picture

Is it possible that HEAD is broken?

star-szr’s picture

Status: Needs work » Needs review

The last couple tests were on bot 3128 which according to #2429617-352: Make D8 2x as fast: Dynamic Page Cache: context-dependent page caching (for *all* users!) was broken and has now been taken out of rotation. Going to hit re-test.

geertvd’s picture

Status: Needs review » Reviewed & tested by the community

back to rtbc

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 60: alt_tag_missing_on_user-2358319-60.patch, failed testing.

Status: Needs work » Needs review
geertvd’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 60: alt_tag_missing_on_user-2358319-60.patch, failed testing.

Status: Needs work » Needs review
geertvd’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed fc8a559 and pushed to 8.0.x. Thanks!

  • alexpott committed fc8a559 on 8.0.x
    Issue #2358319 by hussainweb, Cottser, pbuyle, mgifford, geertvd, crasx...
serundeputy’s picture

Status: Patch (to be ported) » Needs review
FileSize
589.77 KB

I was starting to work on a D7 backport for this, but when I added a profile picture it already has alt text (see screenshot).

In the file template_preprocess_user_picture the lines: 1512 to 1517:

$alt = t("@user's picture", array('@user' => format_username($account)));
      // If the image does not have a valid Drupal scheme (for eg. HTTP),
      // don't load image styles.
      if (module_exists('image') && file_valid_uri($filepath) && $style = variable_get('user_picture_style', '')) {
        $variables['user_picture'] = theme('image_style', array('style_name' => $style, 'path' => $filepath, 'alt' => $alt, 'title' => $alt));
      }

So, I think no action is required for D7.

thanks,
~Geoff

star-szr’s picture

Version: 7.x-dev » 8.0.x-dev
Status: Needs review » Fixed

Cool, thanks @serundeputy!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

cilefen’s picture