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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jonathan1055’s picture

Status: Active » Needs review
FileSize
610 bytes

Here is the patch, against beta5

joachim’s picture

Status: Needs review » Needs work

I'm afraid theme_image_attach_body() has been changed in the dev version -- take a look and see if that needs the same fix.

jonathan1055’s picture

Status: Needs work » Needs review

Hi,
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

joachim’s picture

Status: Needs review » Fixed

Fixed.

#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.

jonathan1055’s picture

Thanks for the commit.

Actually I think D7 theme_image() is OK. I've cut out just the important bits:

function theme_image($variables) {
  ...
  $alt = $variables['alt'];
  $title = $variables['title'];
  $attributes = $variables['attributes'];
  ...
    if (isset($alt)) {
      $attributes['alt'] = $alt;
    }
    if (isset($title)) {
      $attributes['title'] = $title;
    }
    ...
    return '<img' . drupal_attributes($attributes) . ' />';
  }
}

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:

function theme_image($path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  ...
    $attributes = drupal_attributes($attributes);
    return '<img src="'. check_url($url) .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. (isset($image_attributes) ? $image_attributes : '') . $attributes .' />';
}

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.

joachim’s picture

> 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.

Status: Fixed » Closed (fixed)

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