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.

Customising the CMS /

Extend Page class for a module


Reply


5 Posts   2148 Views

Avatar
m-phil

Community Member, 37 Posts

20 May 2010 at 12:45am

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.

Avatar
Willr

Forum Moderator, 5513 Posts

21 May 2010 at 9:36pm

Edited: 21/05/2010 9:37pm

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.

Avatar
m-phil

Community Member, 37 Posts

25 May 2010 at 3:30am

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.

Avatar
Willr

Forum Moderator, 5513 Posts

25 May 2010 at 9:33am

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.

Avatar
m-phil

Community Member, 37 Posts

28 May 2010 at 10:08pm

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!