MailFormatHelper::htmlToText() img/alt handling

Created on 8 February 2013, over 11 years ago
Updated 30 January 2023, over 1 year ago

Problem/Motivation

The drupal_html_to_text function removes images throwing away the alternate text. This is arguably bad from an accessibility perspective. Images could contain semantically meaningful information which is expressed in the alt attributes. In addition, I did a quick comparison and mailchimp appears to add alt attributes from images to their plain text versions by default. It is a little different with them as their plain text is editable. However, if the industry leader is doing it by default, it's probably a good idea.

I suggest replacing images with alt text when alt text exists. Below is one proposed solution (patch coming d7/d8).

include/mail.inc (d7)

401: $supported_tags = array('a', 'em', 'i', 'strong', 'b', 'br', 'p', 'blockquote', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'img');
...
414:  // Replace images with alternate text
415:  $string = preg_replace('@<img[^>]+?alt="([^"]*)"[^>]*?>@i', '$1', $string);

Note, there is a related 250 commenter, but it seems to be inactive (2011) and didn't appear to mention image/alt handling.
See #299138: Improve \Drupal\Core\Utility\Mail::htmlToText()

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Feature request
Status

Needs work

Version

10.1

Component
Mail 

Last updated 21 days ago

No maintainer
Created by

🇺🇸United States jasonlttl

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.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Production build 0.71.5 2024