Problem/Motivation
Images that are used exclusively as decorative content should render an empty alt
attribute to indicate this. (A missing alt attribute would be interpreted as omission.) Since the decision of whether an image from the Media Library is decorative depends on the context in which it is used, the form that governs the placement of the Media Library element should provide an ability to indicate that the image is decorative.
In short, the Media Library Form Element should provide a checkbox to designate Media as decorative in the same way that
https://www.drupal.org/project/decorative_image_widget β
does.
Proposed resolution
Add the ability for implementers of the Media Library Form Element to expose a checkbox. Leave it to the implementer to retrieve this data through the form state and save it as appropriate.
The implementer who wants to expose the checkbox for use would add two new attributes, #can_be_decorative
to expose the checkbox and is_decorative
to render the stored value. In this sense, the implementer is responsible for determining whether the specific implementation should expose the checkbox, rather than trying to determine that from which bundles are allowed.
$element['media'] = [
'#type' => 'media_library',
'#allowed_bundles' => $allowed_bundles,
'#delta' => $delta,
'#description' => $this->t('The recommended minimum image width is 500px wide'),
'#cardinality' => 1,
'#title' => $this->t('Media'),
'#default_value' => $item->media,
'#can_be_decorative' => TRUE,
'#is_decorative' => $item->is_decorative,
];
The implementer would then examine the form_state upon submit (WidgetBase:getWidgetState()
could retrieve that and the value obtained from ['widget'][0]['media']['is_decorative']['#value']
, and saved as appropriate for the implementer.
Alternatives considered
An alternative would be to rework the return value of the Media Library Form Element, along the lines of:
[
'media' => '',
'decorative' => ''
]
... but this would be a fundamental API change and is probably not worth it for this enhancement.