For pages that must always be on the website (only 1, not more, like a homepage), I wanted to make an extension. The page should be created when I run dev/build and cannot be created, deleted, unpublished or archived in the CMS. It can be edited however.
Everthing seems to work just fine except the canCreate
function. It seems as this function doesn't do much for the CMS when it's on a(n) (Data)Extension. When I'm on .../admin/pages/add I can still create the Page as many times as I want.
If I move the canCreate
to the HomePage class, it does work. But I want it in my extension :-)
class OnePage extends DataExtension
{
public function requireDefaultRecords()
{
if($this->canCreate()) {
$page = $this->owner;
$page->Title = $this->owner->className;
$page->write();
$page->publish('Stage', 'Live');
$page->flushCache();
}
}
public function canCreate($member = null)
{
return $this->owner->get()->count() == 0;
}
public function canDeleteFromLive($member = null)
{
return false;
}
public function canArchive($member = null)
{
return false;
}
}
class HomePage extends Page
{
private static $extensions = [
'OnePage'
];
}
Am I doing this the right way or am I missing something?
EDIT
The canCreate
method works just fine, so I can solve it like this;
class HomePage extends Page
{
public function canCreate($member = null)
{
return $this->get()->count() == 0;
}
}
This has the desired effect and works like a charm. However, the question is about putting the canCreate
method in the Extension. That should work aswell, right?