11/05/2013 2:46pm

PHP | Problem - Grid Field Custom Action Buttons .. Working Example.


class Artist extends DataObject{


public static $db = array(
'IsPublished' => 'Boolean',
);


public function getCMSActions() {
$fields = parent::getCMSActions();

if($this->IsPublished){
$unPublishButton = FormAction::create('unpublish');
$unPublishButton->setTitle('Hide');
$unPublishButton->setDescription("UnPublish this artists");
$unPublishButton->addExtraClass('ss-ui-action-destructive');
$unPublishButton->setAttribute('data-icon', 'accept');
$fields->push($unPublishButton);
} else {
$publishButton = FormAction::create('publish');
$publishButton->setTitle('Publish');
$publishButton->setDescription("Publish this artists");
$publishButton->addExtraClass('ss-ui-action-constructive');
$publishButton->setAttribute('data-icon', 'accept');
$fields->push($publishButton);
}
return $fields;
}
}




<?php

class DataAdmin extends ModelAdmin {

static $managed_models = array(
'Artist'
);

static $url_segment = 'dataadmin';
static $menu_title = 'Site Admin';

function getEditForm($id = null, $fields = null) {
$form = parent::getEditForm($id , $fields);
$listfield = $form->Fields()->fieldByName($this->modelClass);

if($gridField = $listfield->getConfig()->getComponentByType('GridFieldDetailForm')) {
$gridField->setItemRequestClass('ArtistPublishFieldDetailForm_ItemRequest');
}

return $form;
}

}

class ArtistPublishFieldDetailForm extends GridFieldDetailForm {

}

class ArtistPublishFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest {

function ItemEditForm() {
$form = parent::ItemEditForm();
$formActions = $form->Actions();
if($actions = $this->record->getCMSActions()) {
foreach($actions as $action) {
$formActions->push($action);
}
}
$form->setActions($formActions);
return $form;
}

function publish($data, $form) {
$controller = Controller::curr();

$this->record->IsPublished = 1 ;
$this->record->write();

$message = sprintf(
_t('Artist.Published', 'Published %s %s'),
$this->record->singular_name(),
'<a href="' . $this->Link('edit') . '">"' . htmlspecialchars($this->record->Title, ENT_QUOTES) . '"</a>'
);

$form->sessionMessage($message, 'good');

if($this->gridField->getList()->byId($this->record->ID)) {
return $this->edit(Controller::curr()->getRequest());
} else {
$noActionURL = $controller->removeAction($data['url']);
$controller->getRequest()->addHeader('X-Pjax', 'Content');
return $controller->redirect($noActionURL, 302);
}
}

function unpublish($data, $form) {
$controller = Controller::curr();

$this->record->IsPublished = 0 ;
$this->record->write();

$message = sprintf(
_t('Artist.unpublish', 'Unpublished %s %s'),
$this->record->singular_name(),
'<a href="' . $this->Link('edit') . '">"' . htmlspecialchars($this->record->Title, ENT_QUOTES) . '"</a>'
);

$form->sessionMessage($message, 'bad');

if($this->gridField->getList()->byId($this->record->ID)) {
return $this->edit(Controller::curr()->getRequest());
} else {
$noActionURL = $controller->removeAction($data['url']);
$controller->getRequest()->addHeader('X-Pjax', 'Content');
return $controller->redirect($noActionURL, 302);
}
}

}



1 Comments 1 Solutions

15/07/2014 11:49pm

PHP | Solution - Raymondgome

I love Christmas Daytime, because I ニューバランス 574 新作 <a href=http://webshoppepro.com/>ニクソン リュック</a> ニューバランス 574 新作,ニューバランス スニーカー 新作 <a href=http://webshoppepro.com/>ニクソン 時計 メンズ</a> ニューバランス 574 新作,ニューバランス 574 新作 <a href=http://webshoppepro.com/>ニクソン 腕時計 レディース</a> ニューバランス 996 新作 - http://webshoppepro.com/ can get multitudinous gifts that I have desired for a sustained time. And this Christmas Period get me a immense rock with the benefaction of coach bag. I never mirage of getting a coach ナイキ スニーカー <a href=http://www.sane23.info/>ナイキ スニーカー</a> ナイキ スニーカー,air max 90 <a href=http://www.sane23.info/>air max 90</a> air max 90,air max 90 <a href=http://www.sane23.info/>エアフォース 1</a> ナイキ スニーカー - http://www.sane23.info/ portmanteau which is now greatly popular. But second my mirage comes valid and I actually own a instructor purse of my own, that remarkably makes me uncommonly happy.There a story of the teacher pocket and me. And it has to オロビアンコ 財布 2014新作 <a href=http://www.stykfigures.com/>オロビアンコ バッグ 新作2014</a> オロビアンコ バッグ 新作2014,オロビアンコ バッグ 新作2014 <a href=http://www.stykfigures.com/>オロビアンコ 財布 2014新作</a> オロビアンコ バッグ 新作2014,オロビアンコ 財布 2014新作 <a href=http://www.stykfigures.com/>オロビアンコ アウトレット</a> オロビアンコ 財布 2014新作 - http://www.stykfigures.com/ token bet on a support to a lovely day pattern week. That was a fine day, so I called my friends to withdraw shopping with me in peace to allow some Christmas gifts to my families and friends. At the rather beginning, I indisputable to steal thermal underwear to my parents and UGG boots to my friends. Though, things didn fit バンズ スリッポン <a href=http://www.sbatresddsinc.com/>コーチ バッグ</a> vans スニーカー,バンズ スニーカー <a href=http://www.sbatresddsinc.com/>コーチ 財布</a> vans スニーカー,バンズ スニーカー <a href=http://www.sbatresddsinc.com/>コーチ アウトレット</a> バンズ スリッポン - http://www.sbatresddsinc.com/ according to the plan, over the extent of I demolish in be thrilled by with the carriage overnight bag at in the beginning sight of it when we were shopping in a mall.I didn recognize much more carriage lookout up front, so I done for hours asking sellers questions about アルマーニ 財布 <a href=http://www.maparanormal.com/>アルマーニ 時計 メンズ</a> アルマーニ 財布,アルマーニ 時計 メンズ <a href=http://www.maparanormal.com/>アルマーニ 財布</a> アルマーニ 財布,アルマーニ 時計 メンズ <a href=http://www.maparanormal.com/>アルマーニ ポロシャツ</a> アルマーニ 時計 メンズ- http://www.maparanormal.com/ the intelligence of instructor bag which is a ensnare variety of American and the train gorgon is the favorite of numerous super stars.


Post Comment