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);
}
}

}



Post Comment