Problem/Motivation
When you remove all buttons from the CK Editor 5 toolbar, \Drupal\ckeditor5\Plugin\Editor\CKEditor5::validateConfigurationForm (line 529) sets a form error: 'Invalid toolbar value.' Of course an empty toolbar isn't very useful, but I do not think we should call it an error. It can be a perfectly logical in-between state while editing the toolbar. There even might be (temporary) reasons to save such an empty toolbar, but a validation error makes that impossible.
Steps to reproduce
- Add a new text format or edit any of the existing text formats
- Select CK Editor 5
- Drag all buttons off the toolbar until it is empty
- Once the "Active toolbar" section is empty, hit the "Save configuration", The error will now appear
Invalid toolbar value.
Proposed resolution
In the validate function, where it checks if there are any toolbar items, return like we do now, but do not set a form error:
if (!$toolbar_items) {
return;
}
I'm not sure if this even needs tests, but I suppose we could add a test in \Drupal\Tests\ckeditor5\FunctionalJavascript\AdminUiTest that removes all buttons and asserts there are no form errors on the page. In fact, such a test could easily be integrated in the test that's waiting for approval in #3259443: Plugin settings do not appear when a configurable plugin is added AFTER removing all buttons.
Remaining tasks
Fix.
User interface changes
none
API changes
none
Data model changes
none
Release notes snippet
not needed
Issue fork drupal-3259892
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 #3
marcvangendFix committed to the issue fork.
If we decide this needs a test, I suggest postponing this on #3259443 and add to that test. (But honestly, it seems bit silly to test that a form validator does not set an error when there is no error.)
Comment #4
wim leersInteresting … that reminds me of #3215897: Toolbar items validation errors not able to target the toolbar.
I think the problem here is that the fix that you made is too broad.
This is the full code:
You are removing the
::setErrorByName()call. That was intended to get invoked when there is invalid JSON. But you're right that PHP also enters that branch when$toolbar_items === [].I think we should special case the empty array case. Or … better yet … change the
if (!$toolbar_items)to different code that checks whether there were JSON decoding errors.Comment #5
marcvangendThanks Wim. I didn't realize the check was intended to catch JSON decoding failures, so I made that more explicit now. This also means that validation will now continue if the toolbar is empty, but I guess the validation was never intended to bail out early in that case.
Comment #6
wim leersWe should still add a test to verify that A) it's possible to remove all buttons using the admin UI, B) it does not result in errors.
A is already the case, but by testing it using the UI we ensure we don't regress on this front.
Although … it looks like whichever toolbar item was the last one will persist. So this does expose/introduce a new edge case. I think we should solve that here at the same time. This won't be able to land without test coverage anyway.
I just tested it (by forcing it manually), and yes, we can use CKEditor 5 without any toolbar items. But you'll only be able to create
<p>and<br>. And while weird … I do think that's valid.So, to expand what I said: We should still add a test to verify that A) it's possible to remove all buttons using the admin UI, B) it does not result in errors, C) we should be able to save it, D) when re-editing, we should assert that there are still no buttons present.
Comment #8
wim leersThoughts from people besides @marcvangend and I? 🤞
Comment #10
wim leersI think you're right actually. It's possible that you want to only allow
<p>and<br>. Several tests written since you wrote that do precisely that.Comment #13
wim leersClosed #3372452: When a textformat has no text-editor configured ckeditor crashes as a duplicate of this. Crediting @tim@lammar.be for that!
Clearly, a tiny percentage of people want to configure CKEditor 5 in a way that there's no toolbar at all. @tim@lammar.be, can you please explain what your use case is for:
?
Thanks! 🙏
Comment #14
tim@lammar.be commented@Wim Leers,
Sure :-)
Our use case is we have a text format field and 2 form modes for content editing of that specific node type.
In one form mode, the field may be filled with content generated by editor buttons. In the other form mode, the same field may only be filled with text (much like a textarea field). This is mostly because the one form mode is used for users with more permissions, like using advanced editor functionality. The other form mode is used for users with less permissions and actually can not use the editor. So the field needs to be text format, but for some users the editor may not be used. So we had a text format that had no editor active (what was possible in CKEditor 4, but now with the upgrade to Drupal 10, it gave issues)
Comment #15
wim leersThanks for the fast clarification!
Even with no buttons, you'll still be able to create
<p>and<br>in CKEditor 4/5 — without those tags, it cannot function.What you're describing indeed sounds like a use case for having NO text editor for that one text format. What problem did you run into when you tried doing that? 🤔
Comment #16
tim@lammar.be commentedI don't remember exactly what errors occurred, but it had to do with the fact that in the code there are array methods used on toolbar items. These are in this case non existing and so causing php errors (something like expecting array, got NULL).
The patch I provided in the previous ticket fixed the ones I encountered, but it's possible other places need to be fixed as well (ref: https://www.drupal.org/project/drupal/issues/3372452#comment-15136932)
Comment #17
wim leersI don't see how any toolbar item method would be getting invoked when no text editor is configured? 🤔
Comment #18
tim@lammar.be commentedExactly, but apparently it did 🤷♂️
Comment #19
socialnicheguru commentedPlain text filter might want this
Comment #20
prashant.cComment #22
prashant.cI added MR for the
11.x, change the condition to check whether the JSON is valid or not. Changes need to be reviewed.