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
| Comment | File | Size | Author |
|---|---|---|---|
| #27 | 1911876-nr-bot.txt | 1.86 KB | needs-review-queue-bot |
| #21 | 1911876-21.patch | 2.08 KB | hardik_patel_12 |
| #8 | drupal-imgaltplain-1911876-8.patch | 2.06 KB | mgifford |
| #2 | drupal-imgaltplain-1911876-2.patch | 2.15 KB | jasonlttl |
| #1 | drupal-imgaltplain-1911876-1.patch | 1.22 KB | jasonlttl |
Comments
Comment #1
jasonlttl commentedAttached is a patch for d7 (d8 coming later). This is more intended for reference by others. I doubt it would be a good idea to change the behavior of the d7 function unexpectedly.
Comment #2
jasonlttl commentedHere is a patch that adds this functionality to Drupal 8. It includes some tests as well. Let me know if there are any changes needed.
Comment #3
mgifford#2: drupal-imgaltplain-1911876-2.patch queued for re-testing.
Comment #4
mgiffordThis is an interesting idea. When is drupal_html_to_text() used? I do wonder how often the alt text is written in a way that it would make sense in a sentence. I expect it would benefit sighted people as much as the non-sighted if the alt text is written the right way.
Useful to consider though.
Comment #5
mgifford#2: drupal-imgaltplain-1911876-2.patch queued for re-testing.
Comment #6
mgifford2: drupal-imgaltplain-1911876-2.patch queued for re-testing.
Comment #7
Jalandhar commentedComment #8
mgiffordThank goodness for grep.. Bloody files moving about all over the place.
Thanks again @Jalandhar!
Comment #9
tibbsa commentedStill applies to 8.0.x.
This winds up getting called for all outgoing mail by default, from what I can tell. (If you're using PhpMail as the mailer, PhpMail::format() calls MailFormatHelper::htmlToText().) Other mailers might or might not do this (if they are sending MIME messages with plain-text parts, for example), but there seems to be some suggestion that one option would be to still rely on Drupal's implementation to produce plain-text versions, per the comment at MailInterface::format():
Comment #10
mgiffordThis is a feature request, so it's getting bumped to 8.1.
@tibbsa you're happy with the patch as it is now? We can try to bring it into 8.1.
Comment #20
jungledrupal_html_to_text()is nowMailFormatHelper::htmlToText()Comment #21
hardik_patel_12 commentedRe-rolling for 9.1.x , kindly review.
Comment #27
needs-review-queue-bot commentedThe 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.