The title attribute is now available when rendering images via BuildFieldTrait helper methods.
What changed:
- getMediaImageAndAlt() now includes a title key in its return array, set to the media entity's label.
- getImageAndAlt() now includes a title key in its return array, set to the entity's label.
- buildImage() and buildImageStyle() accept a new optional $include_title parameter (defaults to FALSE). When TRUE, the rendered image element includes a #title attribute.
- buildMediaResponsiveImage() now always renders the title attribute on responsive images, set to the entity's label.
How to update your code:
No changes are required. The $include_title parameter defaults to FALSE, so existing calls to buildImage() and buildImageStyle() continue to work without modification.
To opt in to title attributes:
// Before (no title attribute):
$build[] = $this->buildImage($entity);
$build[] = $this->buildImageStyle($entity, 'thumbnail');
// After (with title attribute):
$build[] = $this->buildImage($entity, 'field_image', TRUE);
$build[] = $this->buildImageStyle($entity, 'thumbnail', 'field_image', TRUE);
If you consume the return value of getMediaImageAndAlt() or getImageAndAlt() directly, a new title key is now available in the returned array.