Media using view modes with numerical machine names revert to default when editing a node

Created on 20 December 2023, 6 months ago
Updated 18 January 2024, 5 months ago

Problem/Motivation

When editing an existing node that uses CKEditor 5, if the node previously had media that was displayed in a view mode with a numerical machine name, its view mode will be stripped away and it will revert to whatever the default view mode is. This does not occur with non-numerical view modes.

Steps to reproduce

  1. Spin up a minimal Drupal install
  2. Install CKEditor 5, Field UI, Media, Media Library, Text Editor, Views, File, and Image
  3. Create a Media Type and assign the Media Source to Image
  4. Create a View Mode for Media and give it a numerical name (e.g. 15). Enable this view mode for Image Media Types.
  5. Create a new Text Editor that uses CKEditor 5
    1. Include the Drupal Media button
    2. Enable the Embed media checkbox
    3. Enable the "Allow the user to override the default view mode" checkbox
    4. Enable at least 2 View Modes in the "View modes selectable in the 'Edit Media' dialog"
  6. Create a Content Type that has a body field
  7. Create a node and use the Media Library to upload an image. Set this image view mode to the numerically named View Mode.
  8. Save the page
  9. Edit the page and verify that the view mode has been reverted back to "default"
  10. Extra: Change the view mode to a non-numerical option (e.g. Media Library). Save the page, edit the page, and verify that the view mode stays

Proposed resolution

I was able to resolve by modifying web/core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Media.php to ensure $view_mode is a string and not an integer. This seems to resolve any underlying issues with CKEditor 5 PHP or JS. I've provided a patch file with the change.

Remaining tasks

(Per wim-leers in Slack) - Test coverage that fails, probably by extending \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testViewMode()

🐛 Bug report
Status

Active

Version

10.2

Component
CKEditor 5 

Last updated about 3 hours ago

Created by

🇺🇸United States hummeldon

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

Comments & Activities

Production build 0.69.0 2024