Problem/Motivation
Media in CKEditor4 has an edit media
which let's user choose the view mode. This is not yet available in CKEditor 5.
This will allow us to port \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testViewMode().
Note that ideally, we would not use a Drupal dialog for this, and instead implement it on the client side, much like #3246385: [drupalMedia] Support captions on <drupal-media> for captioning and alignment.
Proposed resolution
TBD
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #60 | 3245720-60-93x.patch | 159.22 KB | wim leers |
| #53 | configured view mode removed.mov | 4.1 MB | wim leers |
| #48 | 3245720-48-93x.patch | 159.6 KB | bnjmnm |
| #40 | 3245720-40-93x.patch | 158.44 KB | bnjmnm |
| #40 | 3245720-40-94x.patch | 159.6 KB | bnjmnm |
Issue fork drupal-3245720
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
wim leersLet's do this after #3246385: [drupalMedia] Support captions on <drupal-media>.
Comment #3
wim leersComment #4
wim leersComment #6
hooroomooComment #7
wim leers#3260554: [drupalMedia] Support alignment on <drupal-media> landed. This can happen alongside #3246385: [drupalMedia] Support captions on <drupal-media>.
Comment #9
hooroomooCurrently I have the view modes I have on my site hard coded and the options show up as balloon buttons in the UI.
Comment #10
wim leersProgress! 🥳 :D
Comment #11
hooroomooNote to self: toolbar dropdown currently commented out to avoid breaking the site
Comment #12
hooroomooCurrent behavior: When clicking "tiny mode" button, the model and view updates correctly, but the command does not. HOWEVER, when I then click one of the align buttons, the command tab updates and will show both the tiny and the proper align as shown in the screenshot. In either case, the image preview doesn't change to the chosen view mode.
One of my guesses is that it has something to do with the if condition inside of the execute method in DrupalElementStyleCommand.js.
When I change to a different align type, it hits the "writer.removeAttribute(modelGroupName, element)" line inside the if condition but it does not hit that line for when I change to a different view mode type.
Comment #14
hooroomooThings left to do:
1. dynamically generate config in Media.php (include bundles)
2. add a 'none' view mode
3. write tests
4. fix code so custom commands pass
Comment #15
hooroomoo1. Currently, all the view modes appear in the dropdown regardless of bundle type. It's being dynamically generated in Media.php now, but I am not sure how to generate the toolbar items based which selected bundle/content the toolbar is associated with.
(Screenshot at bottom with a dropdown on a media video)
2. I added a 'no view mode' but there is also an existing 'default view mode'. The 'no view mode' adds nothing to the model nor the view nor the command. The 'default view mode' adds the 'default' value to the model, view, and command. Should I keep this 'no view mode' or would the default view mode suffice?
3. I've gotten several "iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations no-restricted-syntax" because I'm using a lot of for loops. I suppressed all those complaints in the code for now. Is it fine to keep the suppressions on these for loops or should I eventually fix them?
Comment #16
wim leersJust wanted to say: LOVE that demo screenshot 🤩🤣🤣👏
Comment #17
wim leers#15:
\Drupal\ckeditor5\Controller\CKEditor5MediaController::mediaEntityMetadata()to return the bundle from the server side. #3262492: Refactor isMediaUrl to more generic API that supplies frontend metadata about media entities recently refactored that already, and we specifically ensured that adding this would be simple. See #3262492-9: Refactor isMediaUrl to more generic API that supplies frontend metadata about media entities. You can see which other files (in particular JS files) that that issue/MR modified; those are probably all the pointers you need to figure this out 😊🤓data-view-modeattribute. We need to do that to match the behavior of the filter. See\Drupal\media\Plugin\Filter\MediaEmbed::settingsForm(), which says:'#description' => $this->t('The view mode that an embedded media item should be displayed in by default. This can be overridden using the <code>data-view-modeattribute.'),eslint-disable-*lines by doing some refactoring: create named functions for parts of the logic, then call those. That tends to improve legibility of the code too!P.S.: I'm getting PHP notices like this, because an array index that does not exist is being accessed:
Comment #18
hooroomoo1. Having trouble getting site back to normal after merging new alignment work.
2. I have not tested thoroughly yet but the list dropdowns are bundle specific now and hides the appropriate view modes buttons. But the case of a view mode that is not enabled for any bundle still needs to be handled.
Comment #19
hooroomooThe main change I made today was I followed the pattern used in mediaimagetextalternativeediting.js and added a listener for inserting content in drupalelementstyleui.js Inside the listener, I upcast the 'drupalMediaBundle' so it has the attribute. However when I check the bundleType inside toggleButtonVisibility() it's undefined. The function too fast before the attribute is set idk.
I didn't get to fixing the commands that stopped working after merging new changes but if that can be looked at too, that'd be great.
Comment #20
hooroomooGetting this error when trying to run what is there so far for \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testViewMode.
Comment #21
hooroomooComment #22
hooroomoo@lauriii Is there a way to create Media to the same node inside of my view mode test instead of inside of the setup() in MediaTest.php? I added created another Media inside of setup() to test different dropdown items for testViewModeDifferentBundles() but it seems to be affecting some of the other tests because this new media exists in every test now.
Also, still some test failures but the view mode tests pass and addressed all feedback.
Comment #23
wim leersFirst review round. More to come — still digging into the details, but what I posted so far will already help make this MR easier to review 😊
Comment #24
wim leersLooked at this in more detail just now with @hooroomoo. This is going to make the scope expand too much. I told them to just expect a bigger superset for now — that's definitely not causing data loss issues anyway. We'll make the upgrade path perfect (i.e. more narrow; no more superset) in #3269657: [drupalMedia] The CKEditor 4 → 5 upgrade path for the media_embed filter should not forcefully allow the `data-view-mode` attribute on `<drupal-media>`.
Comment #25
wim leersI can confirm this MR is porting
::testViewMode(). It is not yet porting::testDialogAccess(), and that actually makes sense — that does not actually test anything related to media embeds' view modes. Will investigate more closely.But in any case, that means I can remove the tag already 😊
Comment #26
hooroomooWill apply new review feedback tomorrow.
Remaining failing tests I am stumped on:
1) Drupal\Tests\ckeditor5\FunctionalJavascript\MediaLibraryTest::testButton
Image doesn't insert.
2) Drupal\Tests\ckeditor5\FunctionalJavascript\MediaLibraryTest::testAlt
I think same problem of image not inserting.
3) Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testViewModeDifferentBundles
Passes locally, fails on CI :(
4) Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testAlignment
Fails when checking if the default 'Break text' button is selected on insertion. After inserting an image on my site manually, the break text balloon button is selected, but not in the test.
Comment #27
hooroomooComment #28
wim leersEpic progress here! Passing tests now and all feedback addressed.
I did a quick round of review of the changes you made, and have only a few new remarks.
I still need to finish reviewing the test coverage in detail, will get that done today!
Comment #29
hooroomooWill address test feedback and the failing tests tomorrow
Comment #30
wim leersThanks for https://git.drupalcode.org/project/drupal/-/merge_requests/1796/diffs?co..., that made the logic for generating metadata in PHP for the JS to consume slightly easier to understand. 👍
But it's still too hard too understand IMHO, and that makes it very likely that we have bugs (and suboptimal code).
So I started digging into it and found:
Once you've addressed those 3 things, I think it'll become far simpler to simplify the code further/break it down into easier to understand functions that each do only one thing :)
Comment #31
hooroomooAll feedback should be addressed now
Comment #32
hooroomooAll feedback should be addressed now
Comment #33
wim leers#3270765: Add test coverage for createDropdown in drupalElementStyles landed, which means the path is paved for this MR! 😊
Comment #35
lauriiiI think this is now ready for a review from another committer. I have been working on this along @hooroomoo, but all of the heavy lifting is done by them. They have been also reviewing my changes along the way, and we've received additional reviews from @Wim Leers as well. So from my perspective: 🚢🚀
We will still have to generate 10.0.x patch for this, but I don't feel that should block the RTBC.
Comment #36
bnjmnmI provided feedback in the MR. Most/all of it should be easy to address.
Comment #38
lauriiiComment #39
wim leers\Drupal\Tests\media\FunctionalJavascript\CKEditorIntegrationTest::testViewMode()(CKE4) with the new\Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testViewMode()(CKE5), and it's now definitely test coverage that is as comprehensive as we had for CKEditor 4. 👍🥳 I also couldn't spot any mistakes.\Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testDialogAccess()was created as the CKE5 equivalent ofActually, this is still necessary — for example, when\Drupal\Tests\media\FunctionalJavascript\CKEditorIntegrationTest::testDialogAccess(), but it's irrelevant. It was a stub that was added months ago, before the CKE5 Media integration had been finalized. Now it has.EditorMediaDialogis not being used by CKEditor 5, hence there's also no need to test it.filter_captionis disabled, the "toggle caption" toolbar button should disappear. This works, but test coverage for it is still missing. But in the interest of finishing this mammoth issue, let's do that in a follow-up. Created #3275120: [drupalMedia] alt_field setting on "Image" media not respected for that.Comment #40
bnjmnmComment #43
wim leersLayoutBuilderQuickEditTestMediaTest::testDrupalMediaStyleInDropdownMarking RTBC again because the tests for 10 just passed 👍🥳
Comment #47
bnjmnmCommitted to 10.0.x and 9.4.x. I reproduced the 9.3.x test failures locally, so that needs to be addressed before a backport can happen.
Comment #48
bnjmnmckeditor5_drupalelementstyle_test.module was missing changes to get it current with the rest of the patch. Lets see if this gets 9.3.x passing.
Comment #49
wim leers🥳🚢
Comment #50
catchOne question: what happens is media is embedded with a specific view mode, and that view mode is later removed from the site? Do we just fall back to default when rendering? And what happens when you edit the content again?
Comment #51
lauriii#50: The
data-view-modeattribute would be removed when the element is upcast in CKEditor 5, which would result in the embedded media being rendered in the default view mode.Comment #52
wim leers#50: To add to what #51 wrote: that really ought to be prevented by using config dependencies. The
FilterFormatconfig entity definitely does not handle config dependencies correctly yet 😢 It only handles removing filter plugins correctly (see\Drupal\filter\Entity\FilterFormat::onDependencyRemoval()). Heck, it does not even handleRoledependencies correctly yet! See #2569741: [PP-1] FilterFormat should add the roles as config dependencies.EDIT: oh yay, fortunately view modes are handled correctly! Yay for us learning from past mistakes! See
\Drupal\media\Plugin\Filter\MediaEmbed::calculateDependencies()— I just confirmed that it results in something like this:👍
Comment #53
wim leersAlso, after posting #52 I realized that was not quite what @catch was asking, although it is related.
@lauriii's response is accurate.
FilterFormathandles config dependencies getting removed… but definitely out of scope here.Basically, we need to expand
\Drupal\filter\Entity\FilterFormat::onDependencyRemoval(), like #2348925: Uninstalling a filter plugin removes text formats did.Wanted to create an issue for this, but apparently this is a pretty fundamental problem. See #2575547: Formalize patterns for delete/uninstall of configuration dependencies, #2773205: Come up with a design for highly destructive operations in confirm forms, #2912466: Warning about deleting configuration is easy to overlook. Worse, the config dependency system's architecture in
\Drupal\Component\Plugin\DependentPluginInterfacedoes not seem to allow for this … 🤔 It only supports calculating dependencies, not removing them. Reacting to dependency removals is only supported at the config entity level, not at the "config entity using plugins that have configurable dependencies" level.Not sure what to do here. But it's most definitely out of scope/pre-existing.
EDIT: to get this problem space moving forward again, I pushed #3167198-32: Deprecate the 'roles' property of text formats and remove it from the UI forward 🤓
\Drupal\media\Plugin\Filter\MediaEmbed::process():Comment #54
catchThanks that makes sense, but yes we definitely shouldn't just delete filter formats like that. There's also all the limbo state with 'disabled' filter formats too.
Another question: media provides a 'thumbnail' formatter for all media items, which just shows the image without actually rendering the entity. Should it be an option to do a similar thing in the editor (i.e. specify thumbnail + image style)? (See related discussion in #2947796: Responsive image format for media and #3247795: Add text filter plugin to support <img loading="lazy"> and remove it from editor_file_reference). Would be a separate option to view mode obviously, but would need to integrate with what's been added here cleanly.
Comment #55
wim leersYep. And TIL
\Drupal\views\Plugin\DependentWithRemovalPluginInterfaceexists but … is Views-only 😭This also made me look to check how CKEditor 4 has been dealing with config dependencies. In short: poorly. See #2950795, and my detailed response at #2950795-29: CKEditor 5 plugin module dependency not added to text format configuration. tl;dr: the plugin/config system is missing some infrastructure to make this possible.
I … don't know. This is also out of scope here IMHO. This is just making sure that we retain feature parity with CKEditor 4, and all content created with CKEditor 4 continues to be editable. See #2940029: Add an input filter to display embedded Media entities for all the considerations that went into the "media embed" filter + CKEditor 4 functionality.
I've been reviewing #3247795: Add text filter plugin to support <img loading="lazy"> and remove it from editor_file_reference for months; for months I've been asking for embedded Media support there. So far, the UX that has been RTBC'd is just to only allow one to override the default behavior in CKEditor 4 (or 5) by switching to source editing and adding the
lazy=…attribute there. And I think that makes sense: we don't want to throw such an advanced (and rare) decision in the face of content creators all the time. But that is definitely independent of view mode.I was not aware of #2947796: Responsive image format for media . That reminds me of #2061377: [drupalImage] Optionally apply image style to images uploaded in CKEditor 5 and #2822389: Allow responsive image style to be selected in Text Editor's image dialog (necessary for structured content) which I've been trying to land ever since CKEditor 4 was added to Drupal 8 almost 10 years ago 😔 #2947796 has no integration at all yet for CKEditor (4 or 5) — it's only for media fields. It would also need to update
\Drupal\media\Plugin\Filter\MediaEmbedto allow overriding the newresponsive_image_stylefield property. But the good news is that we already have something like this foraltandtitle— see\Drupal\media\Plugin\Filter\MediaEmbed::applyPerEmbedMediaOverrides()(introduced in #2994702: Allow editors to alter embed-specific metadata, as well as `data-align` and `data-caption`). And for that, the CKEditor 5 integration (just like the CKEditor 4 integration) already has the ability to override the imagealtstored in the Media Library. We could do the same for the responsive image style. But that'd definitely result in a confusing UX. That's always the trade-off: which level of granularity do we expose? InMediaEmbed(#2940029: Add an input filter to display embedded Media entities), we chose to expose view modes, not responsive image styles — in no small part because responsive image styles do not work for media other than images. There's nothing stopping you from creating one view mode per responsive image style, and have that view mode only be meaningful (i.e. have config) for images. CKEditor 5 will only show those view modes for the media type that actually have a view display configured. Besides, some responsive image styles may not make sense for image media — maybe they're specifically used for user profile pictures for example. So I do not think #2947796: Responsive image format for media is a wise approach.Comment #56
wim leers@catch: Just a sanity check: can you please confirm that all of these (excellent!) questions you're asking are only about tangential concerns, not concerns with this MR specifically? 🙏
Everything in the CKEditor 5 experience matches what you can do in the CKEditor 4 experience functionality-wise. It just has a better UX 🤓
Comment #57
catch@Wim Leers the stuff about image styles are tangential concerns, but also trying to trace the future path of what this might look like if we allow image style configuration and want parity with that for media. #2061377: [drupalImage] Optionally apply image style to images uploaded in CKEditor 5 is the issue I was looking for but couldn't find.
The view mode removal question isn't really tangential - even though we might not want to fix it here, we'd want to make sure we haven't made the situation worse than it was with ckeditor4, sounds like this is the case.
I know we're trying to provide parity with ckeditor4 so the upgrade path doesn't destroy existing content/editor configurations, but also what ckeditor4 does is not necessarily what we'd want to do if we were adding ckeditor5 to core without having to deal with ckeditor4 configuration on top, especially as features are getting added to media/image styles over time. As long as we've got paths open to modernise things later, that's the main thing.
Comment #58
wim leers100% agreed!
If anything, it has become simpler to add more capabilities (or refine existing ones) in the future, because we can now be confident about the configuration that is stored, and how the text format and editor are configured together.
Thanks for being vigilant and forward-thinking! 😊👍
Comment #59
wim leersWe really still need to get this committed to
9.3.x.The #48 patch appears to still work fine for me locally, not sure why it didn't apply the first time. I just requeued the test.
Comment #60
wim leers#48 doesn't apply using the patch application mechanism that DrupalCI uses. Fetched patch,
git apply -3v'd it, then recreated.Also updated issue metadata to reflect that this is a CKEditor 5 stable blocker.
Comment #62
lauriiiCommitted 69a6d5b and pushed to 9.3.x. Thanks!