Skip to content

Commit

Permalink
Field::EVENT_MODIFY_CONFIG
Browse files Browse the repository at this point in the history
Resolves #70
  • Loading branch information
brandonkelly committed Apr 17, 2023
1 parent bfcd260 commit 612c1c5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- Added the `ckeditor/convert` command, for converting Redactor fields/configs to CKEditor. ([#68](https://github.com/craftcms/ckeditor/pull/68))
- “Config Options” settings can now be entered as JSON, with autocompletion and automatic JavaScript-to-JSON reformatting on paste. ([#66](https://github.com/craftcms/ckeditor/pull/66), [#67](https://github.com/craftcms/ckeditor/pull/67))
- The `placeholder` config option now gets translated, when the config options are defined as JSON.
- Added `craft\ckeditor\events\ModifyConfigEvent`.
- Added `craft\ckeditor\Field::EVENT_MODIFY_CONFIG`. ([#70](https://github.com/craftcms/ckeditor/discussions/70))
- Fixed a bug where editor toolbars would overlap the control panel header when scrolling.

## 3.0.0 - 2023-04-12
Expand Down
20 changes: 18 additions & 2 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Craft;
use craft\base\ElementInterface;
use craft\ckeditor\events\DefineLinkOptionsEvent;
use craft\ckeditor\events\ModifyConfigEvent;
use craft\ckeditor\web\assets\ckeditor\CkeditorAsset;
use craft\elements\Asset;
use craft\elements\Category;
Expand Down Expand Up @@ -58,6 +59,12 @@ class Field extends HtmlField
*/
public const EVENT_DEFINE_LINK_OPTIONS = 'defineLinkOptions';

/**
* @event ModifyConfigEvent The event that is triggered when registering the link options for the field.
* @since 3.1.0
*/
public const EVENT_MODIFY_CONFIG = 'modifyConfig';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -177,7 +184,7 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st

$id = Html::id($this->handle);
$idJs = Json::encode($view->namespaceInputId($id));
$configJs = Json::encode([
$baseConfig = [
'ui' => [
'viewportOffset' => ['top' => 50],
],
Expand Down Expand Up @@ -205,7 +212,14 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st
'transforms' => $this->_transforms(),
'defaultTransform' => $defaultTransform?->handle,
'elementSiteId' => $element?->siteId,
];

// Give plugins/modules a chance to modify the config
$event = new ModifyConfigEvent([
'baseConfig' => $baseConfig,
'ckeConfig' => $ckeConfig,
]);
$this->trigger(self::EVENT_MODIFY_CONFIG, $event);

if (isset($ckeConfig->options)) {
// translate the placeholder text
Expand All @@ -224,8 +238,10 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st
$configOptionsJs = '{}';
}

$baseConfigJs = Json::encode($event->baseConfig);

$view->registerJs(<<<JS
Ckeditor.create($idJs, Object.assign($configJs, $configOptionsJs));
Ckeditor.create($idJs, Object.assign($baseConfigJs, $configOptionsJs));
JS,
View::POS_END,
);
Expand Down
30 changes: 30 additions & 0 deletions src/events/ModifyConfigEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/

namespace craft\ckeditor\events;

use craft\ckeditor\CkeConfig;
use yii\base\Event;

/**
* ModifyConfigEvent class.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.1.0
*/
class ModifyConfigEvent extends Event
{
/**
* @var array The base field config array that the CKEditor config options will be merged into
*/
public array $baseConfig;

/**
* @var CkeConfig $ckeConfig The CKEditor config
*/
public CkeConfig $ckeConfig;
}

0 comments on commit 612c1c5

Please sign in to comment.