22
33namespace NSWDPC \Utilities \Trumbowyg ;
44
5- use SilverStripe \Core \Convert ;
6- use SilverStripe \Forms \FormField ;
75use SilverStripe \Forms \TextareaField ;
86use SilverStripe \View \ArrayData ;
97use SilverStripe \View \Requirements ;
10- use Exception ;
11- use DOMDocument ;
128
13-
14- class TrumbowygEditorField extends TextareaField {
15-
16- private static $ casting = [
9+ class TrumbowygEditorField extends TextareaField
10+ {
11+ private static array $ casting = [
1712 'Value ' => 'HTMLText ' ,
1813 ];
1914
20- private static $ include_own_jquery = true ;
15+ private static bool $ include_own_jquery = true ;
2116
2217 /**
2318 * Get field options
2419 * @return array
2520 */
26- protected function getFieldOptions () {
21+ protected function getFieldOptions ()
22+ {
2723 $ options = $ this ->config ()->get ('editor_options ' );
28- if ( empty ($ options ) || !is_array ($ options ) ) {
24+ if ( empty ($ options ) || !is_array ($ options )) {
2925 // Fallback options in case of none configured
3026 $ options = [
3127 "fixedBtnPane " => true ,
@@ -49,15 +45,16 @@ protected function getFieldOptions() {
4945 ]
5046 ];
5147 }
48+
5249 $ options ['tagsToRemove ' ] = self ::getDeniedTags ();
5350 return $ options ;
5451 }
5552
5653 /**
5754 * These tags are denied by default
58- * @return array
5955 */
60- public static function getDeniedTags () {
56+ public static function getDeniedTags (): array
57+ {
6158 return [
6259 'form ' ,
6360 'script ' ,
@@ -79,10 +76,12 @@ public static function getDeniedTags() {
7976 /**
8077 * Returns the field
8178 */
82- public function Field ($ properties = []) {
83- $ this ->setAttribute ('data-tw ' ,'1 ' );
79+ #[\Override]
80+ public function Field ($ properties = [])
81+ {
82+ $ this ->setAttribute ('data-tw ' , '1 ' );
8483
85- if ($ this ->config ()->get ('include_own_jquery ' )) {
84+ if ($ this ->config ()->get ('include_own_jquery ' )) {
8685 Requirements::javascript (
8786 "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js " ,
8887 [
@@ -91,6 +90,7 @@ public function Field($properties = []) {
9190 ]
9291 );
9392 }
93+
9494 Requirements::javascript (
9595 "https://cdn.jsdelivr.net/npm/trumbowyg@2.31.0/dist/trumbowyg.min.js " ,
9696 [
@@ -101,7 +101,7 @@ public function Field($properties = []) {
101101 // import template with options
102102 $ custom_script = ArrayData::create ([
103103 'ID ' => $ this ->ID (),
104- 'Options ' => json_encode ( $ this ->getFieldOptions () )
104+ 'Options ' => json_encode ($ this ->getFieldOptions ())
105105 ])->renderWith ('NSWDPC/Utilities/Trumbowyg/Script ' );
106106 Requirements::customScript (
107107 $ custom_script ,
@@ -121,16 +121,24 @@ public function Field($properties = []) {
121121 /**
122122 * Return the value, sanitised
123123 */
124- public function Value () {
124+ #[\Override]
125+ public function Value ()
126+ {
125127 return $ this ->dataValue ();
126128 }
127129
128130 /**
129131 * Return cleaned data value
130132 */
131- public function dataValue () {
132- $ sanitiser = new ContentSanitiser ();
133- $ this ->value = $ sanitiser ->clean ($ this ->value );
133+ #[\Override]
134+ public function dataValue ()
135+ {
136+ $ value = $ this ->value ;
137+ if (!is_string ($ value )) {
138+ $ value = "" ;
139+ }
140+
141+ $ this ->value = ContentSanitiser::clean ($ value );
134142 return $ this ->value ;
135143 }
136144
0 commit comments