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.
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
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.
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.
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.
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!