You are here

public function CustomConfig::getConfig in CKEditor custom config 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/CKEditorPlugin/CustomConfig.php \Drupal\ckeditor_config\Plugin\CKEditorPlugin\CustomConfig::getConfig()
  2. 8 src/Plugin/CKEditorPlugin/CustomConfig.php \Drupal\ckeditor_config\Plugin\CKEditorPlugin\CustomConfig::getConfig()

Returns the additions to CKEDITOR.config for a specific CKEditor instance.

The editor's settings can be retrieved via $editor->getSettings(), but be aware that it may not yet contain plugin-specific settings, because the user may not yet have configured the form. If there are plugin-specific settings (verify with isset()), they can be found at

$settings = $editor
  ->getSettings();
$plugin_specific_settings = $settings['plugins'][$plugin_id];

Parameters

\Drupal\editor\Entity\Editor $editor: A configured text editor object.

Return value

array A keyed array, whose keys will end up as keys under CKEDITOR.config.

Overrides CKEditorPluginInterface::getConfig

File

src/Plugin/CKEditorPlugin/CustomConfig.php, line 52

Class

CustomConfig
Defines the "customconfig" plugin.

Namespace

Drupal\ckeditor_config\Plugin\CKEditorPlugin

Code

public function getConfig(Editor $editor) {
  $config = [];
  $settings = $editor
    ->getSettings();
  if (!isset($settings['plugins']['customconfig']['ckeditor_custom_config'])) {
    return $config;
  }
  $custom_config = $settings['plugins']['customconfig']['ckeditor_custom_config'];

  // Check if custom config is populated.
  if (!empty($custom_config)) {

    // Build array from string.
    $config_array = preg_split('/\\R/', $custom_config);

    // Loop through config lines and append to editorSettings.
    foreach ($config_array as $value) {
      $exploded_value = explode(" = ", $value);

      // Convert true/false strings to boolean values.
      if (strcasecmp($exploded_value[1], 'true') == 0 || strcasecmp($exploded_value[1], 'false') == 0) {
        $exploded_value[1] = filter_var($exploded_value[1], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
      }

      // Convert numeric values to integers.
      if (is_numeric($exploded_value[1])) {
        $exploded_value[1] = (int) $exploded_value[1];
      }
      $config['ckeditor_custom_config'][$exploded_value[0]] = $exploded_value[1];
    }
  }
  return $config;
}