5093 Posts in 1516 Topics by 1113 members
| Go to End | Next > | |
| Author | Topic: | 1729 Views |
-
add method to core class

8 April 2009 at 9:33am
Hi there,
I've searched around the forum but cannot find the answer to what I'm looking for. I'm quite new to OOP, so this possibly is quite simple..
I'm wanting to add a method to the core class Image (without adding it directly to the class due to upgrades etc).
Does anyone have any ideas / advice on how to do this?
Many thanks!
-
Re: add method to core class

8 April 2009 at 10:51am Last edited: 8 April 2009 10:54am
Hi.
You can write your own class MyImage by extending the core class Image and add your custom function there
e.g.:<?php
class MyImage extends Image {//your custom function...
public function myFunction() {...
}}
?>And then call your new class instead of the old class in your /mysite/_config.php like this:
Object::useCustomClass('OldClass','NewClass');Hope that helps. And by the way, some other helpful links from other threads:
http://silverstripe.org/template-questions/show/255567
http://www.silverstripe.org/customising-the-cms/show/257393Cheers,
Christian -
Re: add method to core class

8 April 2009 at 11:49am
Thanks for your help, I appreciate it.. but.. I have already tried that, but was not getting any results :S
Just to confirm what I've done is correct..
/mysite/_config.php
Object::useCustomClass('Image','MyImage');
/mysite/code/MyImage.php (created this file)
<?php
class MyImage extends Image {
public function generateTitle() {
return "abc";
}
}?>
I then ran /db/build/ which made a update to the DB.
In my template Page.ss I have..
<% control TopImage %>
<img src="$URL" title="$generateTitle" alt="$Title" />
<% end_control %>But I don't get 'abc' as my title attribute?
Any ideas?
-
Re: add method to core class

9 April 2009 at 11:41pm
It looks ok for me.
Hmmm, sorry, no ideas...
I'm currently learning the wonders of MVC for myself, but maybe it helps if you create a MyImage_Controller class and put your generateTitle() function there...
Cheers,
Christian -
Re: add method to core class

10 April 2009 at 11:01am
Hello Chrisdarl!!
I just tried your code and it works fine. As you did I extended Image and added my function. In added MyImage to my home page to test it so in HomePage.php I wrote:static $has_one = array(
'TopImage'=>'MyImage'
);And then I added the MyImage to the CMS by doing:
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Images", new ImageField('TopImage'));
return $fields;
}And in my templates/HomePage.ss I access the function by doing:
<% control TopImage %>
<img src="$URL" title="$generateTitle"/>
<% end_control %>I checked the Title attribute and it is 'abc', I hope this will help you to see what you are doing wrong.
If not, post again I can try to help you.
Good Luck, bye.
-
Re: add method to core class

10 April 2009 at 10:55pm
Thanks for your reply and testing it out.. I did get that working, but what I'm wanting to do is actually add a method tot he core class, rather than creating a new custom version of it. So that any Image has the method available to them. I did then that the useCustomClass would do the job.. but it just doesn't seem to work
-
Re: add method to core class

11 April 2009 at 10:45pm
Hi chrisdarl
You could try a custom DataObjectDecorator (http://doc.silverstripe.com/doku.php?id=dataobjectdecorator). I used this to add custom functionality to Files in my filemanager module (http://silverstripe.org/has-many-file-manager-module/) and it worked very well.
-
Re: add method to core class

13 April 2009 at 9:43pm
Thanks for the advice, I shall give that a try and post back.
Cheers everyone
| 1729 Views | ||
| Go to Top | Next > |



