I am using mytheme_image_attach_body() to theme images attached for certain node types. One of the reasons I did this was to change the default values of the alt and title attributes from using the image node title to the parent node title.
In the simple case, when no third parameter is passed to image_display() then the alt and title are set correctly from the image node (screen grab 1)
I added array('alt' => $node->title, 'title' => $node->title) as the third parameter to my call to image_display, but then noticed that the generated img tag had these values duplicated. Whilst most browsers will cope with this, I am not sure that it is good html, and it looks confusing. The problem was in theme_image_display() which creates $alt and $title and passes them to theme('image", ...) which then calls theme_image(). Theme_image() uses $alt and $title directly in the img tag, but also uses all the values from the $atttributes array. So this caused the duplication (screen grab 2)
The solution is to unset $attributes['alt'] and $attributes['title'] in theme_image_display() as we know at this point that any given values will now be stored in $alt and $title. Screen grab 3 shows the result of this change, with only one set of ALT and TITLE tags.
Jonathan
Comment | File | Size | Author |
---|---|---|---|
#1 | _824324.image_.repeated_attributes.D6.patch | 610 bytes | jonathan1055 |
Comments
Comment #1
jonathan1055 CreditAttribution: jonathan1055 commentedHere is the patch, against beta5
Comment #2
joachim CreditAttribution: joachim commentedI'm afraid theme_image_attach_body() has been changed in the dev version -- take a look and see if that needs the same fix.
Comment #3
jonathan1055 CreditAttribution: jonathan1055 commentedHi,
I've followed it through and altough theme_image_attach_body() and theme_image_attach_teaser() in beta5 have been replaced with theme_image_attach_node_attached() in the latest dev, the call to image_display() and ultimately theme_image_display() is the same, so the correction to theme_image_display() I described above is still valid.
I think you could make a claim that it is actually a fault in theme_image() in theme.inc, as this is where the ALT and TITLE attributes are added to the <img> tag followed by everything returned from drupal_attributes($attributes) which causes the doubling up. It may be more correct to fix it there, so that any module which calls theme_image() with ALT and TITLE in the $attributes array does not get duplicated values in the html. But I doubt if we can get theme_image() corrected so I think it is worth fixing Image module'e theme_image_display().
The patch in #1 still applies cleanly to the 2010-07-11 dev release.
Jonathan
Comment #4
joachim CreditAttribution: joachim commentedFixed.
#824324 by jonathan1055: Fixed theme function allowing duplicate ALT and TITLE in IMG tag.
As far as I can tell, theme_image on D7 has the same problem -- you might want to file an issue on core for that.
Comment #5
jonathan1055 CreditAttribution: jonathan1055 commentedThanks for the commit.
Actually I think D7 theme_image() is OK. I've cut out just the important bits:
If both $variables['alt'] and $variables['attributes']['alt'] are defined then the $variables['alt'] value takes precedence because $attributes['alt'] is updated to this value. The IMG tag only takes values from $attributes, so there is no duplication.
The D6 version is:
which is where the duplication arises, because ALT appears on its own and can also be taken from $attributes.
As it is fixed in D7 do you think it is still worth raising a core D6 fix? Everyone is working hard for D7 and I don't want to make work if it would be viewed as wasting time and not worth fixing.
Comment #6
joachim CreditAttribution: joachim commented> As it is fixed in D7 do you think it is still worth raising a core D6 fix?
If you file it as a patch, and explain in the issue that it's fixed in D7 and could do to be fixed in D6 too, and also it won't break anyone's existing themes -- in other words, make it easy to understand why it's a Good Thing, then I imagine it'll get committed.