Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Users have no indication if the "alt text required" option has been enabled on the image field because the usual Red Asterisk indicating a field is required is not being shown next to this field.
Proposed resolution
Add the "required" attribute to this field.
Before
After
Remaining tasks
Write patch#88 by @claudiu.cristea- Code review
Manual testby @kattekrab
User interface changes
Yes. An improvement! But a very small change.
API changes
None?
Beta phase evaluation
Issue category | Bug because this is a usability problem and easily fixed. |
---|---|
Issue priority | Normal - it's not actually preventing anything from working, but will be a win for overall accessibility and WCAG compliance. | Unfrozen changes | Unfrozen because it simply adds an indicator that the alt tag is a required field, which is missing currently. |
Prioritized changes | The main goal of this issue is usability & accessibility |
Disruption | Minimal to none |
Original report by @mgifford
Required * isn't present on required alt text. Probably same on the Title too.
<div class="form-item form-type-textfield form-item-field-image2-fr-0-alt">
<label for="edit-field-image2-fr-0-alt">Alternate text</label>
<input id="edit-field-image2-fr-0-alt" class="form-text" type="text" maxlength="512" size="60" value="What with alt text" name="field_image2[fr][0][alt]" aria-describedby="edit-field-image2-fr-0-alt--description">
<div id="edit-field-image2-fr-0-alt--description" class="description">This text will be used by screen readers, search engines, or when the image cannot be loaded.</div>
</div>
It is required and you get the error message but no red "*"
Comment | File | Size | Author |
---|---|---|---|
#93 | 1906264-asterisk.png | 35.55 KB | kattekrab |
#89 | Screen Shot 2015-02-01 at 1.28.59 pm.png | 228.83 KB | kattekrab |
#89 | Screen Shot 2015-02-01 at 1.23.13 pm.png | 43.06 KB | kattekrab |
#88 | interdiff-to-84.txt | 2.29 KB | claudiu.cristea |
#88 | required_alt_tag-1906264-88.patch | 4.33 KB | claudiu.cristea |
Comments
Comment #1
blueminds CreditAttribution: blueminds commentedI am afraid this cannot be fixed easily. Moreover I think this is by the design and not really a bug.
The problem is that on hitting the upload button it gets validated. So there is a custom validation callback for the alt and title attributes that take into account if the file has been initially uploaded or the form is actually being submitted as a whole. In case you set the field to be #required you will get error messages just after hitting the Upload button, which is not correct as from the user point of view the error has not occurred. You can mess around with #limit_validation_errors for upload and remove buttons, but it will not work properly for remove action in case you remove item that is not the last one. Attaching patch with which I could come up... maybe someone else will be able to handle it.
Another solution would be to simply display Alt/Title fields implicitly for new items.
Comment #2
blueminds CreditAttribution: blueminds commentedsorry, includes some phpunit stuff, so fixed patch
Comment #3
mgiffordWell, I haven't looked at the code in any depth, but the output is working.
Comment #4
blueminds CreditAttribution: blueminds commentedtry following:
- add multiple images
- hit any Remove action but the last one
result will be that all the items that followed get removed as well.
So if this can be fixed then we are fine. But I could not come up with reasonable solution, it looks like the problem is that $form_state gets rebuild in a different way then the submit_validation_errors arrays are built and so the remaining items get removed from $form_state['values'].
Comment #5
mgiffordWell, I can confirm that this is a problem that is introduced in the patch.
I'd say it isn't something that is acceptable as it is introducing a bug.
Comment #6
blueminds CreditAttribution: blueminds commentedSo another step in evolution :)
This patch work just fine. However, introduces one small bug - when I upload an image, fill out alt or title, then upload another one without saving the whole form the values I previously entered for title and alt are gone. So if this gets solved, I think we have what we wanted.
Comment #7
mgiffordSorry, does the patch in #6 address this? Not sure why you didn't engage the bot. Just not sure how to move this issue ahead.
Comment #8
kim.pepperEngaging the bot...
Comment #9
blueminds CreditAttribution: blueminds commentedWith this patch it should all work nicely. Please try out.
Comment #11
blueminds CreditAttribution: blueminds commentedanother try
Comment #13
blueminds CreditAttribution: blueminds commented#11: drupal-alt_title_required-1906264-5.patch queued for re-testing.
Comment #14
mgiffordPatch seems to work well. I did a quick review of the code and it made sense to me. Good to extend function image_field_widget_process().
Comment #15
xjm#11: drupal-alt_title_required-1906264-5.patch queued for re-testing.
Comment #16
xjmThanks @mgifford and @blueminds!
This looks like an accessibility bug to me. Also, I think we probably need to add an automated test for this?
Also, a few minor suggestions for the comments:
This comment could be simplified and clarified a lot. For example, the whole first line is superfluous.
This could also be shortened and clarified. Also, the first line is over 80 characters.
There should be a comment after "uploaded," and let's say "file ID" instead of "fid."
The words "Here we need to" can be removed.
I'd change this to "Validate the whole field for previously uploaded files."
This can also be simplified. I'd suggest "Limit validation errors for the upload and remove buttons."
Comment #17
mgiffordIn number 3 above, what did you mean by "There should be a comment after "uploaded,"?
I applied the rest, trying to simplify the comments as you suggested.
So, we still need automated testing needed.
Comment #18
xjm@mgifford Can you provide an interdiff? Thanks!
Comment #19
mgiffordI hate interdiff's. Understand why they are useful, but I've found that none of the explanations I've seen:
http://drupal.org/documentation/git/interdiff
http://hojtsy.hu/blog/2012-apr-13/quick-and-simple-interdiffs
http://xjm.drupalgardens.com/blog/interdiffs-how-make-them-and-why-they-...
really help. They work fine if you're already doing this as part of your workflow, but otherwise.
There was also stuff missing like, how do you delete a branch:
git branch -D old_patch
or steps like adding the code to the repo.
It's also particularly frustrating to try to follow these instructions already having 2 patches built, but not being able to apply the second on because the assumption is that you haven't already built the second patch...
Anyways, hope that this approach work. It's still awkward, but might well be better.
I just went to a Linux box though and installed interdiff
sudo apt-get install interdiff
and then am doing it with:interdiff old.patch new.patch > interdiff.txt
Hope this interdiff helps.
Comment #20
xjmNot really the place to talk about all this on issue, but ask in IRC if you're having any difficulty with git and folks will help.
I'm not sure how that can be "missing" from a doc that has a very specific purpose. That's elsewhere in the git documentation.
Create two branches. Apply your old patch to the first and your new patch to the second. Diff the second branch against the first. There's your interdiff. You can diff anything against anything in git; the instructions are just for a common usecase.
IIRC that's also suggested in a lot of the documentation.
Comment #21
mgiffordThanks @xjm.. I'll get it eventually... Hope the interdiff I provided helps.
Comment #22
mgifford#17: drupal-alt_title_required-1906264-17.patch queued for re-testing.
Comment #24
mgiffordJust a re-roll of #17.
Comment #25
mgifford#24: drupal-alt_title_required-1906264-23.patch queued for re-testing.
Comment #26
mparker17#24: drupal-alt_title_required-1906264-23.patch queued for re-testing.
Comment #27
shanly CreditAttribution: shanly commentedI'm reviewing and testing.
Comment #28
shanly CreditAttribution: shanly commentedIs this a potential XSS vulnerability? Should it be sanitized here or is it already sanitized or sanitized after?
Is this a potential XSS vulnerability? Should it be sanitized here or is it already sanitized or sanitized after?
Comment #29
mgifford#24: drupal-alt_title_required-1906264-23.patch queued for re-testing.
Comment #30
mgiffordI'm pretty sure they'd need to be sanitized because it is being pulled right from the $_POST.
Comment #32
mgiffordre-roll with added
check_plain()
.Comment #33
mgifford#32: drupal-alt_title_required-1906264-32.patch queued for re-testing.
Comment #35
mgiffordReroll. 3 of 4 chunks failed.
Comment #37
mgiffordRelated issue #2034999: [meta] Comply with WCAG 1.1: Text Alternatives
Comment #38
johanv CreditAttribution: johanv commentedI did not fix the issue, but here is a unit test, which obviously fails, because the bug is still unfixed. That's why I added -do-not-test to the patch name.
Comment #39
claudiu.cristeaLet's see how fails. Can you repost without suffix, so that we can see the test running and failures?
Comment #40
mgiffordThis is a combination of #35 & #38, both of which still apply with no problems. Let's see what errors this causes.
Comment #42
johanv CreditAttribution: johanv commentedAs requested in #39, the unit test without do-not-test. Which will fail.
I tried the patch from #40 as well with simpletest.me. I configured the story content type to make the alt text of the image required. When I then want to add a new story, I get notices 'Undefined variable: settings in image_field_widget_process()'. (see attached screenshot)
And when I select an image, the required indicator for the alt text is not shown. (see other attached screenshot)
Comment #43
johanv CreditAttribution: johanv commentedOops, I forgot to change the status to 'Needs review'. Sorry.
Comment #44
claudiu.cristeaOK. Cancelled the test from #43.
Comment #45.0
(not verified) CreditAttribution: commentedAdding screenshot to display
Comment #46
mgifford43: drupal-unit_test_for_required_tag_alt_text-1906264-43.patch queued for re-testing.
Comment #48
mgiffordThe module moved to core/modules/image/lib/Drupal/image/Plugin/Field/FieldWidget/ImageWidget.php
#43 just includes the test. I've brought it over, but
$settings['alt_field_required'] || $settings['title_field_required']
aren't available in $settings any more.I'm not sure how to properly use getSettings() without $this....
Comment #49
mgiffordComment #50
mitsuroseba CreditAttribution: mitsuroseba commentedI wrote new patch instead of reroll, because i think it more simple.
Comment #52
mitsuroseba CreditAttribution: mitsuroseba commentedReroll
Comment #53
barraponto CreditAttribution: barraponto commentedIt seems to me that only the
return
statement should be within the if clause.Comment #54
mitsuroseba CreditAttribution: mitsuroseba commentedThanks, @barraponto.
In new path removed $form_state['limit_validation_errors'] = array();
Comment #56
mitsuroseba CreditAttribution: mitsuroseba commentedReroll. Changed randomName -> randomMachineName.
Comment #57
mgiffordI still don't understand why the code below
// Check if field is left empty.
is removed..However the patch works great. I tested it with both Alt & Title text to verify it works well for both as required fields.
I'm basically ready to mark it RTBC based on my tests on SimplyTest.me and review of the code... But I want to know why that code was removed and check that there aren't going to be other implications of this.
Comment #58
mgiffordOh ya, a quick screenshot from my tests...
Comment #59
mitsuroseba CreditAttribution: mitsuroseba commentedRemoved because we have now required attribute on alt and title and not need custom validation.
Without patch message
$form_state->setError($element, t('The field !title is required', array('!title' => $element['#title'])));
uppear when #title_field_required = 1 and submit form not image process.
Comment #60
mgiffordExcellent.. In that case, RTBC.
Comment #62
claudiu.cristeaOnly a small coding standards fix, please. Should be
Comment #63
amitgoyal CreditAttribution: amitgoyal commentedPlease review revised patch with a fix mentioned in #62.
Comment #64
mgiffordGood catch @claudiu.cristea - setting it back.
Comment #65
alexpottIt would be better if there were no messages here since since the assert's default message will indicate exactly what is the problem.
What I mean is this:
Comment #66
mgifford@alexpott - That looks pretty easy to implement.
Comment #67
claudiu.cristeaNitpick. Capitalize first letter, end with a dot.
Comment #68
mitsuroseba CreditAttribution: mitsuroseba commentedComment #69
claudiu.cristeaRTBC if turns green.
Comment #71
mitsuroseba CreditAttribution: mitsuroseba commentedWhoa, reroll.
Comment #72
claudiu.cristeartbc
Comment #76
claudiu.cristeainterdiff against #66.
Comment #77
mgiffordLooks good! Applying it here http://sc61e125c2b67564.s2.simplytest.me/node/add/article
No problem with mandatory values (on alt or title).
Will be great to get this fixed. Thanks again @alexpott for the direction.
Comment #78
alexpottI think we can remove the #element_validate now and remove ImageWidget::validateRequiredFields()
Comment #79
mgiffordI had to re-roll the code. However, @alexpott, I don't understand the change you're requesting. Is it just this:
Comment #81
mgiffordComment #82
kattekrab CreditAttribution: kattekrab commentedNow that #2303765: Make the default 'alt' attribute for Image fields required is green - we really need to get this one resolved.
Comment #83
kattekrab CreditAttribution: kattekrab commentedLooks like there's something wrong with the patch - there's a bunch of stuff added at the bottom, that looks like it shouldn't be there.
Comment #84
amitgoyal CreditAttribution: amitgoyal commentedRe-roll of #79.
Comment #86
Sivaji_Ganesh_Jojodae CreditAttribution: Sivaji_Ganesh_Jojodae commentedRerolled patch #84 to prevent git line offset warning.
Comment #88
claudiu.cristea@alexpott, unfortunately
#element_validate
(and::validateRequiredFields()
) cannot be removed. In fact there we are doing the "required" validation. Why? Because when the form is displayed for the first time the 2 textfields are not displayed and are empty. When an image is picked up the form is submitted and validated and that is causing a validation error.::validateRequiredFields()
is the workaround. See also::validateRequiredFields()
docblock. We are using#required
only to get the red * on the form.Interdiff against #84
Comment #89
kattekrab CreditAttribution: kattekrab commentedSetting back to needs work.
Just did manual test with simplytest.me
Uploaded an image. No indication that alt text is required, also let me save without alt text. So, I'm not getting the error, but the point here was to improve our base accessibility by requiring alt text, and informing editors that they should insert an empty tag if the image is purely decorative.
It has inserted an empty alt tag though.
Comment #90
kattekrab CreditAttribution: kattekrab commentedHmmm.
Been thinking about this. Maybe this is actually better? Quietly just insert the empty alt tag. If most images are purely decorative... maybe this is the way to go? It's less jarring to the user than getting an error. Setting back to needs review for someone else's opinion.
The use of alt text around images isn't always straightforward. And because a field image is often used in multiple contexts (on it's node, but also in lists generated by views, possibly as links from a list view to a node...) it can get quite tricky, and the way forward seems subjective.
Comment #91
Charles BelovThe content editor needs to make a concious determination whether an image is purely decorative. So defaulting to adding an empty alt attribute is contrary to providing accessibility.
Comment #92
claudiu.cristea@kattekrab, @Charles Belov,
Yes, I get the point but in this ticket we are only showing the "required" asterisk to
alt
&title
form elements if the site administrator decided that those fields are required. Setting thealt
tag required by default is out of scope. Please fill a follow-up explaining why we need that. I would be happy to provide a patch.In the meantime if you feel that the patch from #88 has fixed the original requirement, please RTBC.
Comment #93
kattekrab CreditAttribution: kattekrab commentedOops - of course @claudiu.cristea - Sorry about that!
I tested again, edited the article content type to make the image field required this time, and voila - the asterisk is there now!
You are absolutely right that making it required by default is out of scope. And there is already another ticket for that. See #2303765: Make the default 'alt' attribute for Image fields required which is still at "need work".
Screenshot attached showing "Alternative Text" field now has red asterisk indicating it is a required field. RTBC from manual test. Could probably do with a quick actual code review from someone who reads code.
Thanks @claudiu.cristea!! This is great. I'm updating the issue summary too.
Comment #94
kattekrab CreditAttribution: kattekrab commentedComment #95
kattekrab CreditAttribution: kattekrab commentedComment #96
mgiffordThis looks good to me. Thanks for marking it RTBC @kattekrab - +1
Comment #98
webchickLooks like Alex has been following this one.
Comment #99
alexpottCommitted cd57d59 and pushed to 8.0.x. Thanks!
Thanks for adding the beta evaluation to the issue summary.