Skip empty alt and title attributes

Created on 13 February 2017, over 7 years ago
Updated 19 September 2023, 9 months ago

Problem/Motivation

The module always adds "alt" and "title" attributes for an image tag, even they are empty. I propose to skip these attributes if they are empty.

Proposed resolution

The easiest way is to update the theme_image_srcset() function. But I'm not sure that this update will not create other issues.
Another way is to refactor the theme_picture() (don't set 'alt' and 'title' keys if they are empty):

  if (variable_get('picture_fallback_method', 'src') === 'src') {
      $min_ie9_fallback = array(
        '#theme' => 'image_srcset',
        '#uri' => $src,
        '#alt' => isset($attributes['alt']) ? $attributes['alt'] : '',
        '#title' => isset($attributes['title']) ? $attributes['title'] : '',
        '#attributes' => array_diff_key($attributes, array('alt' => '', 'title' => '')),
      );
      $output[] = drupal_render($min_ie9_fallback);
    }
    else {
      // Fallback image for < IE9.
      $min_ie9_fallback = array(
        '#theme' => 'image_srcset',
        '#uri' => $src,
        '#alt' => isset($attributes['alt']) ? $attributes['alt'] : '',
        '#title' => isset($attributes['title']) ? $attributes['title'] : '',
        '#attributes' => array_diff_key($attributes, array('alt' => '', 'title' => '')),
      );
      $output[] = '<!--[if lt IE 9]>';
      $output[] = drupal_render($min_ie9_fallback);
      $output[] = '<![endif]-->';

      // Fallback image for > IE8.
      $srcset = array(
        'uri' => $src,
      );
      $dimensions_clone = $dimensions;
      picture_get_image_dimensions($variables['style_name'], $dimensions_clone);
      if (!empty($dimensions_clone['width'])) {
        $srcset['width'] = $dimensions_clone['width'] . 'w';
      }
      $plus_ie8_fallback = array(
        '#theme' => 'image_srcset',
        '#srcset' => array($srcset),
        '#alt' => isset($attributes['alt']) ? $attributes['alt'] : '',
        '#title' => isset($attributes['title']) ? $attributes['title'] : '',
        '#attributes' => array_diff_key($attributes, array('alt' => '', 'title' => '')),
      );
      $output[] = '<!--[if !lt IE 9]><!-->';
      $output[] = drupal_render($plus_ie8_fallback);
      $output[] = '<!-- <![endif]-->';
    }
✨ Feature request
Status

Closed: outdated

Version

2.0

Component

Code

Created by

πŸ‡¬πŸ‡ͺGeorgia Chalk

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024