5095 Posts in 1518 Topics by 1114 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1516 Views |
-
Extend Page class for a module

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. -
Re: Extend Page class for a module

21 May 2010 at 9:36pm Last edited: 21 May 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.
-
Re: Extend Page class for a module

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. -
Re: Extend Page class for a module

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.
-
Re: Extend Page class for a module

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 classclass PageFolderExtension extends DataObjectDecorator {
public function ShowPageFolder($pageName = null, $pageCount = null) {
...
}
}
All other page types have access to this new function!
| 1516 Views | ||
|
Page:
1
|
Go to Top |


