Search code examples
content-management-systemsilverstripemodeladmin

silverstripe dopublish function for ModelAdmin managed Pages


in the silverstripe backend I´m managing certain PageTypes via a ModelAdmin. That works fine so far, the only thing i can´t figure out is how to make a Page being "Published" when saving it.

Thats my code:

class ProjectPage extends Page { 
    public function onAfterWrite() {
        $this->doPublish();
        parent::onAfterWrite();
    }
}

At the moment I can still see the ModelAdmin-created Pages in the Sitetree and I can see they are in Draft Mode. If I use the code above I get this error: Maximum execution time of 30 seconds exceeded in .../framework/model/DataList.php

Many thx, Florian


Solution

  • the reason why you get "Maximum execution time exceeded" is because $this->doPublish(); calls $this->write(); which then calls $this->onAfterWrite();. And there you have your endless loop. So doing this in onAfterWrite() or write() doesn't really work

    you should just display the save & publish button instead of the save button But I guess its easier said than done. Well adding a button is actually just a few lines, but we also need a provide the functions that do what the button says it does.

    This sounds like the perfect call for creating a new Module that allows proper handling of Pages in model admin. I have done this in SS2.4, and I have a pretty good idea of how to do it in SS3, but no time this week, poke me on the silverstripe irc channel on the weekend, maybe I have time on the weekend.