Hm, you can't pass alt in $variables['attributes']. (Same for src but for different reasons.) It always ends up as "".
It's a bit of a DrupalWTF, because title, width and height *can* be passed this way. The current code fails in picking up alt because of the isset() check on $variables['alt'] and the fact that $variables['alt'] is assigned '' as default in drupal_common_theme().
Comment | File | Size | Author |
---|---|---|---|
#82 | interdiff-999338-78-82.txt | 544 bytes | lauriii |
#82 | theme_image_alt-999338-82.patch | 3.39 KB | lauriii |
#75 | theme_image_alt-999338-75.patch | 2.62 KB | joelpittet |
#73 | 999338_test_patch_works.png | 55.87 KB | meichr |
#73 | 999338_test_error_reproduced.png | 55.61 KB | meichr |
Comments
Comment #1
derjochenmeyer CreditAttribution: derjochenmeyer commentedDoc: theme_image($variables)
Why do you want to pass alt in $variables['attributes']?
Comment #2
Dave ReidDefinitely not major.
Comment #3
derjochenmeyer CreditAttribution: derjochenmeyer commentedI think theme_image() works as designed. This is not a bug. Re-open if I'm wrong.
Comment #4
RobLoachRelated: #1025796: Rename path to uri in image functions
Comment #5
effulgentsia CreditAttribution: effulgentsia commentedI think this is a valid WTF that's worth fixing in D8. There are good use-cases for wanting to pass all HTML attributes in $variables['attributes'], especially in the context of render arrays. Given D8's default html output will be html5, it may make sense to default 'alt' to NULL like the rest of them, or else remove those 4 keys from being special at all, and simply use 'attributes' exclusively.
For D7, I think we're stuck with this, because of the need for legacy compatibility, and coddling people towards valid html4/xhtml. See #555830: Clean up theme_image() to use drupal_attributes() for all attributes and revisit defaults for "alt" and "title" for background.
Comment #6
sun1) For D7, we want to replace the isset() with array_key_exists() to allow overriding the default of '' with NULL.
2) $variables vs. $variables['attributes'], we can fix, too.
For D8, we probably want to open a separate issue to remove the totally whack separated variables and only support 'attributes'. Or similar.
Comment #7
sunComment #8
effulgentsia CreditAttribution: effulgentsia commentedThis will always be TRUE, given the default in drupal_common_theme().
Comment #9
Everett Zufelt CreditAttribution: Everett Zufelt commentedI would be a fan of using just attributes, although, using explicit variables is useful so that people can see the commonly used attributes.
I would be a huge fan of alt * not * defaulting to ''. If alt doesn't default to '' then the html5 validator will most likely throw a warning (unless it is one of the stupid exclusions the co-chairs have added to the spec). The warning was intentionally designed so that people would be made aware of the missing alt attribute on the image, but still allows the page to validate.
** note: this is from memory of the spec and associated discussions and things may have changed.
Comment #10
mgifford#6: drupal8.theme-image-attributes.6.patch queued for re-testing.
Comment #12
mgiffordThis is basically a re-roll. I didn't address @effulgentsia's concern in #8. Sounds like it should be just simplified and that if statement removed to simplify it.
Comment #13
mgifford#12: drupal8.theme-image-attributes.999338-12.patch queued for re-testing.
Comment #15
mgiffordI'm bumping this up to major because it's inconsistent and because it restricts accessibility for no good reason. This can be fixed in D8.
Comment #16
marcingy CreditAttribution: marcingy commentedThis is not major as per #2
Comment #17
Eric_A CreditAttribution: Eric_A commentedHm, if you pass in a NULL value for $variables['alt'] and nothing for $variables['attributes']['alt'], then the image element won't have an alt attribute. So there *is* a way, even if not very pretty. If we document this, then we have our official no alt API for D7 (and D8).
Returning to my original report: by passing in a NULL value for $variables['alt'], any value for $variables['attributes']['alt'] does win.
Regarding #8: that seems bogus. We have process, preprocess and registry_alter as possible ways to unset the alt key.
Comment #18
mgiffordRemember, alt tags are the low hanging fruit of accessibility. That being said, this should be easier.
Comment #19
mgiffordComment #20
mgifford#12: drupal8.theme-image-attributes.999338-12.patch queued for re-testing.
Comment #21
mgiffordThis simplifies it based on @effulgentsia - but I do agree with Everett that defaulting to
alt=""
will make it harder to find broken alt text. That should be a conscious choice made every time.Comment #23
mgifford#21: drupal8.theme-image-attributes.999338-21.patch queued for re-testing.
Comment #24
mgifford#21: drupal8.theme-image-attributes.999338-21.patch queued for re-testing.
Comment #25
mgifford#21: drupal8.theme-image-attributes.999338-21.patch queued for re-testing.
Comment #26
mgifford#21: drupal8.theme-image-attributes.999338-21.patch queued for re-testing.
Comment #27
RobLoachNot sure about removing attributes if they arn't width, height, alt, or title. What if we want a data- attribute on an image?
Comment #28
mgiffordIf we want to add data then it would have to be added to this array first - array('width', 'height', 'alt', 'title')
This only unsets the variables defined with the array above.
Comment #29
shanly CreditAttribution: shanly commented#21: drupal8.theme-image-attributes.999338-21.patch queued for re-testing.
Comment #30
mgifford#21: drupal8.theme-image-attributes.999338-21.patch queued for re-testing.
Comment #31
mgifford#21: drupal8.theme-image-attributes.999338-21.patch queued for re-testing.
Comment #33
mgifford#21: drupal8.theme-image-attributes.999338-21.patch queued for re-testing.
Comment #34
mgiffordThis is a very simple patch. Also the test results were filled up with things like : mkdir(): No space left on device
Comment #36
mgifford#21: drupal8.theme-image-attributes.999338-21.patch queued for re-testing.
Comment #37
mgiffordThis failed last time with:
Link with label Test Operation: ;::]A$7A found in EntityOperationsTest.php Line 54
Not sure hwo this would affect it:
$this->assertLinkByHref($uri['path'] . '/test_operation');
Comment #38
bowersox CreditAttribution: bowersox commentedThe tests look like they pass again now on the patch in 21.
Comment #39
mgifford#12: drupal8.theme-image-attributes.999338-12.patch queued for re-testing.
Comment #40
mgifford#21: drupal8.theme-image-attributes.999338-21.patch queued for re-testing.
Comment #43
mgiffordComment #44
alarcombe CreditAttribution: alarcombe commentedFixed patch to reference core/includes/theme.inc rather than includes/theme.inc
Comment #45
alarcombe CreditAttribution: alarcombe commentedComment #47
alarcombe CreditAttribution: alarcombe commented44: theme_image_alt_attribute-999338-44.patch queued for re-testing.
Comment #49
mgiffordreroll.
Comment #50
alarcombe CreditAttribution: alarcombe commentedLet's get this slippery customer tested again before it needs another reroll!
Comment #52
lauriiiComment #53
lauriiiComment #54
mgiffordWe need a clear test to manually test this.
Comment #55
katiebot CreditAttribution: katiebot commentedWe're looking into this at DrupalCon Austin.
Comment #56
mlncn CreditAttribution: mlncn commentedWorking with Katie, verified the problem. This module provides working and non-working (in attribute array) examples: https://drupal.org/sandbox/mlncn/2281847
The first render array displays its alt text, the second does not:
Comment #57
mlncn CreditAttribution: mlncn commentedThe attached patch from comment #53 does not change anything (a nice self-contained argument that this issue needs tests).
In Drupal 8, instead of making alt a special case which is pre-set to empty string because it is required, we should pre-set it to NULL like title, uri, width, and height. We no longer use an XHTML 1.0 or HTML 4 doctype and there is no need to complicate our code attempting to adhere to the letter, not the spirit, of accessibility to conform to its entirely unenforced .
This comment appears twice in our code and can be removed entirely along with its ensuing complication.
The html5 specification (that last link) properly makes no distinction between a missing and an empty alt text:
"Except where otherwise specified, the alt attribute must be specified and its value must not be empty".
Even for xhtml 1.0, do any validators people use make this distinction? Could we get rid of this in 7 also? If not, i'm happy to make two different patches, one that's almost all removing silliness for 8, and a workaround of it for 7. But dropping the pre-set to empty string is what ezufelt calls for in comment #9.
Comment #58
mlncn CreditAttribution: mlncn commentedThis patch fixes the problem that alt cannot be passed in with $variables['attributes'] by removing the misguided attempt to set it to "" if it isn't present. Test forthcoming.
Comment #59
mlncn CreditAttribution: mlncn commentedComment #61
mgifford@mlncn I don't see how the patch in #10 resolves this problem.
HTML5 specs & WCAG 2.0 AA are different things. Our goal as a community is both, but for accessibility it is the latter.
The problem isn't as much of it coming up with
alt=""
as much as it not coming up withalt="New test alt"
- to use your earlier example.Good catch on the duplicate help text. There should be some reference though as it's 1000 lines of code after.
Comment #62
Sir-Arturio CreditAttribution: Sir-Arturio commentedHi.
I followed the basic Test Driven Development method to solve the problem.
First, I created an automated test based on @mlncn's #56 report/sandbox, which is included in my patch.
Then I created the minimal ammount of code that makes the test to pass. At least the tests in Drupal\image\Tests\ImageThemeFunctionTest do not break. Let's see what Drupal TestBot thinks...
So, my sollution solves the issue but does not take part in the wider discussion about should the shortcut variable alt default to NULL instead of ''.
Comment #63
Sir-Arturio CreditAttribution: Sir-Arturio commentedRemoving "Needs tests" tag as the automated tests have been implemented.
Comment #64
lauriiiPatch looks good!
We still need to implement a separated test patch which proves us that the tests are working. More information about Automated tests.
Comment #65
Sir-Arturio CreditAttribution: Sir-Arturio commentedHere you are. I also refactored the tests as well.
Comment #68
lauriiiI updated the commentings a bit.
Comment #69
mgifford@lauriii - Did you also "implement a separated test patch which proves us that the tests are working"?
Just wondering how close we're getting to RTBC and what other steps are required. Nice to see the bots like your patch.
Comment #70
lauriii@mgifford - I didn't create separated test patch because there's one available in comment #65. Do you think it's enough to prove the tests are working?
Comment #71
mgiffordI've got a test environment up here - http://s22557ad640c574b.s2.simplytest.me/
Although that's not all that useful in testing this.. I'll try to get some feedback on what would be required to prove the tests are working...
Comment #72
meichr CreditAttribution: meichr commentedI'm going to test this
Comment #73
meichr CreditAttribution: meichr commentedBy using the sandbox module referenced in comment #56 the error can be reproduced, while the first image has the correct ALT text applied, the second image has an empty ALT text:
The patch from comment #68 corrects the problem as now also the second image has the correct ALT text applied:
Comment #74
joelpittetDoc 80 character fix and micro optimization:
@see http://www.zomeoff.com/php-fast-way-to-determine-a-key-elements-existanc...
Also moved it into the existing IF for further optimization so it doesn't need to check the NULL array key if there is no value to put in there in the first place.
Summary: isset() is super fast! Like 65 times faster than array_key_exists(). So do that isset() check first.
Comment #75
joelpittetTypo, that patch is not right.
Comment #76
meichr CreditAttribution: meichr commentedConfirming like in #73 that patch at #75 also corrects the problem, the second image also has the correct ALT text applied:
Comment #77
webchickThis is pretty minor, but did confuse me in reading the test. $image_with_alt_attribute_not_working then later has an assertion that says the alt attribute actually IS working? I think we mean something there like:
$image_with_alt_property
and
$image_with_alt_attribute
Yes?
And then additionally, I don't actually see tests for this condition.
Comment #78
lauriiiFixed variable names and added test for #77.2
Comment #80
joelpittetAwesome! Thanks for test only patch!
Comment #81
alexpottThis looks like an unnecessary micro-optimisation we'd have to be doing thousands of these per request to notice the difference. Why not just
if (array_key_exists($key, $variables['attributes'])) {
Outputs:
So it is twice as slow as something that is really really fast - so it is still fast. Also see http://3v4l.org/hPpbU for comparison accross PHP versions.
Comment #82
lauriiiI removed that unnecessary isset.
Comment #83
joelpittet@alexpott regarding the micro-optimization. With a preprococess that gets called as much as this does, it is worth it.
see #74 for why I put it in. Also this optimization is in core in other places and I think @jessebeach walked me through it and convinced me it was the better way to IIRC.
From the article:
Comment #84
joelpittetre #81 I must have not seen those stats or you added them after, I did similar tests here and kinda came to the same conclusion, it's minimal.
The only thing that the isset() || array_key_exists() really does it make you ask your self as a developer, do you really need to have NULL keys because isset() does the exact same thing except it is FALSE on NULL.
I like the pattern because it highlights that decision.
Anyways, this is RTBC. Grievances aired, optimization minimal.
Comment #85
alexpottCommitted 6d8e333 and pushed to 8.0.x. Thanks!