Problem/Motivation
The new MediaEmbed functionality added in 8.8.0 incorrectly conflates a text format's MediaEmbed default view mode with the view mode selected by an editor when embedding the media. If the default view mode for the text format is changed, this will result in corruption of data. The implicit relationship with the embeds using the previous default view mode is now broken, and all of the previously embedded media that used the old embed are now displaying the new default view mode.
If an editor embeds media and selects what a site builder has designated as the default view mode, then that data (the 'data-view-mode' attribute) is stripped from the <drupal-media>
element. The editor is unaware this happens. From their point of view, they selected a view mode and expect that selection to be honored; however, from this point forward, MediaEmbed assumes that if a <drupal-media>
element lacks a 'data-view-mode' attribute, then the view mode must be the current default view mode.
The following comment from \Drupal\media\Form\EditorMediaDialog::buildForm()
pretty much sums it up:
// Store the default from the MediaEmbed filter, so that if the selected
// view mode matches the default, we can drop the 'data-view-mode'
// attribute.
Here's the catch. If that default view mode changes, then piece of embedded media that used the prior default view mode will now use the new default view mode. The editor's view mode selection has been disregarded and their original selection is unrecoverable.
Setting priority to critical as this bug corrupts user data.
See below for steps to reproduce:
- Install Drupal 8.8.2-dev with Standard install profile.
- Enable Media and Media Library.
- Add a new Media view mode: 'Image'.
- For 'Image' media bundle, enable custom display settings for the 'Image' view mode.
- (Note that the default view mode will render a 480x480 image.)
- For the 'Image' view mode for the 'Image' media bundle, configure the view mode to render a 220x220 image.
- Edit the 'Basic HTML' text format/editor.
- Add the Media Embed button to the toolbar and enable the 'Embed media' filter.
- For the 'Embed media' filter, configure it as follows:
- Default view mode: Image
- Media types selectable in the Media Library: Image
- View modes selectable in the 'Edit media' dialog: Default, Image, Media Library
- Save format/editor configuration.
- Upload an image to the Media Library.
- Create a new Basic page node.
- Embed the image.
- Observe that the default embed is using the 'Image' view mode (e.g. 220x220px).
- Use browser inspector tool to inspect the element and observe the 'data-view-mode' attribute is not present.
- Click on 'Edit media' and change the view mode to 'Default'.
- Use browser inspector tool to inspect the element and observe the 'data-view-mode' attribute is present and has a value of 'default'.
- Click on 'Edit media' and change the view mode back to 'Image'.
- Save the node.
- Edit the 'Basic HTML' text format/editor.
- Change the 'Default view mode' to 'Default'.
- Save format/editor configuration.
- Reload the Basic page. Notice that the image is now being rendered 480x480px and not 220x220px.
Proposed resolution
Stop removing 'data-view-mode' from <drupal-media>
elements.
This bug is already out in the wild, so we'll need to figure out how to address existing installs. Right now, there are sites out there with <drupal-media>
elements, tucked away in various fields, without a 'data-view-mode' attribute. I assume we'll need to use hook_update_N()
to update those <drupal-media>
elements.
Remaining tasks
User interface changes
None
API changes
None.
Data model changes
None.
Release notes snippet
There will certainly need to be something.