Jump to:

5520 Posts in 1734 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Does anyone know how to use set_active() ?

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

Page: 1
Go to End
Author Topic: 868 Views
  • Chris J
    Avatar
    Community Member
    10 Posts

    Does anyone know how to use set_active() ? Link to this post

    Hi

    Sorry if I explain things in a long way, I want to be specific though...

    I would like to have a set of TinyMCE fields, each with different options, modules and buttons for different field types. I think I almost have a way to get this but not quite. Here's what I have so far:

    Silverstripe version 2.4.2

    In the file: /mysite/_config.php
    I have defined my own set of options, buttons and plugins for TinyMCE with a new name as below:

    HtmlEditorConfig::get('reduced')->setOptions(array(
    // My options...
    );
    HtmlEditorConfig::get('reduced')->disablePlugins('table', 'contextmenu');
    HtmlEditorConfig::get('reduced')->enablePlugins('media', 'fullscreen');
    HtmlEditorConfig::get('reduced')->enablePlugins(array('ssbuttons' => '../../../cms/javascript/tinymce_ssbuttons/editor_plugin_src.js'));
    HtmlEditorConfig::get('reduced')->setButtonsForLine(1,
       'bold','separator','pastetext','pasteword','separator','undo','redo','separator',
       'sslink','unlink', 'anchor', 'separator','bullist','numlist','ssimage', 'ssflash', 'fullscreen'
    );
    HtmlEditorConfig::get('reduced')->setButtonsForLine(2);
    HtmlEditorConfig::get('reduced')->setButtonsForLine(3);

    this works fine and in my home page php file - mysite/code/HomePage.php
    I have:

    function getCMSFields() {
    $fields = parent::getCMSFields();
    HtmlEditorConfig::set_active('reduced');
    $reducedEditor = new HtmlEditorField('reducedEditor','Reduced-Editor',1);
    $fields->addFieldToTab('Root.Content.Main',$reducedEditor,'Content');
    // rest of fields...
    }

    In another file I created so I could set the active WYSIWYG settings back to the original - mysite/code/OtherPage.php
    I have:

    function getCMSFields() {
       $fields = parent::getCMSFields();
       HtmlEditorConfig::set_active('cms');
       return $fields;
    }

    Here's the unexpected bit...

    regardless of which page type I am editing the first loaded WYSIWYG settings are used.
    eg: If from editing the home page I go to another section of the admin, for example Security, then I go back to the Pages section (it loads the last page I was editing), the 'reduced' settings are used. But then they are also used for every HtmlEditorField on any page type.

    Then, if from another page which uses OtherPage.php, I go to another admin section and then back to the pages section, the default 'cms' options are loaded. Yes then 'cms' settings are used accross all page types too.

    I expected set_active to be able to set the TinyMCE settings on each page the way I have used it.
    Has anyone come up with a good workaround? I'm sure I'm not the only person who would like to be able to do this.

    I was wondering if I could rename the HtmlEditorField to something else eg: ReducedHtmlEditorField and set the options specific to that.
    Then based on whether I use HtmlEditorField or ReducedHtmlEditorField the custom options would be used but I'm not sure how or if it would still have the same issues.

    eg:

    function getCMSFields() {
    $fields = parent::getCMSFields();

    $defaultEditor = new HtmlEditorField('defaultEditor','Reduced-Editor',1);
    $fields->addFieldToTab('Root.Content.Main',$defaultEditor,'Content');

    $reducedEditor = new ReducedHtmlEditorField('reducedEditor','Reduced-Editor',1);
    $fields->addFieldToTab('Root.Content.Main',$reducedEditor,'Content');

    // rest of fields...
    }

  • Chris J
    Avatar
    Community Member
    10 Posts

    Re: Does anyone know how to use set_active() ? Link to this post

    So I have had a go at trying to create a new form field type.

    I created a file: ReducedEditorField.php

    class ReducedEditorField extends HtmlEditorField {

       public static function include_js() {
          Requirements::javascript(MCE_ROOT . 'tiny_mce_src.js');
          Requirements::customScript(ReducedEditorConfig::get_active()->generateJS(), 'ReducedEditorConfig');
       }
    }

    I created a file: ReducedEditorConfig.php

    class ReducedEditorConfig extends HtmlEditorConfig {

       static $configs = array();

       static function get($identifier = 'default') {
          if (!array_key_exists($identifier, self::$configs)) self::$configs[$identifier] = new ReducedEditorConfig();
          return self::$configs[$identifier];
       }
    }

    Then in _config.php added:

    ReducedEditorConfig::get('cms')->setOptions(
    // custom options...
    );
    ReducedEditorConfig::get('cms')->disablePlugins('table', 'contextmenu');
    ReducedEditorConfig::get('cms')->enablePlugins('media', 'fullscreen');
    ReducedEditorConfig::get('cms')->enablePlugins(array('ssbuttons' => '../../../cms/javascript/tinymce_ssbuttons/editor_plugin_src.js'));
    ReducedEditorConfig::get('cms')->setButtonsForLine(1,
       'bold','separator','pastetext','pasteword','separator','undo','redo','separator',
       'sslink','unlink', 'anchor', 'separator','bullist','numlist','ssimage', 'ssflash', 'fullscreen'
    );
    ReducedEditorConfig::get('cms')->setButtonsForLine(2);
    ReducedEditorConfig::get('cms')->setButtonsForLine(3);

    And of course tried to use my new field type in HomePage.php as below

    function getCMSFields() {
    ...
    $reducedEditor = new ReducedEditorField('reducedEditor','Reduced-Editor',1);
    $fields->addFieldToTab('Root.Content.Main',$reducedEditor,'Content');
    ...
    }

    The field was loaded with the default for HtmlEditorField though so no luck there yet.

    Any Ideas?

    868 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.