Jump to:

5537 Posts in 1737 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Extend Page class for a module

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

Page: 1
Go to End
Author Topic: 2055 Views
  • m-phil
    Avatar
    Community Member
    37 Posts

    Extend Page class for a module Link to this post

    Is it possible to extend the Page class within a module?
    I've created a module, but I need to extend the Page class to give all pages access to a function. I know, that's no problem to extend the Page class manually, but I want to offer my module for everyone and don't want to give an instruction what people need to add in Page.php to use it correctly. The function needs to be global.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Extend Page class for a module Link to this post

    Yep you can extend Page fine. In fact most modules do. User Defined Forms extends Page to add a form, ecommerce, blog etc all extend Page to offer new page types. If you want to append functionality to page though (eg you don't want to force users to make a 'MyCoolNewPage' in the CMS - so you want the functionality appended to page) you can add functionality / fields via extensions and a dataobjectdecorator.

  • m-phil
    Avatar
    Community Member
    37 Posts

    Re: Extend Page class for a module Link to this post

    Hey Willr, thx for your post, I know to create a new page type by extending from the Page class.
    Sry, I wasn't very clear by explaining my problem.

    I have a module (also with a new page type extending Page). But my problem is, I added a function to the Page class, which is global for all other pages, because of inheritance. But the access should be global for all other page types within a module.
    So I need to extend the Page class by adding a function, but I can't touch the Page class, because I want to offer my module to everyone and don't want to tell someone to add a piece of code to his/her Page class.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Extend Page class for a module Link to this post

    But the access should be global for all other page types within a module.

    So you will need to use an Extension rather than subclass. See http://doc.silverstripe.org/dataobjectdecorator. You can decorate the built in Page class, or SiteTree if needed.

  • m-phil
    Avatar
    Community Member
    37 Posts

    Re: Extend Page class for a module Link to this post

    Perfect! Thx Willr!

    For everyone here is the solution:

    I added in my module config file Object::add_extension('Page', 'PageFolderExtension');
    Now I can add functions in my new PageFolderExtension class in /code which enhanced the Page class

    class PageFolderExtension extends DataObjectDecorator {
       
       public function ShowPageFolder($pageName = null, $pageCount = null) {
          ...
       }
    }


    All other page types have access to this new function!

    2055 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.