Problem/Motivation

  • Some CKEditor plugins need additional CSS to function. E.g. image captions, alignment, etc.
  • Loading additional CSS in CKEditor iframe instances currently requires hook_ckeditor_css_alter() to be implemented.
  • This means a CKEditor plugin is not able to encompass everything it does: some CKEditor plugins need both a plugin implementation and a hook implementation.

Conclusion: this is a gap in the current API.

Proposed resolution

Fortunately, CKEditor plugins are already built on composed interfaces. So, the mere addition of a CKEditorPluginCssInterface will be sufficient. Any module is then free to adopt this. This is therefore a pure API addition, not even a feature addition.

Remaining tasks

Do it.

User interface changes

None.

API changes

None. Pure API addition.

Data model changes

None.

Comments

Wim Leers created an issue. See original summary.

wim leers’s picture

Title: Allow CKEditor plugins to add CSS to the iframe CKEditor instance. » Allow CKEditor plugins to add CSS to iframe CKEditor instances
wim leers’s picture

Title: Allow CKEditor plugins to add CSS to iframe CKEditor instances » CKEditorPluginCssInterface: Allow CKEditor plugins to add CSS to iframe CKEditor instances
wim leers’s picture

thpoul’s picture

StatusFileSize
new7.16 KB

Let's try and move this forward. Here is a first shot.

  • Added the interface
  • Implemented the new getCssFiles() method
  • Moved DrupalImageCaption related css from ckeditor_ckeditor_css_alter() to Drupal\ckeditor\Plugin\CKEditorPlugin\DrupalImageCaption
wim leers’s picture

Status: Active » Needs review
wim leers’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,45 @@
    + * Defines an interface to allow CKEditor plugins to add additional CSS to iframe
    + * CKEditor instances.
    

    Too long (should fit on one line). What about:

    Defines an interface for CKEditor plugins with associated CSS.

  2. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,45 @@
    + * This allows a CKEditor plugin to add additional CSS to iframe CKEditor
    

    s/to iframe/in iframe/

  3. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,45 @@
    + * instances without the need to implement hook_ckeditor_css_alter() in order
    + * to do so.
    

    s/the need to/needing to/

    s/in order to do so//

  4. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,45 @@
    + * @see \Drupal\ckeditor\CKEditorPluginInterface
    + * @see \Drupal\ckeditor\CKEditorPluginButtonsInterface
    + * @see \Drupal\ckeditor\CKEditorPluginContextualInterface
    + * @see \Drupal\ckeditor\CKEditorPluginConfigurableInterface
    

    Can you also update those 4 interfaces to get an @see to this new interface?

  5. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,45 @@
    +   * @return array &$css
    

    s/array/string[]/

  6. +++ b/core/modules/ckeditor/src/CKEditorPluginManager.php
    @@ -182,4 +182,28 @@ public function injectPluginSettingsForm(array &$form, FormStateInterface $form_
    +    $plugins = array_keys($this->getDefinitions());
    

    This is not yet specific enough; this is going to get the CSS for all CKEditor plugins, whereas we just want it for the enabled CKEditor plugins.

    Apparently CKEditorPluginManager does not yet have a specific method to get the enabled plugins.

    So you'll want to factor out a protected getEnabledPlugins() method out of the already existing getEnabledPluginFiles() method.

    Then both getEnabledPluginFiles() and getCssFiles() will be able to call that new protected method.

    Apparently we should use $enabled_plugins = array_keys($this->ckeditorPluginManager->getEnabledPluginFiles($editor, TRUE)); for that :) And in fact, you already do exactly that in CKEditor::buildContentCssJSSettings().

    I think we want *this* method to return only CSS files for enabled plugins, then CKEditor::buildContentCssJSSettings() also becomes much simpler.

    Or what reason do you see to return all CSS files?

  7. +++ b/core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalImageCaption.php
    @@ -72,6 +73,30 @@ public function getConfig(Editor $editor) {
    +    if ($format->filters('filter_caption')->status) {
    +      $css[] = drupal_get_path('module', 'filter') . '/css/filter.caption.css';
    +    }
    

    This CSS file is not specific to the DrupalImageCaption plugin. It actually is necessary even when this plugin is not enabled, because you can also add captions to tags other than <img>. So, this should go away here and be added back to ckeditor_ckeditor_css_alter().

  8. +++ b/core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalImageCaption.php
    @@ -72,6 +73,30 @@ public function getConfig(Editor $editor) {
    +    // Add the filter caption CSS if the text format associated with this text
    +    // editor uses the filter_align filter. This is used by the included
    +    // CKEditor DrupalImageCaption plugin.
    +    if ($format->filters('filter_align')->status) {
    +      $css[] = drupal_get_path('module', 'ckeditor') . '/css/plugins/drupalimagecaption/ckeditor.drupalimagecaption.css';
    +    }
    

    The CSS file added here is actually completely specific to drupalimagecaption. So, it can be loaded unconditionally (you can remove the if-statement).

    Precisely because thanks to this new interface, this is only going to be invoked if this plugin is active in the first place!

thpoul’s picture

StatusFileSize
new7.14 KB
new9.27 KB

Thank you for the review Wim. Here is the updated version after your suggestions.

I haven't done anything yet with:
I think we want *this* method to return only CSS files for enabled plugins, then CKEditor::buildContentCssJSSettings() also becomes much simpler.

thpoul’s picture

Status: Needs work » Needs review
wim leers’s picture

Status: Needs review » Needs work

Looking much better already!

  1. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,44 @@
    + * instances without the needing to implement hook_ckeditor_css_alter()
    

    s/the//

  2. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,44 @@
    + * in order to do so.
    

    s/in order to do so//

  3. +++ b/core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalImageCaption.php
    @@ -72,6 +73,18 @@ public function getConfig(Editor $editor) {
    +    // Add the filter caption CSS if the text format associated with this text
    +    // editor uses the filter_align filter. This is used by the included
    +    // CKEditor DrupalImageCaption plugin.
    

    This comment can be removed altogether.

  4. +++ b/core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalImageCaption.php
    @@ -72,6 +73,18 @@ public function getConfig(Editor $editor) {
    +    return array(
    +        drupal_get_path('module', 'ckeditor') . '/css/plugins/drupalimagecaption/ckeditor.drupalimagecaption.css'
    +    );
    

    s/array()/[]/

    4 spaces of indentation instead of 2.

thpoul’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests
StatusFileSize
new9.08 KB
new2.71 KB

Changes made :) Still needs tests though. Tagging accordingly.

wim leers’s picture

This will also need a change record and documentation updates. (The documentation that should be updated lives at https://www.drupal.org/developing/api/8/ckeditor.)

wim leers’s picture

Thank you so much for pushing this forward!

The last submitted patch, 11: 2645100-11.patch, failed testing.

wim leers’s picture

Status: Needs review » Needs work

Hm, I wonder why testbot didn't mark this "needs work" given #11 failed.

thpoul’s picture

Status: Needs work » Needs review
StatusFileSize
new3.87 KB
new9.66 KB

Added a test for the new api addition. Also cleaned up some things and refactored a bit.

thpoul’s picture

StatusFileSize
new589 bytes
new9.65 KB

Minor comment fix.

wim leers’s picture

Status: Needs review » Needs work

Mostly there :)

  1. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,43 @@
    +   * @todo document.
    +   *
    +   * The editor's settings can be retrieved via $editor->getSettings().
    

    Needs to be updated.

    Perhaps also add:

    Note: this does not use a Drupal asset library because this CSS will be loaded by CKEditor, not by Drupal.

  2. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,43 @@
    +  public function getCssFiles(Editor $editor);
    

    This function interface method name is consistent with the base interface's method name: \Drupal\ckeditor\CKEditorPluginInterface::getFile() and has similar reasoning/requirements ("files, not asset libraries, because this is for CKEditor, not Drupal").

    So, +1 for the method name.

  3. +++ b/core/modules/ckeditor/src/CKEditorPluginManager.php
    @@ -182,4 +182,31 @@ public function injectPluginSettingsForm(array &$form, FormStateInterface $form_
    +   * Retrieves all additional CKEditor CSS files, keyed by plugin ID.
    

    Retrieves enabled plugins' iframe instance CSS files, keyed by plugin ID.

  4. +++ b/core/modules/ckeditor/src/CKEditorPluginManager.php
    @@ -182,4 +182,31 @@ public function injectPluginSettingsForm(array &$form, FormStateInterface $form_
    +   *   All available CKEditor CSS files, with plugin IDs as keys and
    

    s/All available/Enabled plugins'

  5. +++ b/core/modules/ckeditor/src/CKEditorPluginManager.php
    @@ -182,4 +182,31 @@ public function injectPluginSettingsForm(array &$form, FormStateInterface $form_
    +   * @see CKEditorPluginCssInterface::getCssFiles()
    

    FQCN

  6. +++ b/core/modules/ckeditor/src/Plugin/Editor/CKEditor.php
    @@ -9,6 +9,7 @@
    +use Drupal\ckeditor\CKEditorPluginCssInterface;
    

    Unused.

  7. +++ b/core/modules/ckeditor/src/Plugin/Editor/CKEditor.php
    @@ -421,7 +422,9 @@ public function buildContentsCssJSSetting(EditorEntity $editor) {
    +
    

    Unnecessary whitespace change.

  8. +++ b/core/modules/ckeditor/src/Plugin/Editor/CKEditor.php
    @@ -421,7 +422,9 @@ public function buildContentsCssJSSetting(EditorEntity $editor) {
         $this->moduleHandler->alter('ckeditor_css', $css, $editor);
    +    $css = array_merge($css, $this->ckeditorPluginManager->getCssFiles($editor));
         $css = array_merge($css, _ckeditor_theme_css());
    

    The placement of this call is interesting. It's:

    • After the hook_ckeditor_css_alter() invocation, meaning that you cannot use that hook to remove CSS files added by CKEditor plugins.
    • Before the adding of themes' CSS (ckeditor_stylesheets: []), which is great, because this means themes' CSS can override default styling.

    Given that the alter hook is already being invoked before the theme's CSS is added, this is consistent with that. Furthermore, if a certain CKEditor plugin is present and it has associated CSS, there is no valid reason to remove that CSS.

    So, +1 for this approach.

  9. +++ b/core/modules/ckeditor/tests/modules/src/Plugin/CKEditorPlugin/LlamaCss.php
    @@ -0,0 +1,32 @@
    +class LlamaCss extends Llama implements CKEditorPluginCssInterface {
    

    This is a test implementation. But… there's no new test coverage actually testing this. So, you'll want to expand test coverage:

    1. Add \Drupal\ckeditor\Tests\CKEditorPluginManagerTest::testCssFiles(), look at \Drupal\ckeditor\Tests\CKEditorPluginManagerTest::testEnabledPlugins() for inspiration

    Also: this CKEditor plugin will never be enabled. It will need to implement either \Drupal\ckeditor\CKEditorPluginButtonsInterface or \Drupal\ckeditor\CKEditorPluginConfigurableInterface to be able to become enabled. Read the documentation on \Drupal\ckeditor\CKEditorPluginInterface.

The last submitted patch, 16: 2645100-16.patch, failed testing.

The last submitted patch, 17: 2645100-17.patch, failed testing.

thpoul’s picture

Status: Needs work » Needs review
StatusFileSize
new7.67 KB
new13.15 KB

Here is the patch after Wim's review. Hopefully most of the comments should be covered. I am wondering if we should add additional test coverage at \Drupal\ckeditor\Tests\CKEditorPluginManagerTest::testEnabledPlugins() for Case 3

Status: Needs review » Needs work

The last submitted patch, 21: 2645100-21.patch, failed testing.

wim leers’s picture

I am wondering if we should add additional test coverage […] for case 3

+1

  1. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -27,9 +27,10 @@
    +   * Retrieves enabled plugins iframe instance CSS files, keyed by plugin ID.
    
    +++ b/core/modules/ckeditor/src/CKEditorPluginManager.php
    @@ -184,17 +185,17 @@ public function injectPluginSettingsForm(array &$form, FormStateInterface $form_
    +   * Retrieves enabled plugins iframe instance CSS files, keyed by plugin ID.
    ...
    +   *   Enabled plugins CKEditor CSS files, with plugin IDs as keys and
    

    s/plugins/plugins'/

  2. +++ b/core/modules/ckeditor/src/Tests/CKEditorPluginManagerTest.php
    @@ -123,4 +123,29 @@ function testEnabledPlugins() {
    +  function testCssFiles() {
    

    Looks good.

  3. +++ b/core/modules/ckeditor/src/Tests/CKEditorPluginManagerTest.php
    @@ -123,4 +123,29 @@ function testEnabledPlugins() {
    +  }
     }
    

    Nit: needs an empty line in between.

  4. +++ b/core/modules/ckeditor/tests/modules/src/Plugin/CKEditorPlugin/Llama.php
    @@ -22,6 +22,7 @@
      * @see MetaContextual
      * @see MetaButton
      * @see MetaContextualAndButton
    + * @see MetaCss
    

    Eh, none of those make any sense! Instead of Meta*, they should all be \Drupal\ckeditor_test\Plugin\CKEditorPlugin\Llama*! Can't believe this went unnoticed all this time!

  5. +++ b/core/modules/ckeditor/tests/modules/src/Plugin/CKEditorPlugin/LlamaCss.php
    @@ -15,17 +16,28 @@
    +      drupal_get_path('module', 'ckeditor_test') . '/css/llama_css.css'
    

    s/llama_css.css/llama.css/

thpoul’s picture

Status: Needs work » Needs review
StatusFileSize
new7.41 KB
new17.17 KB

Patch changed according to feedback. Additional test coverage added for Case 3 of \Drupal\ckeditor\Tests\CKEditorPluginManagerTest::testEnabledPlugins(). Also changed a the @see of CKEditorPluginManager::getButtons() to FQCN to match the annotation of CKEditorPluginManager::getCssFiles()

Status: Needs review » Needs work

The last submitted patch, 24: 2645100-24.patch, failed testing.

wim leers’s picture

Issue tags: -Needs tests

Interdiff:

+++ b/core/modules/ckeditor/src/Tests/CKEditorPluginManagerTest.php
@@ -144,8 +154,9 @@ function testCssFiles() {
-      'llama_css' => array(drupal_get_path('module', 'ckeditor_test') . '/css/llama_css.css')
+      'llama_css' => array('core/modules/ckeditor/tests/modules/css/llama.css')

Please bring back the drupal_get_path() call.


Patch:

+++ b/core/modules/ckeditor/src/CKEditorPluginManager.php
@@ -182,4 +183,31 @@ public function injectPluginSettingsForm(array &$form, FormStateInterface $form_
+  }
 }

Needs newline in between.


I think this is ready once the patch is green. Thank you! :) :)

thpoul’s picture

Status: Needs work » Needs review
StatusFileSize
new6.17 KB
new18.88 KB

Thank you Wim for all the time you took reviewing this!

Please bring back the drupal_get_path() call.

I did change also the rest of the test file so it is consistent.

There was a foolish approach in the previous patches at

CKEditor::buildContentsCssJSSetting()

which I corrected in this one. Hopefully we will have green light now :)

thpoul’s picture

StatusFileSize
new831 bytes
new18.88 KB

Oops :)

wim leers’s picture

Status: Needs review » Needs work
+++ b/core/modules/ckeditor/src/Plugin/Editor/CKEditor.php
@@ -422,7 +422,11 @@ public function buildContentsCssJSSetting(EditorEntity $editor) {
-    $css = array_merge($css, $this->ckeditorPluginManager->getCssFiles($editor));
+    // Get a list of all enabled plugins' iframe instance CSS files.
+    $plugins_css = array_reduce($this->ckeditorPluginManager->getCssFiles($editor), function($result, $item) {
+      return array_merge($result, array_keys($item));
+    }, array());
+    $css = array_merge($css, $plugins_css);

Oh, hah, of course! The old code was merging an array of arrays, not an array of strings! :)


  1. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,44 @@
    +   * Retrieves enabled plugins' iframe instance CSS files, keyed by plugin ID.
    ...
    +   *   An array of CSS files. This is a flat list of file paths relative to
    +   *   the Drupal root.
    

    The keyed by plugin ID must be removed. It is untrue because it doesn't make sense: there's no need to key this by plugin ID, because this is already for a specific plugin, hence there is no need for any keying. Look e.g. at DrupalImageCaption and note that there's no keying going on.

  2. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -0,0 +1,44 @@
    +   * @return array &$css
    

    s/array/string[]/

    Why the &$css? AFAICT that should be removed.

  3. +++ b/core/modules/ckeditor/src/CKEditorPluginManager.php
    @@ -182,4 +183,32 @@ public function injectPluginSettingsForm(array &$form, FormStateInterface $form_
    +   *   Enabled plugins CKEditor CSS files, with plugin IDs as keys and
    +   *   CSS file paths relative to the Drupal root (as implemented by
    +   *   getCssFiles()) as values.
    

    80 cols.

    ("CSS file" on the second line can be moved to the end of the first line, etc.)

  4. +++ b/core/modules/ckeditor/src/Tests/CKEditorPluginManagerTest.php
    @@ -121,6 +125,38 @@ function testEnabledPlugins() {
    +    $editor = entity_load('editor', 'filtered_html');
    

    Nit: let's use Editor::load() instead.


This is now very close!

The updated test coverage for CKEditorPluginManagerTest only tests the plugin manager. It's not an integration test. The integration test for loading these CSS files lives at \Drupal\ckeditor\Tests\CKEditorTest::testBuildContentsCssJSSetting(). We should update that test coverage: add the LlamaCss button, and verify that the additional expected CSS file is indeed being loaded.

Only other thing: a change record.

Then this will be ready! :) Thanks again!

thpoul’s picture

StatusFileSize
new8.77 KB
new23.95 KB

Thank you Wim once again for your review!

Nit: let's use Editor::load() instead.

Changed also the rest of the two test files so they are consistent. Still we need to remove the deprecated entity_load('editor'..... all around. But this should be done in an other issue I suppose.

Added test coverage also in \Drupal\ckeditor\Tests\CKEditorTest::testBuildContentsCssJSSetting().

thpoul’s picture

Status: Needs work » Needs review

And here is the draft of the change record: https://www.drupal.org/node/2660712 (Kudos @timplunkett for helping out!)

thpoul’s picture

StatusFileSize
new911 bytes
new23.92 KB

A minor change to match the \Drupal\ckeditor\CKEditorPluginCssInterface::getCssFiles() annotation.

wim leers’s picture

Status: Needs review » Needs work

The CR looks good. I just made it more generic: https://www.drupal.org/node/2660712/revisions/view/9339100/9340882.

  1. +++ b/core/modules/ckeditor/src/CKEditorPluginCssInterface.php
    @@ -35,7 +35,7 @@
    +   * @return string[] $css
    

    You can omit $css entirely. This is not a parameter, it is a return value. Hence it has no name. Look at examples elsewhere :)

  2. +++ b/core/modules/ckeditor/src/CKEditorPluginManager.php
    @@ -185,15 +185,15 @@ public function injectPluginSettingsForm(array &$form, FormStateInterface $form_
    -   * Retrieves enabled plugins' iframe instance CSS files, keyed by plugin ID.
    +   * Retrieves enabled plugins' iframe instance CSS files.
    
    @@ -204,7 +204,7 @@ public function getCssFiles(Editor $editor) {
    -        $css_files[$plugin_id] = $plugin->getCssFiles($editor);
    +        $css_files[] = $plugin->getCssFiles($editor);
    

    Now they're no longer keyed by plugin ID? Why not?

    (This is the plugin manager, and so anything that this does operates on a set of plugins, so it's useful which information is associated with which plugin.)

    In #29.1, I was only referring to a plugin implementing the new interface, not to the plugin manager!

  3. +++ b/core/modules/ckeditor/src/Tests/CKEditorPluginManagerTest.php
    @@ -153,10 +155,12 @@ function testCssFiles() {
         // Case 2: CKEditor iframe instance CSS file.
    -    $css = array(
    -      'llama_css' => array(drupal_get_path('module', 'ckeditor_test') . '/css/llama.css')
    -    );
    -    $this->assertIdentical($css, $this->manager->getCssFiles($editor), 'Iframe instance CSS file found.');
    +    $expected = array();
    +    $expected[] = drupal_get_path('module', 'ckeditor_test') . '/css/llama.css';
    +    $css_files = array_reduce($this->manager->getCssFiles($editor), function($result, $item) {
    +      return array_merge($result, array_values($item));
    +    }, array());
    +    $this->assertIdentical($expected, $css_files, 'Iframe instance CSS file found.');
    

    Consequently these test changes will also need to be reverted.

  4. +++ b/core/modules/ckeditor/src/Tests/CKEditorPluginManagerTest.php
    @@ -8,6 +8,7 @@
     use Drupal\simpletest\KernelTestBase;
    +use Drupal\editor\Entity\Editor;
    

    Super nit: let's order these alphabetically.

  5. +++ b/core/modules/ckeditor/src/Tests/CKEditorTest.php
    @@ -222,7 +222,7 @@ function testGetJSSettings() {
       function testBuildToolbarJSSetting() {
    -    $editor = entity_load('editor', 'filtered_html');
    +    $editor = Editor::load('filtered_html');
     
    

    Nit: let's use Editor::load() instead.

    Changed also the rest of the two test files so they are consistent.

    Hm… I can live with that, but a core committer may shoot this down for that reason. IMO it's fine to just use the proper pattern in new code. (It's not as disconcerting as mixing array() and [] in one file.)

thpoul’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
StatusFileSize
new5.82 KB
new21.39 KB

After a week of having the flu and lying in bed all day with fever here I am with my new patch :D

Hm… I can live with that, but a core committer may shoot this down for that reason. IMO it's fine to just use the proper pattern in new code. (It's not as disconcerting as mixing array() and [] in one file.)

Just to be on the safe side, I followed your advice and only applied the Editor::load() in the new code :)

wim leers’s picture

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

After a week of having the flu and lying in bed all day with fever here I am with my new patch :D

:( I know the feeling! Didn't have the flu, but did get various small things over the past 2 weeks. Hope you're all better now!


I just did a round of manual testing:

+++ b/core/modules/ckeditor/ckeditor.module
@@ -62,13 +62,6 @@ function ckeditor_ckeditor_css_alter(array &$css, Editor $editor) {
-  // Add the filter caption CSS if the text format associated with this text
-  // editor uses the filter_align filter. This is used by the included
-  // CKEditor DrupalImageCaption plugin.
-  if ($editor->getFilterFormat()->filters('filter_align')->status) {
-    $css[] = drupal_get_path('module', 'ckeditor') . '/css/plugins/drupalimagecaption/ckeditor.drupalimagecaption.css';
-  }

+++ b/core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalImageCaption.php
@@ -21,7 +22,7 @@
-class DrupalImageCaption extends PluginBase implements CKEditorPluginInterface, CKEditorPluginContextualInterface {
+class DrupalImageCaption extends PluginBase implements CKEditorPluginInterface, CKEditorPluginContextualInterface, CKEditorPluginCssInterface {

@@ -72,6 +73,15 @@ public function getConfig(Editor $editor) {
+  public function getCssFiles(Editor $editor) {
+    return array(
+      drupal_get_path('module', 'ckeditor') . '/css/plugins/drupalimagecaption/ckeditor.drupalimagecaption.css'
+    );
+  }

This is the key change, and the actual concrete example/test case in Drupal core. This CSS still is loaded after applying the patch, hence it's working correctly.

If I remove the CKEditorPluginCssInterface from the list of implemented interfaces, it disappears.

Ergo, it works correctly even after manual testing. The automated test coverage is also solid.


Per #12, I updated the documentation at https://www.drupal.org/developing/api/8/ckeditor: https://www.drupal.org/node/2617470/revisions/view/9118520/9372512. Including an indicator this is an API available in Drupal 8.1 and later.


Thanks, @thpoul, great work! :)

catch’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalImageCaption.php
    @@ -72,6 +73,15 @@ public function getConfig(Editor $editor) {
    +      drupal_get_path('module', 'ckeditor') . '/css/plugins/drupalimagecaption/ckeditor.drupalimagecaption.css'
    

    Couldn't this use __DIR__?

  2. +++ b/core/modules/ckeditor/src/Tests/CKEditorTest.php
    @@ -253,7 +253,7 @@ function testBuildToolbarJSSetting() {
    -    $editor = entity_load('editor', 'filtered_html');
    +    $editor = Editor::load('filtered_html');
    

    Have a feeling these have been changed in other patches and will conflict.

  3. +++ b/core/modules/ckeditor/src/Tests/CKEditorTest.php
    @@ -261,9 +261,21 @@ function testBuildContentsCssJSSetting() {
    +    $expected[] = file_url_transform_relative(file_create_url(drupal_get_path('module', 'ckeditor_test') . '/ckeditor_test.css'));
    

    Same here with __DIR__.

wim leers’s picture

Fair points. @thpoul, can you address those?

wim leers’s picture

Actually, I don't think #36.1 & .3 make sense (use __DIR__: #2351919: Replace uses of drupal_get_path() with __DIR__ where possible did that for places where we use drupal_get_path(). But here, it's fine to hardcode the path completely. If we want to use __DIR__ instead of hardcoded paths, then let's do that for the entire module in one go. Nowhere else do we do this, and definitely not in this module.

So, I think only #36.2 needs to be addressed.

catch’s picture

did that for places where we use drupal_get_path(). But here, it's fine to hardcode the path completely.

That patch is using drupal_get_path()?

wim leers’s picture

Oh, you're right. I thought I remembered it does not, but you're right. Sorry for the confusion.

thpoul’s picture

Status: Needs work » Needs review
StatusFileSize
new21.25 KB

Rerolled for 8.1.x.

@catch __DIR__ can't be used here because relative paths are expected and tests fail by using it. For example #2351919-30: Replace uses of drupal_get_path() with __DIR__ where possible had the same issue and finally was decided to stay with drupal_get_path('module', 'ckeditor').

wim leers’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.1.x, thanks!

  • catch committed 4503311 on 8.1.x
    Issue #2645100 by thpoul, Wim Leers: CKEditorPluginCssInterface: Allow...
wim leers’s picture

Yay!

Congrats @thpoul, this is a significant addition to Drupal 8 core! :)

wim leers’s picture

Issue summary: View changes

Published the CR.

thpoul’s picture

Epic win! Wouldn't be able to do it without @Wim Leers guidance :) Thank you!

Status: Fixed » Closed (fixed)

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