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

?Bug? Overloading Getters


Go to End


4 Posts   2865 Views

Avatar
reekrd

Community Member, 9 Posts

4 February 2009 at 7:39am

Hi.

I've added two extra fields to my HomePage. Demo and Demo2. Both are HTMLText. Before I output any data in those fields I need to change all & signs to & or I'll get an invalid XML error. I don't want to escape all HTML entities since the data I store in those fields are HTML snippets with an embed code from youtube.

In my HomePage.php file I added this.

function getDemo() {
return str_replace('&','&',$this->Demo);
}

and in HomePage.ss I just call it using $Demo

But nothing is returned.

I changed the function to

function getDemo() {
return "ABC";
}

and now ABC is returned.

Changed again to

function getDemo() {
return "ABC" . $this->Demo;
}

now the output is ABCABC

I'm not sure if I'm logically doing something wrong. I read this http://doc.silverstripe.com/doku.php?id=datamodel and there they are using $this->Status so I'm guessing I'm doing it right but it still seems like $this->Demo ends up calling getDemo instead of giving me the value stored in Demo.

If I change the function to return $this->Demo2 or if I rename the function to getMyDemo() then it works but then what's the point of overloading it?

Any thoughts or maybe even better any idea on what would be the best way to allow users to copy and paste a youtube embed snippet without them having to change the & to & manually?

Cheers

Avatar
UncleCheese

Forum Moderator, 4102 Posts

4 February 2009 at 8:35am

What you're trying to do is supported natively by the XML_val function. $this->XML_val('Demo');

Weird that you're using an HTMLText datatype to take a URL. Why not just use a VarChar?

I would skip the getDemo() all together and just overload the Demo property. The template will look for a function first.

function Demo()
{
return str_replace('&','&',$this->Demo);
}

Avatar
reekrd

Community Member, 9 Posts

4 February 2009 at 10:23pm

UncleCheese thanks for your reply.

I'm bit lost when it comes to the different datatypes and which one to use. I'll change it to a normal text field as setting the field like this:
'Demo' => 'Varchar' creates a Varchar(50) in the database and that is not enough to store a youtube embed code.

I'm not using $Demo.XML (which I assume does the same thing as XML_val) because that makes turns everything into HTML entities and the embed code shows up as text instead of actual HTML element on the site.

This is what I want to put into a field in the CMS and when I show it on my homepage I want the video to be there:

<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/tWNaJrbPJZk&hl=en&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/tWNaJrbPJZk&hl=en&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>

I'm not sure I understand the exact difference between overloading getDemo() and Demo() but when I overload the function Demo() it works. Thanks!

Avatar
UncleCheese

Forum Moderator, 4102 Posts

5 February 2009 at 3:19am

Yeah, try casting it as Text. You shouldn't need that str_replace stuff.