From 5250de3388d70715e972718944fccf379fa03658 Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Sat, 27 Jan 2024 21:16:54 +1100 Subject: [PATCH] Add support for CKEditor and separate editor configs --- src/services/Export.php | 30 ++++++++++++++++++++++++++++++ src/services/Import.php | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/services/Export.php b/src/services/Export.php index 97ba46f..9f05722 100644 --- a/src/services/Export.php +++ b/src/services/Export.php @@ -10,12 +10,17 @@ use yii\base\Component; +use Throwable; + use verbb\supertable\SuperTable; use verbb\supertable\fields\SuperTableField; use benf\neo\Plugin as Neo; use benf\neo\Field as NeoField; +use craft\ckeditor\Plugin as CkEditor; +use craft\ckeditor\Field as CkEditorField; + class Export extends Component { // Public Methods @@ -57,6 +62,12 @@ public function export(array $fieldIds): array } } + if (Plugin::isPluginInstalledAndEnabled('ckeditor')) { + if ($field instanceof CkEditorField) { + $newField['settings'] = $this->processCkEditor($field); + } + } + $fields[] = $newField; } } @@ -216,4 +227,23 @@ public function processSuperTable($field): array return $fieldSettings; } + + public function processCkEditor($field): array + { + $fieldSettings = $field->settings; + $ckeConfigUid = $fieldSettings['ckeConfig'] ?? null; + + if ($ckeConfigUid) { + try { + $ckeConfig = CkEditor::getInstance()->getCkeConfigs()->getByUid($ckeConfigUid); + + if ($ckeConfig) { + $fieldSettings['ckeConfig'] = $ckeConfig; + } + } catch (Throwable) { + } + } + + return $fieldSettings; + } } diff --git a/src/services/Import.php b/src/services/Import.php index 83e2f50..4d1ff9d 100644 --- a/src/services/Import.php +++ b/src/services/Import.php @@ -9,6 +9,12 @@ use yii\base\Component; +use Throwable; + +use craft\ckeditor\Plugin as CkEditor; +use craft\ckeditor\Field as CkEditorField; +use craft\ckeditor\CkeConfig; + class Import extends Component { // Public Methods @@ -157,6 +163,10 @@ public function import(array $fields): array $fieldInfo['settings'] = $this->processPosition($fieldInfo); } + if ($fieldInfo['type'] === 'craft\ckeditor\Field') { + $fieldInfo['settings'] = $this->processCkEditor($fieldInfo); + } + $field = Craft::$app->getFields()->createField([ 'groupId' => $fieldInfo['groupId'], 'name' => $fieldInfo['name'], @@ -282,6 +292,35 @@ public function processPosition($fieldInfo): array return $settings; } + public function processCkEditor($fieldInfo): array + { + $settings = $fieldInfo['settings']; + + // Get or create the config from its UID + $ckeConfigData = $settings['ckeConfig'] ?? null; + $ckeConfigUid = $settings['ckeConfig']['uid'] ?? null; + + if ($ckeConfigUid) { + try { + $ckeConfig = CkEditor::getInstance()->getCkeConfigs()->getByUid($ckeConfigUid); + } catch (Throwable) { + $ckeConfig = null; + } + + if (!$ckeConfig) { + $ckeConfig = new CkeConfig($ckeConfigData); + + CkEditor::getInstance()->getCkeConfigs()->save($ckeConfig); + + $ckeConfigUid = $ckeConfig->uid; + } + + $settings['ckeConfig'] = $ckeConfigUid; + } + + return $settings; + } + public function getData($json) { $data = Json::decode($json, true, 512);