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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

Filtering Content with onBeforeWrite


Go to End


2 Posts   1078 Views

Avatar
adambuczek

Community Member, 5 Posts

14 March 2014 at 11:42pm

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

Community Member, 5 Posts

16 March 2014 at 3:17am

Edited: 16/03/2014 8:34am

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?