Skip to main content

Drupal image assist force a default alt tag

An article from ComputerMinds - Building with Drupal in the UK since 2005
8th Mar 2008

Mike Dixon

Senior Mind
Mike Dixon
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

We make heavy use of the Drupal image assist module (or img_assist as its known) - but find that its handling of empty alt tags is quite poor. The problem being that if when inserting the image the user does not specify a caption or description then the alt (and title) attributes are both empty.

This is clearly not ideal on a modern standards compliant site - luckily the solution to the image assist alt tag problem is a little theme override.


function phptemplate_image_display($node, $label, $url, $attributes) {
  if (!$node->title){
    $node_temp = node_load($node->nid);
    $node->title=$node_temp->title;
  }
  return theme('image', $url, $node->title, $node->title, $attributes, FALSE);
}

We do a quick check to see if the title of the node has been emptied (typically by the image assist module, but it could be from somewhere else) - and if it has we reload the node (don't worry, it's cheap as Drupal caches these for us) and reset our title. This title is then passed into the image theme and is used as the alt and title text for the image.

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.