function SmartDefaultSettings::createSettingsFromCKEditor4

Same name in other branches
  1. 10 core/modules/ckeditor5/src/SmartDefaultSettings.php \Drupal\ckeditor5\SmartDefaultSettings::createSettingsFromCKEditor4()

Creates equivalent CKEditor 5 settings from CKEditor 4 settings.

Parameters

array $ckeditor4_settings: The value for "settings" in a Text Editor config entity configured to use CKEditor 4.

\Drupal\ckeditor5\HTMLRestrictions $text_format_html_restrictions: The restrictions of the text format, to allow an upgrade plugin to inspect the text format's HTML restrictions to make a decision.

Return value

array An array with two values: 1. An equivalent value for CKEditor 5. 2. Messages explaining upgrade path issues.

Throws

\LogicException Thrown when an upgrade plugin is attempting to generate plugin settings for a CKEditor 4 plugin upgrade path that have already been generated.

1 call to SmartDefaultSettings::createSettingsFromCKEditor4()
SmartDefaultSettings::computeSmartDefaultSettings in core/modules/ckeditor5/src/SmartDefaultSettings.php
Computes the closest possible equivalent settings for switching to CKEditor 5.

File

core/modules/ckeditor5/src/SmartDefaultSettings.php, line 433

Class

SmartDefaultSettings
Generates CKEditor 5 settings for existing text editors/formats.

Namespace

Drupal\ckeditor5

Code

private function createSettingsFromCKEditor4(array $ckeditor4_settings, HTMLRestrictions $text_format_html_restrictions) : array {
    $settings = [
        'toolbar' => [
            'items' => [],
        ],
        'plugins' => [],
    ];
    $messages = [];
    // First: toolbar items.
    // @see \Drupal\ckeditor\CKEditorPluginButtonsInterface
    foreach ($ckeditor4_settings['toolbar']['rows'] as $row) {
        foreach ($row as $group) {
            $some_added = FALSE;
            foreach ($group['items'] as $cke4_button) {
                try {
                    $equivalent = $this->upgradePluginManager
                        ->mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem($cke4_button, $text_format_html_restrictions);
                } catch (\OutOfBoundsException $e) {
                    $this->logger
                        ->warning('The CKEditor 4 button %button does not have a known upgrade path. If it allowed editing markup, then you can do so now through the Source Editing functionality.', [
                        '%button' => $cke4_button,
                    ]);
                    $messages[MessengerInterface::TYPE_WARNING][] = $this->t('The CKEditor 4 button %button does not have a known upgrade path. If it allowed editing markup, then you can do so now through the Source Editing functionality.', [
                        '%button' => $cke4_button,
                    ]);
                    continue;
                }
                if ($equivalent) {
                    $settings['toolbar']['items'] = array_merge($settings['toolbar']['items'], $equivalent);
                    $some_added = TRUE;
                }
            }
            // Add a CKEditor 5 toolbar group separator for every group.
            if ($some_added) {
                $settings['toolbar']['items'][] = '|';
            }
        }
    }
    // Remove the trailing CKEditor 5 toolbar group separator.
    array_pop($settings['toolbar']['items']);
    // Strip the CKEditor 4 buttons without a CKEditor 5 equivalent.
    $settings['toolbar']['items'] = array_filter($settings['toolbar']['items']);
    // Second: plugin settings.
    // @see \Drupal\ckeditor\CKEditorPluginConfigurableInterface
    $enabled_ckeditor4_plugins_with_settings = $ckeditor4_settings['plugins'];
    foreach ($enabled_ckeditor4_plugins_with_settings as $cke4_plugin_id => $cke4_plugin_settings) {
        try {
            $cke5_plugin_settings = $this->upgradePluginManager
                ->mapCKEditor4SettingsToCKEditor5Configuration($cke4_plugin_id, $cke4_plugin_settings);
            if ($cke5_plugin_settings === NULL) {
                continue;
            }
            assert(count($cke5_plugin_settings) === 1);
            $cke5_plugin_id = array_keys($cke5_plugin_settings)[0];
            if (isset($settings['plugins'][$cke5_plugin_id])) {
                throw new \LogicException(sprintf('The %s plugin settings have already been upgraded. Only a single @CKEditor4To5Upgrade is allowed to migrate the settings for a particular CKEditor 4 plugin.', $cke5_plugin_id));
            }
            $settings['plugins'] += $cke5_plugin_settings;
        } catch (\OutOfBoundsException $e) {
            $this->logger
                ->warning('The %cke4_plugin_id plugin settings do not have a known upgrade path.', [
                '%cke4_plugin_id' => $cke4_plugin_id,
            ]);
            $messages[MessengerInterface::TYPE_WARNING][] = $this->t('The %cke4_plugin_id plugin settings do not have a known upgrade path.', [
                '%cke4_plugin_id' => $cke4_plugin_id,
            ]);
            continue;
        }
    }
    return [
        $settings,
        $messages,
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.