diff --git a/src/services/Export.php b/src/services/Export.php index 9c914d7..0200be9 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 da9f142..ec5aa35 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 @@ -156,6 +162,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([ 'name' => $fieldInfo['name'], 'handle' => $fieldInfo['handle'], @@ -280,6 +290,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);