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.

Customising the CMS /

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

add method to core class


Go to End


9 Posts   4389 Views

Avatar
chrisdarl

Community Member, 33 Posts

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!

Avatar
Invader_Zim

Community Member, 141 Posts

8 April 2009 at 10:51am

Edited: 08/04/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/257393

Cheers,
Christian

Avatar
chrisdarl

Community Member, 33 Posts

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?

Avatar
Invader_Zim

Community Member, 141 Posts

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

Avatar
Jardiamj

Community Member, 17 Posts

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.

Avatar
chrisdarl

Community Member, 33 Posts

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 :(

Avatar
bummzack

Community Member, 904 Posts

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.

Avatar
chrisdarl

Community Member, 33 Posts

13 April 2009 at 9:43pm

Thanks for the advice, I shall give that a try and post back.

Cheers everyone

Go to Top