Score:2

CKEditor 5 - access PHP plugin configuration from JS plugin?

in flag

I have provided a CKEditor 5 plugin a PHP configuration class. I am able to set and store configuration for the text editor plugin now, but do not know how to access that configuration value(s) from my JS plugin files that create the editor button/logic. How do you do that?

apaderno avatar
us flag
I looked at the plugins implemented by Drupal core, but finding an answer is not that easy. It seems the documentation on that is scarce, probably because the integration between Drupal and CkEditor5 is relatively recent.
Score:1
in flag

Figured this out.

Your editor plugin YAML definition needs to specify a class and path to that class like so:

class: Drupal\mymodule\Plugin\CKEditor5Plugin\MyClass

Within that plugin, implement the following method:

  /**
   * {@inheritdoc}
   */
  public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor): array {
    $options = $static_plugin_config;
    $config = $this->getConfiguration();

    return [
      'your_ckeditor5_plugin_name' => [
        // an array or multidimensional array of config values
        // ex. 'foo' => $config['foo'] ?? $options['foo'], 
      ]
    ];
  }

In your editor plugin JS, you can then access those values:

const config = this.editor.config.get('your_ckeditor5_plugin_name');

You need an appropriate schema too for both the Drupal configuration as well as the plugin configuration in the mymodule.ckeditor5.yml plugin definition. These serve as the default values, while getDynamicPluginConfig overrides that from what the user has provided in the configuration screen for that text format.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.