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.

Data Model Questions

Filtering Content with onBeforeWrite


Reply

2 Posts   349 Views

Avatar
adambuczek

14 March 2014 at 11:42pm Community Member, 5 Posts

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.

Avatar
adambuczek

16 March 2014 at 3:17am (Last edited: 16 March 2014 8:34am), Community Member, 5 Posts

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?