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

Command icon 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

marcvangend created an issue. See original summary.

marcvangend’s picture

Status: Active » Needs review

Fix 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.)

wim leers’s picture

Status: Needs review » Needs work
Related issues: +#3215897: Toolbar items validation errors not able to target the toolbar

Interesting … 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:

    $toolbar_items = Json::decode($json);

    // This basic validation must live in the form logic because it can only
    // occur in a form context.
    if (!$toolbar_items) {
      $form_state->setErrorByName('toolbar][items', $this->t('Invalid toolbar value.'));
      return;
    }

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.

marcvangend’s picture

Status: Needs work » Needs review

Thanks 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.

wim leers’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

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.

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.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

wim leers’s picture

Issue tags: +Usability

Thoughts from people besides @marcvangend and I? 🤞

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

wim leers’s picture

I 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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

wim leers’s picture

Closed #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:

When you have a text format where you don't want a toolbar and you configure the text-editor field to be none, […]

?

Thanks! 🙏

tim@lammar.be’s picture

@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)

wim leers’s picture

Thanks for the fast clarification!

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)

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? 🤔

tim@lammar.be’s picture

I 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)

wim leers’s picture

I don't see how any toolbar item method would be getting invoked when no text editor is configured? 🤔

tim@lammar.be’s picture

Exactly, but apparently it did 🤷‍♂️

socialnicheguru’s picture

Plain text filter might want this

prashant.c’s picture

Issue summary: View changes

prashant.c’s picture

I added MR for the 11.x, change the condition to check whether the JSON is valid or not. Changes need to be reviewed.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.