2

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?

Fatal Error
  • 1,024
  • 6
  • 12

1 Answers1

0

Well, $this->owner->get() won't work. $this->owner always returns the actual object, not a DataList. It should work like:

public function canCreate() {

    $className = $this->owner->ClassName;

    return $className::get()->count() == 0;
}

BTW: if you plan to release a module of this please consider another name, I already have published https://github.com/wernerkrauss/silverstripe-onepage/ for making simple onepage-sites utilizing the SiteTree

wmk
  • 4,598
  • 1
  • 20
  • 37
  • Thanks for your response, however the `get` function does work. It is a [method](http://api.silverstripe.org/3.1/class-DataObject.html#_get) on `DataObject`. It returns a DataList, so could you change that in your answer? Besides that I do not plan on releasing it, just use it for my own. And this answer does not really answer my question, because I stated that the canCreate function does work, if you move it to the actual HomePage class – Fatal Error Nov 09 '16 at 13:37
  • `get` is a static method, i doubt you can call it directly from the extension. – wmk Nov 09 '16 at 13:40
  • 1
    Calling a static method as a non-static method [is possible](http://stackoverflow.com/a/10907777/6251886) Give it a try. – Fatal Error Nov 09 '16 at 13:45