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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Filtering Content with onBeforeWrite


Go to End


2 Posts   1438 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?