From f13ee4486dda820409c8d0f5687eab6b718bb605 Mon Sep 17 00:00:00 2001 From: Dylan Wagstaff Date: Mon, 3 Sep 2018 12:11:35 +1200 Subject: [PATCH] Add schema information for HTMLEditorField This allows React form builders (or other such view layer builders in a headless environment) to obtain the details that would otherwise only be rendered in a PHP side template. Some of the details are critical for rendering correctly, and are necessary to be passed through - particularly when moving toward replacing the Entwine initiator for TinyMCE with a React component in `silverstripe/admin`. I new interface method has been added to the abstract class for HTML editor configs in order to facilitate this. It is not itself abstract as this would break backwards compatiblity with any existing custom config (aside from the TinyMCE one which we're editing here), which is most certainly not what we want. --- src/Forms/HTMLEditor/HTMLEditorConfig.php | 13 +++++++++++++ src/Forms/HTMLEditor/HTMLEditorField.php | 13 +++++++++++++ src/Forms/HTMLEditor/TinyMCEConfig.php | 6 ++++++ 3 files changed, 32 insertions(+) diff --git a/src/Forms/HTMLEditor/HTMLEditorConfig.php b/src/Forms/HTMLEditor/HTMLEditorConfig.php index 24b4c8e9a..27a1527aa 100644 --- a/src/Forms/HTMLEditor/HTMLEditorConfig.php +++ b/src/Forms/HTMLEditor/HTMLEditorConfig.php @@ -224,4 +224,17 @@ abstract class HTMLEditorConfig * Initialise the editor on the client side */ abstract public function init(); + + /** + * Provide additional schema data for the field this object configures + * + * @return array + */ + public function getConfigSchemaData() + { + return [ + 'attributes' => $this->getAttributes(), + 'editorjs' => null, + ]; + } } diff --git a/src/Forms/HTMLEditor/HTMLEditorField.php b/src/Forms/HTMLEditor/HTMLEditorField.php index 2d5c23ccc..9bf6096ef 100644 --- a/src/Forms/HTMLEditor/HTMLEditorField.php +++ b/src/Forms/HTMLEditor/HTMLEditorField.php @@ -3,6 +3,7 @@ namespace SilverStripe\Forms\HTMLEditor; use SilverStripe\Assets\Shortcodes\ImageShortcodeProvider; +use SilverStripe\Forms\FormField; use SilverStripe\Forms\TextareaField; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObjectInterface; @@ -23,6 +24,10 @@ class HTMLEditorField extends TextareaField 'Value' => 'HTMLText', ]; + protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_HTML; + + protected $schemaComponent = 'HtmlEditorField'; + /** * Use TinyMCE's GZIP compressor * @@ -185,4 +190,12 @@ class HTMLEditorField extends TextareaField $this->getEditorConfig()->init(); return parent::Field($properties); } + + public function getSchemaStateDefaults() + { + $stateDefaults = parent::getSchemaStateDefaults(); + $config = $this->getEditorConfig(); + $stateDefaults['data'] = $config->getConfigSchemaData(); + return $stateDefaults; + } } diff --git a/src/Forms/HTMLEditor/TinyMCEConfig.php b/src/Forms/HTMLEditor/TinyMCEConfig.php index c1d7d95a7..43788179d 100644 --- a/src/Forms/HTMLEditor/TinyMCEConfig.php +++ b/src/Forms/HTMLEditor/TinyMCEConfig.php @@ -713,6 +713,12 @@ class TinyMCEConfig extends HTMLEditorConfig Requirements::javascript($this->getScriptURL()); } + public function getConfigSchemaData() + { + $data = parent::getConfigSchemaData(); + $data['editorjs'] = $this->getScriptURL(); + return $data; + } /** * Get the current tinyMCE language