I know that it is possible to load custom css to all ckeditor instances by specifying 'ckeditor_stylesheets' in my theme.

The problem I encountered with this was that I needed to load an external css (containing webfonts). This does not work since I cannot define an external css file like I do when defining a library, which is:

font:
  version: 1.0
  css:
    base:
      //cloud.webtype.com/css/XXXXXXXXXXXXX.css: { type: external }

One way to fix this would be to make it possible to pass in an object to ckeditor_stylesheets instead of a list of files. But wouldn't it be even nicer if we could attach libraries to be loaded instead? Then we could attach internal and external css and even javascript if the library contained that. Something like:

ckeditor_libraries:
  - theme/font

Comments

reekris created an issue. See original summary.

wim leers’s picture

even javascript if the library contained that

We specifically don't want that. That's why it's not using libraries.

reekris’s picture

Oh I see :)

But what about the possibility of adding css in 'object form' then to make it possible to attach external css?

Or maybe a check could be added to the css path so that it supports absolute urls? Now it prefixes each css with my theme path even if it's an absolute url to an external resource

My current workaround is to use the hook for adding css which is working fine. Except that it seems it needs to be added in a module and not a theme. So I have a custom module only containig this hook and referencing css in my theme, which doesn't feel right.

thpoul’s picture

@reekris for #3 at Drupal 8.1 there is a new CKEditorPluginCssInterface. Checkout #2645100: CKEditorPluginCssInterface: Allow CKEditor plugins to add CSS to iframe CKEditor instances.

reekris’s picture

@thpoul thanks for that link!

Although it seems for my use case a ckeditor plugin seems to be something more complex than what I want. I'm not adding any custom button or other functionality to my ckeditor, I simply wish to add external css to the ckeditor iframe without having to create a custom module to implement the hook.

Since adding libraries is not supported by design, I would say that adding support for external css under the ckeditor_stylesheets key eould be a good solution.

But maybe I'm just nitpicking, the hook solution I went with is working fine so its actually not that big a deal :)

wim leers’s picture

Title: CKEditor: Make it possible to attach library » Allow ckeditor_stylesheets to refer to external URL, for e.g. webfonts
Priority: Normal » Minor
Issue tags: -CKEditor in core, -ckeditor +Needs tests

Clarifying issue title.

I think the case could be made for this. But it's definitely something that's pretty rarely necessary. As long as this has test coverage, I'm fine with it.

breezeweb’s picture

Thanks for feedback on this Wim.

I'd have to disagree that this is a minor/rare situation, however.

If you're using externally-hosted webfonts in your front end theme (I'd wager the majority of sites do) then content editors need to see those same font styles in CKEditor for consistency. Otherwise it is only quasi-WYSIWYG.

wim leers’s picture

Issue tags: +Novice, +php-novice

I agree fonts are quite common. But loading fonts from a 3rd party is always a bad idea. An additional point of failure (and actually a single point of failure for tens of thousands of sites). Guaranteed worse performance, because more TCP/IP connections.
Whenever possible, self-host your font(s). And subset them.

But, like I said: . As long as this has test coverage, I'm fine with it.


This would just require modifications to _ckeditor_theme_css(), so that it detects when the specified value is not actually a path. i.e. use parse_url() to detect whether it's an absolute or protocol-relative URL, and if that's the case, don't prefix it with the theme path. That's it.

tstoeckler’s picture

Issue tags: +DrupalBCDays
wim leers’s picture

#9: woot! Happy to provide reviews.

maggo’s picture

Here's the basic fix as requested in #8

I'll have to look into the tests next. I guess CKEditorLoadingTest::testLoading is a good starting point? (I'm new to this testing thing :) )

thpoul’s picture

Status: Active » Needs review

Switching to NR.

wim leers’s picture

Status: Needs review » Needs work

#11: That looks perfect :)

Now all we need is some test coverage. I'd add a new testExternalStylesheets method to CKEditorLoadingTest, and let first let it install theme A which has an absolute external URL, then let it install theme B, which has a root-relative external URL and finally let it install theme C, which has a relative URL (a path), to test the original case

avinashm’s picture

Issue summary: View changes
thpoul’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new4.49 KB

Test only patch. Should fail!

thpoul’s picture

StatusFileSize
new4.49 KB
new5.35 KB

And the full patch :)

EDIT: Oh snap, saw some nits already in the info.yml files but waiting for Wim's review first :)

The last submitted patch, 15: 2682723-15-test-only.patch, failed testing.

wim leers’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

#15: Thanks, that looks great!

  1. +++ b/core/modules/ckeditor/src/Tests/CKEditorLoadingTest.php
    @@ -194,6 +194,35 @@ protected function testLoadingWithoutInternalButtons() {
    +    \Drupal::service('theme_handler')->install(['test_ckeditor_stylesheets_external']);
    

    Rather than repeating \Drupal::service('theme_handler') six times, let's store it in a variable.

  2. +++ b/core/modules/ckeditor/src/Tests/CKEditorLoadingTest.php
    @@ -194,6 +194,35 @@ protected function testLoadingWithoutInternalButtons() {
    +    // Case 2: Install theme which has an external root-relative CSS URL.
    

    This is protocol-relative, not root-relative.

  3. +++ b/core/modules/system/tests/themes/test_ckeditor_stylesheets_external/test_ckeditor_stylesheets_external.info.yml
    @@ -0,0 +1,9 @@
    +name: Test External CKEditor stylesheets
    ...
    +description: 'A theme that uses an external webfont stylesheet.'
    
    +++ b/core/modules/system/tests/themes/test_ckeditor_stylesheets_external_root_relative/test_ckeditor_stylesheets_external_root_relative.info.yml
    @@ -0,0 +1,9 @@
    +name: Test External CKEditor stylesheets
    ...
    +description: 'A theme that uses an external webfont stylesheet.'
    
    +++ b/core/modules/system/tests/themes/test_ckeditor_stylesheets_relative/test_ckeditor_stylesheets_relative.info.yml
    @@ -0,0 +1,9 @@
    +name: Test External CKEditor stylesheets
    ...
    +description: 'A theme that uses an external webfont stylesheet.'
    

    These are all identical names. Let's give them corresponding names.

  4. +++ b/core/modules/system/tests/themes/test_ckeditor_stylesheets_external/test_ckeditor_stylesheets_external.info.yml
    --- /dev/null
    +++ b/core/modules/system/tests/themes/test_ckeditor_stylesheets_external_root_relative/test_ckeditor_stylesheets_external_root_relative.info.yml
    

    s/root/protocol/


#16:

+++ b/core/modules/ckeditor/ckeditor.module
@@ -86,7 +87,12 @@ function _ckeditor_theme_css($theme = NULL) {
       foreach ($css as $key => $path) {

Let's rename $path to $url.


Other remarks:

  1. We need to update hook_ckeditor_css_alter()'s documentation in ckeditor.api.php.
  2. We need a change record.
  3. The documentation at https://www.drupal.org/developing/api/8/ckeditor is sufficient already.
jagjitsingh’s picture

StatusFileSize
new2.88 KB

hi,

i am a newbie , this is my first path ever. uploading my first path here. i have just incorporated point #2 from comment #15 .

wim leers’s picture

#19: Thanks! Can you please provide an interdiff? See https://www.drupal.org/documentation/git/interdiff.

jagjitsingh’s picture

Status: Needs work » Needs review
StatusFileSize
new5.35 KB
new827 bytes

Uploading a new patch with inter-diff.
Thanks for your reply.

wim leers’s picture

Status: Needs review » Needs work

Thanks! Great first patch :)

That means #18.2 is now solved, which leaves #18.1, #18.3 and #18.4, as well as the remarks at the bottom of #18.

avinashm’s picture

Status: Needs work » Needs review
StatusFileSize
new5.44 KB
new802 bytes

Uploaded my first patch ever thanks @snehi and @jagjit for help.
Renamed $path to $url as mentioned for #16.

thpoul’s picture

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

Thank you!

Temporarily assigning to me to work on it.

wim leers’s picture

@avinashm: thanks, that also looks good :)

thpoul’s picture

Assigned: thpoul » Unassigned
Status: Needs work » Needs review
StatusFileSize
new5.95 KB
new6.02 KB

Here is the updated patch as per #18

Note for #18-4: Couldn't apply s/root/protocol due to test_ckeditor_stylesheets_external_protocol_relative is over the maximum allowed length of 50 characters I changed it to test_ckeditor_stylesheets_protocol_relative.

thpoul’s picture

Here is the change record draft too: https://www.drupal.org/node/2717985

wim leers’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs change record

Perfect!

CR looks great (I made only minor changes).

I think this is ready.

The last submitted patch, 19: 2682723-17.patch, failed testing.

alexpott’s picture

Version: 8.2.x-dev » 8.1.x-dev

Nice tests! Committed 0dd68ab and pushed to 8.2.x. Thanks!

Isn't this more of a bug and therefore eligible for 8.1.x too?

  • alexpott committed 0dd68ab on 8.2.x
    Issue #2682723 by thpoul, jagjitsingh_drupal, avinashm, maggo: Allow...
wim leers’s picture

It wasn't a bug; it was intentional that we only supported files in the theme originally. But you could argue that was an oversight. This is extremely low risk, so I'd be fine with committing it to 8.1 too.

alexpott’s picture

@Wim Leers but if I'm reading the code right on 8.1.x atm using an external url will result in 404s. Since we're just doing $css[$key] = $theme_path . '/' . $path;. If we'd chosen to error on an external URL I'd agree.

wim leers’s picture

Good point. Though I don't know if it'd 404, I don't know how a browser would parse something like /themes/foobar/http://example.com/webfont.css, and I'd suspect there might be differences between browsers.

Only committing to 8.2 to be on the safe side is fine for me.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Discussed with @catch and we decided that fixing this in 8.1.x was ok.

Committed d8ee7e2 and pushed to 8.1.x. Thanks!

  • alexpott committed d8ee7e2 on 8.1.x
    Issue #2682723 by thpoul, jagjitsingh_drupal, avinashm, maggo: Allow...

Status: Fixed » Closed (fixed)

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

jibran’s picture