Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

HTMLEditorConfig - per Editor configuration - question from a humble n00b.


Go to End
Reply


10 Posts   3273 Views

Avatar
mats

Community Member, 5 Posts

18 August 2012 at 9:32am

Edited: 20/08/2012 12:37am

Mmh - Uncle Cheese's BootstrapForm Module rocks, but it did not help me with my problem...

Instead I have - ahem probably - succeded in adapting CustomConfigHtmlEditorField to my needs:

CustomHtmlEditorfield::create($DBObject, $Title, $NameOfExistingEditorConfig, $Value)

This gives you a HtmlEditorfield based on a configuration $NameOfExistingEditorConfig.
Edit: Above became obsolete, see below.

To the best of my knowledge, this might be a pretty bad hack: I haven't really figured out Entwine (which seems pretty cool) and the PHP/JavaScript Interactions in the backend yet and I am not a programmer.

Still, here's my code for CustomHtmlEditorfield. It might not be pretty but - for now - it works for me. Any criticism is very much appreciated. Maybe I just reinvented the wheel, but the advocated solutions did not work for me...

Edit: Did not work as expected after all - created multiple invisible TinyMCE instances :-(

New approach:

class CustomHtmlEditorField extends HtmlEditorField {

protected $config = 'cms';

public function __construct($name, $title = null, $value = '') {
      TextareaField::__construct($name, $title, $value);
}

public function setConfig($config = 'cms') {
$this->config = $config;
return $this;
}

function Field($properties = array()) {
$this->include_customjs();
return parent::Field($properties);
}

protected function include_customjs() {
$this->addExtraClass('htmleditor');
$configObj = HtmlEditorConfig::get($this->config);
$configIdString = '_'.$this->config;
$this->addExtraClass('editorconfig'.$configIdString);

Requirements::javascript(MCE_ROOT . 'tiny_mce_src.js');
Requirements::customScript(
str_replace('ssTinyMceConfig','ssTinyMceConfig'.$configIdString,$configObj->generateJS()).
"(function($){
$.entwine('ss', function($) {
$('textarea.htmleditor.customhtmleditor.editorconfig$configIdString').entwine({
redraw: function() {
var config = ssTinyMceConfig$configIdString, self = this, ed = this.getEditor();
self.css('visibility', '');
ed.create(this.attr('id'), config, function() {
self.css('visibility', 'visible');
});
}
});
});
})(jQuery);
", 'htmlEditorConfig'.$configIdString);
}
}

Editor configurations are now loaded via

CustomHtmlEditorField::setConfig('my_editor_configuration_name')

Well, i guess it's still a pretty crude hack :-)

Avatar
theruss

Community Member, 2 Posts

11 March 2014 at 10:27am

Edited: 11/03/2014 10:39am

As a follow-up and as @mats found out, there is no way to have multiple HTMLEditorConfig's in SS 3.x out-of-the-box (As at March '14). Also, we shouldn't have to extend HTMLEditorField or HTMLEditorConfig to achieve this, so a core-patch will be required.

I needed some functionality almost identical to @matt and after a chat with Hamish, I came up with the following:

https://github.com/phptek/silverstripe-framework/tree/issue/multi-editor

- Multiple HTMLConfig's defined in _config.php = check.
- No additional fields = check.
- Tested: Only FF and Chrome on OS X.
- To add own config:

1). Adapt/create your own config in your module's or mysite's _config.php file.
2). Apply the patch from the repo above
3). Add an extra boolean param to your HtmlEditorField construct call e.g.

HtmlEditorConfig::set_active('my-basic-config');
new HtmlEditorField('MyField', 'My label', 3, 20, true);

4). Voila.

Caveats:

1). While this framework patch does work as far as switching between PageTypes with differing HTMLEditorConfig's set goes, there is a small JS glitch when swapping between them. If you switch from a Page with a custom config, back to a Page using the default config (This on a CWP SS 3.1 site) you'll notice that TinyMCE's default dropdown templates show for the <paragraph> and <heading> dropdowns. Simply reloading the page, "fixes" this.

2). This is untested in anything but Firefox and Chrome on OS X

I'm guessing some sort of editor JS redraw needs to be called, but not sure at this point what or where as yet.

Cheers
Russ

Go to Top