0

I have a page view for image gallery. It has only 1 Photo field of Photo which has a field of type Image. In view it use "Lightbox2: lightshow: small-square -> large" as formatter. small-square is a custom image style.

The rendered page doesn't show any alt tag:

<img typeof="foaf:Image" src="http://www.example.com/sites/default/files/styles/small-square/public/6486388587_61846b6d84_1323732945.jpg" width="240" height="240">

I need to add alt tag to images. What do I have to change?

My drupal installation has these versions:

  • Drupal 7.10
  • Lightbox2 7.x-1.0-beta1
  • Views 7.x-3.0-rc3

looking at Lightbox2 modules's file lightbox2.formatter.inc it seems that alt tab should be forced to show, using image title.

  // Set up the title.
  $image_title = $item_data['description'];
  $image_title = (!empty($image_title) ? $image_title : $item_data['title']);
  $image_title = (!empty($image_title) ? $image_title : $item_data['alt']);
  if (empty($image_title) || variable_get('lightbox2_imagefield_use_node_title', FALSE)) {
    $node = node_load($node->nid);
    $image_title = $node->title;
  }

  $image_tag_title = '';
  if (!empty($item_data['title'])) {
    $image_tag_title = $item_data['title'];
  }


  // Enforce image alt.
  if (!empty($item_data['alt'])) {
    $image_tag_alt = $item_data['alt'];
  }
  elseif (!empty($image_title)) {
    $image_tag_alt = $image_title;
  }  $image_tag_alt = '';
  if (!empty($item_data['alt'])) {
    $image_tag_alt = $item_data['alt'];
  }
  elseif (!empty($image_title)) {
    $image_tag_alt = $image_title;
  }

So... where is the error?

  • Did you try doing a temporary hack to the code, changing the instances of empty strings with a test string? That could be helpful to check if that is the actual code being executed. – Jedihe Dec 15 '11 at 13:01
  • It seems due to a missing $item['alt'] value in function theme_lightbox2_image($variables) in lightbox2/lightbox2.formatter.inc – Alessandro Pezzato Dec 20 '11 at 10:06
1

For people coming to this page...I wanted to use the title of the node containing the image as the caption on the lightbox2 image, which I was displaying in a view, but I didn't want to modify the module, which I think is the issue with the above technique.

What I ended up doing was making my own image formatter. Basically I cut and pasted the hook_field_formatter_info and hook_field_formatter_view from the lightbox2 module and modified them. There may be a better way, but it works, solved my problem, and I can upgrade lightbox2 without worrying about breaking my site.

/**
 * Implementation of hook_field_formatter_info().
 */
function mymod_field_formatter_info() {
  $formatters = array();
  if (module_exists('image')) {
    $formatters["mymod_custom_formatter"] = array( 
      "label" => "Field formatter for my Photos",
      "field types" => array("image"),
    );
  }
  return $formatters;
}

/**
 * Implements hook_field_formatter_view().
 */
function mymod_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $pieces = explode('__', "lightbox2__lightbox__thumbnail__large"); // forced this, you could do something trickier, but it solved my issue

  ... untouched ...
  foreach ($items as $delta => $item) {
    // here is the bit I added which sets the node title to be the image caption
    if ($entity_type == 'node') {
      $item["title"] = $entity->title;
    }

    $uri = array(
    ... etc ...
  • This seems an elegant solution. I'll try it :) – Alessandro Pezzato May 10 '12 at 15:09
0

I solved this adding a patch to lightbox2/lightbox2.formatter.inc. This get title from node if image's title/alt is not defined.

function theme_lightbox2_image($variables) {
  $path = $variables['path'];
  $item = $variables['item'];
  $image_style = $variables['image_style'];
  $node_id = $variables['node_id'];
  $field_name = $variables['field_name'];

  // PATCH Start
  if (!isset($item['title'])) {
      $node = node_load($variables['node_id']);
      $item['title'] = $node->title;
  }
  if (!isset($item['alt'])) {
      $item['alt'] = $item['title'];
  }
  // PATCH End

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.