Problem/Motivation

I'd like users of my site to be easily able to add predefined CSS classes to images, in the same way that CKEditor 5 allows them to easily add predefined classes to text. But it isn't clear how to configure this.

The CKEditor 5 config page for a text format allows the Styles plugin to be used to add arbitrary CSS classes to specific HTML tags - these are displayed as a drop-down list which the user can select from. e.g. I can add "a.special-offer|Special Offer" to provide the user with a "Special Offer" style, which will add the "special-offer" class to a selected <a> tag in the text.

However, this does not work for images.

The Styles plugin will allow me to add an entry such as "img.feature|Featured Image" as a style, and this will appear as an option to the user in the Styles drop down. However, when the user selects an image, the Styles dropdown becomes greyed out, and there's a separate, pop-up toolbar with image-specific functions. This makes it impossible to use the styles drop-down to add the required style. (I imagine this is the expected behaviour.)

I presume that in order to allow the user to apply my custom style to an image, I'll need to add an option button to the image pop-up toolbar. How do I do that?

The CKEditor documentation for Image Styles says that custom CSS styles can be added. And it is clear that this is different from adding text Styles. These pages explain how to achieve this at the CKEditor config level. But what would I need to do in Drupal terms?

I can see that in core/modules/ckeditor5/ckeditor5.ckeditor5.yml there's some config relating to the imagestyle toolbar. I am happy to forgo a GUI interface, but even so, I imagine that what I want to do cannot be achieved simply by editing this file.

I looked at https://www.drupal.org/docs/core-modules-and-themes/core-modules/ckedito..., but it doesn't cover this kind of issue. I also looked at https://www.drupal.org/docs/core-modules-and-themes/core-modules/ckedito..., but wasn't sure it that was the right route to go down.

What are the steps for customising the imagestyle toolbar? Do I need to create a new plugin? Or can can I simply adjust some configuration for an existing plugin - as I can for styles for tags other than <img>?

Steps to reproduce

  1. Go to the configuration page for the Basic HTML text format.
  2. Ensure the Style button is in the Active Toolbar.
  3. Under CKEditor 5 Plugin Settings > Style, add a line: img.feature|Featured Image
  4. Click outside the text box.
  5. Click Save Configuration.
  6. Edit a page on the site with a rich text field. Ensure the field is set to use Basic HTML text format.
  7. Add some text to the field.
  8. Select some text, then click on the Styles dropdown. Note that you have the option to select the "Featured Image" style, even though this is not relevant for text (Doing so will have no effect).
  9. Insert an image into the field.
  10. Click on the image to select it.
  11. Note that the Styles dropdown is now greyed out, making it impossible to select the "Featured Image" style, and instead, a pop-up toolbar is shown with image-related options.

Proposed resolution

Provide some documentation which explains how to add custom buttons to the image pop-up toolbar, including a button which adds a specified class to the image.

✅ It would be helpful if the Styles Plugin could warn users when they attempt to define a style for an <img> tag that this will not be effective.

Or, better still, extend the Styles Plugin so that defining a style for an <img> tag causes a suitable button to appear in the image pop-up toolbar.

Remaining tasks

Review.

User interface changes

A helpful validation error message now appears immediately:

API changes

None.

Data model changes

None.

Release notes snippet

TBD

Issue fork drupal-3347721

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dabley created an issue. See original summary.

Wim Leers’s picture

Title: How to add custom CSS image styles? » [Style] Warn the user about styles for unsupported elements
Category: Support request » Task
Priority: Normal » Major
Issue tags: +Usability
Related issues: +#2642808: [upstream] CKEditor Style for <img> not working because it is an "image" widget, +#3222797: Upgrade path from CKEditor 4's StylesCombo to CKEditor 5's Style, +#3280124: [PP-1] [upstream] Consider allowing styles for non-HTML5 tags, +#3117172: Allow CKEditor styles for <drupal-media>

The same limitation existed in CKEditor 4: #2642808: [upstream] CKEditor Style for <img> not working because it is an "image" widget. That's why it was not a blocker for #3222797: Upgrade path from CKEditor 4's StylesCombo to CKEditor 5's Style.

Note the sibling issue #3280124: [PP-1] [upstream] Consider allowing styles for non-HTML5 tags for CKEditor 5 and #3117172: Allow CKEditor styles for <drupal-media> for Drupal core's Media module.

It would be helpful if the Styles Plugin could warn users when they attempt to define a style for an Only local images are allowed. tag that this will not be effective.

💯 — this is a great action to take today!

Let's do that here! I'll ask in the upcoming CKEditor 5 meeting later today how we can get a reliable list of elements that the current Style plugin actually supports.

Wim Leers credited Reinmar.

Wim Leers’s picture

Just discussed with @Reinmar & Witek. Witek indicates that list of tags that are supported by Style is actually one of the things their customers are missing in their official docs. So: 2 birds, 1 stone — they'll work on getting us this list soonish so that we can add the necessary validation and ship that to users in 9.5.x and 10.0.x 🤓👍

dabley’s picture

@Wim-Leers, thanks for your input on this. My comment about warning about the use of an unsupported style tag was really just an aside, and not the main point of the issue I raised. (It did occur to me that perhaps I should not create confusion by mentioning this.) Your change to the title rather shifts attention away from my main concern.

Thanks for pointing out the related issues. It is sad to see that despite 7 years of effort, there are no solutions, only partially successful workarounds. Nobody seems to have given much consideration to customising the image widget so as to include a styles dropdown. I wonder if that's because it is just too difficult?

Wim Leers’s picture

Issue tags: +Needs tests

@witeksocha from CKSource investigated this: https://github.com/ckeditor/ckeditor5/issues/13763

I think this should be implemented as validation? OTOH, doing so could cause existing configuration to be treated as invalid, even if in the future it will work, but for now it won't, until the upstream bugs are fixed … tricky 🤔

lauriii’s picture

Could we implement it just as a warning in the UI instead of schema validation that prevents saving?

Wim Leers’s picture

Yes, but how?

  1. A confirmation form if we detect that you added new styles that are known to not work?
  2. Or simply always a confirmation form if any of the styles are known to not work?
  3. Or something else? 😅
Wim Leers’s picture

Version: 9.5.x-dev » 11.x-dev
Assigned: Unassigned » lauriii
Status: Active » Needs review
Issue tags: +Needs issue summary update

#3326261: [Style] Add tests for inability to apply styles to <div>, <ul>, <ol>, <table> etc. in CKEditor 5 — and <a> allows applying it to all elements proves that one of the examples in the issue summary now does work: a.special-offer|Special Offer. But img.feature|Featured Image indeed does not yet work (upstream issue: https://github.com/ckeditor/ckeditor5/issues/13778), nor does drupal-media.features|Features media (#3117172-34: Allow CKEditor styles for <drupal-media>).

So I think this is still relevant.

@lauriii, what do you think about limiting the scope of this issue to <img> and <drupal-media> for now? And perhaps showing a warning message that links to the relevant upstream issue, asking the site builder to go and vote on those issues 🤓

smustgrave’s picture

Just my 2 cents but I think any warnings that core can do around ckeditor would be extremely useful.

lauriii’s picture

Assigned: lauriii » Unassigned
Status: Needs review » Needs work

The proposal seems fine 👍 We can expand the scope of the warning if we discover other use cases that are not compatible.

Wim Leers’s picture

Just closed #3398786: CKEditor 5 - styles for Images not picking up as a duplicate of this. This is clearly becoming increasingly important!

Wim Leers’s picture

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

Just pushed a test.

Wim Leers’s picture

Wim Leers’s picture

Now passing tests 👍

P.S.: please vote on https://github.com/ckeditor/ckeditor5/issues/13778 — that's how they prioritize things: based on demand!

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Ran the test-only feature and got

1) Drupal\Tests\ckeditor5\Kernel\ValidatorsTest::test with data set "INVALID: Style plugin configured to add class to plugin-supported tag known to not work with Style … yet" (array(array(array('drupalInsertImage', 'style')), array(array(false), array(array(array('Featured image', '<img class="featured">'), array('Fancy linebreak', '<br class="fancy">'))))), array('Unfortunately, the <code>&lt;...lugin.', 'Unfortunately, the <code>&lt;...lugin.'))
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
-Array &0 (
-    'settings.plugins.ckeditor5_style.styles.0.element' => 'Unfortunately, the <code>&lt;img&gt;

tag is not yet supported by the Style plugin.'
- 'settings.plugins.ckeditor5_style.styles.1.element' => 'Unfortunately, the &lt;br&gt; tag is not yet supported by the Style plugin.'
-)
+Array &0 ()

Tested manually following the steps in the issue summary and am now seeing the warning message.

Think this will be very helpful for many people

  • lauriii committed e8d2c22a on 11.x
    Issue #3347721 by Wim Leers, dabley, lauriii, smustgrave, witeksocha,...

  • lauriii committed 134e3137 on 10.2.x
    Issue #3347721 by Wim Leers, dabley, lauriii, smustgrave, witeksocha,...

lauriii’s picture

Version: 11.x-dev » 10.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed e8d2c22 and pushed to 11.x. Also cherry-picked to 10.2.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.