Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Filtering Content with onBeforeWrite

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

Page: 1
Go to End
Author Topic: 321 Views
  • adambuczek
    Avatar
    Community Member
    5 Posts

    Filtering Content with onBeforeWrite Link to this post

    I'm a SS newbie but I really love the system!

    I'm Polish. In my language leaving one letter words at the end of the line is a mistake. I know i can deal with it adding   after such words.

    I'm trying to modify Content using onBeforeWrite() inside Page_Controller so it would work on every custom class extending from it.

       public function onBeforeWrite() {
          parent::onBeforeWrite();

          $text = $this->getField('Content');
          $text = preg_replace('/(?<=\b[a-z]) /i', '&nbsp;', $text);
          $this->setField('Content',$text);
       
    }

    It's not working at all and I don't know why.

    Is there any way to modify content before it gets wrapped into HTML or should I try to create plugin for TinyMCE that takes care of it?
    Is there anything similar to Drupal or Wordpress text filters?

    I did some research but it all leads to documentation which isn't much help for a newbie - at least in this case.

  • adambuczek
    Avatar
    Community Member
    5 Posts

    Re: Filtering Content with onBeforeWrite Link to this post

    So I figured that out - code was fine but it should go into Page class, not Page_Controller class.

    .:EDIT:.
    The Content is already HTML so I edited my regular expression to omit spaces in "<p class..." or "projekt&oacute;w ".

    public function onBeforeWrite() {
       parent::onBeforeWrite();

       $text = $this->getField('Content');
       $text = preg_replace('/(?<=\b(?<![<;])[a-z])[ ]/i', '&nbsp;', $text);
       $this->setField('Content',$text);

    }

    Can I check for current language so I can change the spaces in polish only?

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