diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index d568c851b23..5f2519d5339 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -1830,7 +1830,15 @@ class html_writer { public static function img($src, $alt, array $attributes = null) { $attributes = (array)$attributes; $attributes['src'] = $src; - $attributes['alt'] = $alt; + // In case a null alt text is provided, set it to an empty string. + $attributes['alt'] = $alt ?? ''; + if (array_key_exists('role', $attributes) && core_text::strtolower($attributes['role']) === 'presentation') { + // A presentation role is not necessary for the img tag. + // If a non-empty alt text is provided, the presentation role will conflict with the alt text. + // An empty alt text denotes a decorative image. The presence of a presentation role is redundant. + unset($attributes['role']); + debugging('The presentation role is not necessary for an img tag.', DEBUG_DEVELOPER); + } return self::empty_tag('img', $attributes); }