Deleting a Media view mode should not result in the deletion of an entire text format

Created on 11 December 2022, over 2 years ago
Updated 28 September 2023, almost 2 years ago

Problem/Motivation

Allowing the user to customize the media view modes that the user can select in the WYSIWYG unexpectedly creates a hard configuration dependency on the media view mode for the HTML format itself, even if the view mode is never used.

Steps to reproduce

  1. Install Drupal 9.5.x HEAD with the Standard profile.
  2. Enable the Media and Media Library modules.
  3. On /admin/structure/display-modes/view/add/media/, create a new view mode for Media called "Test" and click save.
  4. On /admin/structure/media/manage/image/display, under "Custom display settings", check "Test" and click save.
  5. On /admin/config/content/formats/manage/basic_html, add the media button to the WYSIWYG toolbar.
  6. Further down the page, check "Embed media".
  7. Further down still, open the "Embed media" vertical tab that becomes available.
  8. Under "View modes selectable in the 'Edit media' dialog", check "Default" and "Test". Click "Save".
  9. Go to /admin/structure/display-modes/view/manage/media.test/delete. It warns you that not only the unused view mode but also the editor config and the basic HTML input format are about to be deleted:
  10. If you don't read carefully, you may end up deleting the entire Basic HTML format, rendering your content both broken and uneditable in the process.

Proposed resolution

The selectable view modes for the media embed button should be treated as optional third-party settings that can be cleaned up from the editor and filter config entities, especially if they are never used. (Even if they are used, it'd be better to ignore a single data attribute rather than break the entire site's content.)

Remaining tasks

TBD

User interface changes

TBD

API changes

TBD

Data model changes

TBD

Release notes snippet

TBD

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Media 

Last updated about 3 hours ago

Created by

🇺🇸United States xjm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India aditi saraf

    i am working on this .

  • Issue was unassigned.
  • Status changed to Needs review almost 2 years ago
  • 🇮🇳India aditi saraf

    When i keep the checkbox Allow the user to override the default view mode checked i am not getting the error .

  • Status changed to Needs work almost 2 years ago
  • 🇺🇸United States smustgrave

    Following the steps of the IS can confirm the bug still on 11.x

  • First commit to issue fork.
  • Pipeline finished with Failed
    about 1 month ago
    Total: 3015s
    #525599
  • Pushed up MR 12404 with a POC of a different approach, described as follows:

    • Views has the interface DependentWithRemovalPluginInterface, with an onDependencyRemoval() method. When dependencies of a View entity are removed, View::onDependencyRemoval() checks each of the view's displays to see whether any views handler plugins implement the interface and method. If onDependencyRemoval() in the implementing plugin returns TRUE, then the handler plugin configuration is removed from the view and prevents the dependency removal from deleting the view config entity
    • The idea here is to create a similar interface and method in the Core namespace, and that method should be invoked any implementing plugin in a config entity's plugin collection, when a dependency of the config entity is removed
    • The core version of DependentWithRemovalPluginInterface::onDependencyRemoval() is slightly different from the Views version. Instead returning a boolean of whether the plugin should be removed or not, it returns an it, which is 1 of 3 values: -1 if the plugin settings have changed, 0 if the plugin settings have not changed, or 1 if the plugin should be removed from the entity's plugin collection. (This could possibly be an enum?)
    • In ConfigEntityBase::onDependencyRemoval(), if the entity is an instance of EntityWithPluginCollectionInterface, then any plugin in any plugin collection that implements DependentWithRemovalPluginInterface will have its onDependencyRemoval() invoked, and the return value is reacted on appropriately
    • To address this issue specifically, the MediaEmbed filter is updated to implement DependentWithRemovalPluginInterface, and in onDependencyRemoval(), if any media view mode is being deleted and is present in the plugin's "allowed_view_modes" or "default_view_mode" settings, those settings are updated to remove that view mode and set to use the "default" view mode as needed. The method returns that the plugin settings have been changed, so ConfigEntityBase::onDependencyRemoval() returns TRUE. The dependencies for the filter format are then recalculated and should prevent it from being deleted when the media view mode is deleted

    The new MR also includes the test from MR 3115
    (The Build tests has failed consecutively a few times, but I can't identify any related reason in this MR, so just going to let that sit for a bit.)
    Moving to Needs Work for this POC and soliciting feedback for this change to config entity subsystem.

    ensure that any media already embedded with a removed view mode falls back to the default view mode

    Is this still something we want to do? Right now MediaEmbed renders an error "The referenced media source is missing and needs to be re-embedded" for media embedded with invalid view modes, so it would be a change in behavior to make it show in the default view mode as fallback instead.

  • First commit to issue fork.
  • 🇬🇧United Kingdom oily Greater London

    oily changed the visibility of the branch 3326447-fail to hidden.

  • Pipeline finished with Success
    about 1 month ago
    Total: 35921s
    #525709
  • 🇫🇷France mably

    In our case, we validated the display mode configuration deletion a bit too fast and completely lost the data related to several fields in various entity types. Hopefully we had daily backups.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 435s
    #528074
  • Moving to Needs Review to get feedback on PoC MR. Also, there's functional test coverage for the specific issue, but will probably need additional tests for the new Interface/API introduced if we go with the approach.

  • Looks like my proposed resolution in #16 overlaps a lot with 🐛 Config entities implementing EntityWithPluginCollectionInterface should ask the plugins to react when their dependencies are removed Needs work and might make this a duplicate of that.

  • 🇦🇹Austria fago Vienna

    I also ran into this and opened 📌 Deleting filter formats may result in data loss Active for issue I faced as consequence: deletion of text item fields that have the text format as "allowed_format". I do think we should fix both, this issue and 📌 Deleting filter formats may result in data loss Active .

    Checking on the proposed solutions I think both are viable. I agree that MR 12404 is the best approach though. Since it's a generally new feature, I guess it's best done over at [#579743]?

  • 🇺🇸United States xjm

    Agreed @fago; any issue that implements a fix for this can be treated as a critical UX bugfix (even if it requires a feature or API addition).

Production build 0.71.5 2024