Jump to:

3432 Posts in 1025 Topics by 865 members

Template Questions

SilverStripe Forums » Template Questions » Replace a core class with my own.

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

Page: 1
Go to End
Author Topic: 2240 Views
  • micahsheets
    Avatar
    Community Member
    164 Posts

    Replace a core class with my own. Link to this post

    I would like to replace a core class with my own without actually editing anything in the saphire folder. Is there a way to make they system load my version of a class instead of the core version?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Replace a core class with my own. Link to this post

    Object::useCustomClass('OldClass','NewClass');

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: Replace a core class with my own. Link to this post

    Thank you for your reply.

    Where would I put that line of code? Does it go in the Page.php file?

  • Willr
    Avatar
    Forum Moderator
    5483 Posts

    Re: Replace a core class with my own. Link to this post

    no usually you would put that in your mysite/_config.php file

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: Replace a core class with my own. Link to this post

    I have attempted to replace the Image_Selector class with one of my own so that instead of having options to upload an image from users computer is removed. (I only want CMS users to be able to select from a preselected set of images from a specific folder.) After putting my version of the class in my page.php file I added

    Object::useCustomClass('Image_Selector','Custom_Image_Selector');

    to my _config.php file. However SS is still using the original. I read somewhere else on the forums that only some core classes can be replaced with custom ones using the Object::useCustomClass method.

    I really just want to override a single function in the Image_Selector class. If I create a new class in page.php that extends Image_Selector, I get an error telling me that Image_Selector class does not exist. I am not very experienced with OOP so I am not sure how to properly overload a function this way as I am guessing that page.php does not inherit the Image_Selector class. If this is the case, then how am I able to overload a function?

  • chrisdarl
    Avatar
    Community Member
    33 Posts

    Re: Replace a core class with my own. Link to this post

    Think you need to create a new file, with the name of the custom class. I.e.

    MyImage extends Image would have a filename of MyImage.php

  • BangMan
    Avatar
    Community Member
    7 Posts

    Re: Replace a core class with my own. Link to this post

    A file MyImage.php must be In what file?

    2240 Views
Page: 1
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.