Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » add method to core class

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

Page: 1 2
Go to End
Author Topic: 2040 Views
  • chrisdarl
    Avatar
    Community Member
    33 Posts

    add method to core class Link to this post

    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!

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: add method to core class Link to this post

    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

  • chrisdarl
    Avatar
    Community Member
    33 Posts

    Re: add method to core class Link to this post

    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?

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: add method to core class Link to this post

    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

  • Jardiamj
    Avatar
    Community Member
    17 Posts

    Re: add method to core class Link to this post

    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.

  • chrisdarl
    Avatar
    Community Member
    33 Posts

    Re: add method to core class Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: add method to core class Link to this post

    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.

  • chrisdarl
    Avatar
    Community Member
    33 Posts

    Re: add method to core class Link to this post

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

    Cheers everyone

    2040 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.