silverstripe dopublish function for ModelAdmin managed Pages

↘锁芯ラ 提交于 2020-01-06 19:34:06

问题


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


回答1:


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.




回答2:


I found the same need/lack and I built a workaround that seems to work for me, maybe it can be useful.

public function onAfterWrite()
{
if(!$this->isPublished() || $this->getIsModifiedOnStage())
{
    $this->publish('Stage', 'Live');
    Controller::curr()->redirectBack(); 
}
parent::onAfterWrite();
}



回答3:


Create a class that extends ModelAdmin and define an updateEditForm function to add a publish button to the actions in the GridFieldDetailForm component of the GridField.

public function updateEditForm($form) {

    if ( ! singleton($this->owner->modelClass)->hasExtension('Versioned') ) return;

    $gridField = $form->Fields()->fieldByName($this->owner->modelClass);
    $gridField->getConfig()->getComponentByType('GridFieldDetailForm')->setItemEditFormCallback(function ($form) {
        $form->Actions()->push(FormAction::create('doPublish', 'Save & Publish'));
    });
}

Then create a class that extends GridFieldDetailForm_ItemRequest to provide an action handler for your publish button.

public function doPublish($data, $form) {
    $return = $this->owner->doSave($data, $form);
    $this->owner->record->publish('Stage', 'Live');
    return $return;
}

Make sure the extensions are applied and you're done.

Or, you could just grab all the code you need from GitHub.



来源:https://stackoverflow.com/questions/12157964/silverstripe-dopublish-function-for-modeladmin-managed-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!