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

Best way to alter $Content?


Go to End


5 Posts   1032 Views

Avatar
Jare

Community Member, 39 Posts

4 December 2014 at 1:45am

Hi,
my purpose is to modify page content in a separate module to cloak email addresses. I have made an Extension class and applied it to SiteTree. Inside the Extension class I have a public function Content() which does the stuff I want and the function itself works fine. The whole thing is working well on every Page object, but when I go to Contact page that is of type UserDefinedForm, my custom function does not have any effect. I guess it's never called because UserDefinedForm overrides it. I have tried to apply the extension also directly to UserDefinedForm instead of via SiteTree, but with no luck. How to make my function affect everything that is derived from SiteTree?

Another, closely relating question is that what would be the easiest way to do some 'search and replace' style content modification that replaces keywords (or regex patterns) with values? Is the only way to perform a string search operation on the Content property of Page (or whatever class) or does SilverStripe provide some more sophisticated way to do this?

Thank you for your support!

Avatar
WandL

Community Member, 9 Posts

4 December 2014 at 11:14am

Hi Jare,

Sorry can't help with first part of the question as haven't really used the UserDefinedForms module, with the second part you could use short codes for that I am guessing?

http://doc.silverstripe.org/framework/en/reference/shortcodes

Hope this helps.

Avatar
Jare

Community Member, 39 Posts

5 December 2014 at 11:36pm

Hi WandL!

Thanks for the tip! I wonder how I've never ended up to see that page before.

Avatar
kinglozzer

Community Member, 187 Posts

6 December 2014 at 12:00am

Edited: 06/12/2014 12:01am

Hi Jare,

As an alternative, you could perform your modifications in an extension that's applied to HTMLText. For example:

class HTMLTextExtension extends Extension {

	public function FooBar() {
		return str_replace('foo', 'bar', $this->owner->forTemplate());
	}

}

Apply the extension to HTMLText and you could then use $Content.FooBar in your template.

For the second part, you could do that in an extension again. For complex DOM manipulation, I’d use the DOMDocument PHP class. There’s an example of how the framework uses it to parse anchors here: http://api.silverstripe.org/3.1/source-class-HtmlEditorField.html#67-95.

Hope this helps,
Loz

Avatar
Jare

Community Member, 39 Posts

6 December 2014 at 12:15am

Hi kinglozzer!

Thanks to you too! That's a nice way to do it :). I actually had already extended the HTMLText datatype because I also wanted to cloak email addresses that are defined in my custom SiteConfig. It just didn't come to my mind that I can also use the same approach for the page content :). My bad :D. Hope these answers help some others too :).