Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » PageClass in HTMLEditor

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

Page: 1
Go to End
Author Topic: 170 Views
  • Sygmoral
    Avatar
    Community Member
    13 Posts

    PageClass in HTMLEditor Link to this post

    Some of the pages in my Silverstripe 3.1 website have specific styles, so I used the page class to target just those pages. Now the issue is that I am not able to make that distinction in the CMS because it does not include the pageclass.

    I found HtmlEditorConfig::get('cms')->setOption('body_class', page_class); which sounded very promising, but that is a static setting: it is set once upon the first page load within the CMS. Any next page loaded via ajax will use those same editor settings, even if the page is of a different type. So what I really need is something like this, but refreshing every time the HTMLEditor is displayed.

    One way is making the CMS reload upon every page load (i.e. disable the ajax for CMS page loads), but I'm not sure whether that's possible.

    The other (and more prefered) way is making the HTMLEditor config settings come along with an ajax load. At the moment only Content is included in the json; it could easily include a ssTinyMceConfig field, in order to overwrite any current TinyMCE settings. Is there a specific reason this was not done, some kind of issue perhaps? It might just be because it was deemed unnecessary, so in that case I'd be interested to know how to make it happen of course.

    A third way might be to hack into a single config field by adding <script>ssTinyMceConfig.body_class="..."</script> in the returned Content field, but I'm not sure how to do that either, and I'm not sure it would work.

  • Sygmoral
    Avatar
    Community Member
    13 Posts

    Re: PageClass in HTMLEditor Link to this post

    So am I correct in assuming it is currently impossible to have different styles in different PageTypes, within the CMS?

    Framework › Reference › Typography for example mentions how different HTMLEditorFields can have different styles, but that doesn't actually work (anymore) does it? I don't see any class like "Content" being applied anywhere on TinyMCE.

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: PageClass in HTMLEditor Link to this post

    Just in case anyone stumbles upon this thread, I've packaged up a small module that enables you to do this:

    https://github.com/kinglozzer/htmleditorstyinghook

  • Sygmoral
    Avatar
    Community Member
    13 Posts

    Re: PageClass in HTMLEditor Link to this post

    That is awesome, thank you!

    I've implemented it with a single line in Page.php (inside getCMSFields), which puts the correct class name using get_called_class (available since php 5.3):

    $fields->dataFieldByName('Content')->setAttribute('data-mce-body-class', get_called_class());

    170 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.