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.

Template Questions /

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

Custom text functions for content


Go to End


3 Posts   2509 Views

Avatar
mmh

Community Member, 24 Posts

31 May 2010 at 3:33am

Edited: 31/05/2010 3:33am

Hi there,

I just wan't to know if it's possible to extend Silverstripe in one way:

In every Silverstripe installation you've the opporturnity to change the text e.g. in this way:

$Content.FirstSentence

or
$Content.LimitWordCountXML(10, read more)

Now, I need a custom function like

$Content.MyFancyFunction

I know this was possible if I will hack "sapphire/core/model/fieldtypes/Text.php" but this is not the way i wan't to go.
I've tried to "extend" with the Object::add_extension() method but in this case I will lose "$this->value" in my custom class because it has to extend from "Extension" and can not extend from the "Text"-Class.

Is there a way to do this without hacking the Silverstripe core?

Avatar
Willr

Forum Moderator, 5523 Posts

31 May 2010 at 9:05am

You should be able to use the Extension class. You would then refer to your "$this->value" as you would in a subclass as $this->owner->value. $this->owner gives you the class the extension has been applied to.

The other option is to make a FirstSentence function in your Page.php and instead of using $Content use $FirstSentence in the template.

function FirstSentence() {
$content = $this->Content;

// do whatever you want
$text = ....;

return $text;

Avatar
mmh

Community Member, 24 Posts

31 May 2010 at 10:47am

Thanks, Willr that's it.

I know your second option but I need in my case the "extension". :-)