Silverstripe Version: 4.3
Question: How can i modify a ModelAdmin GridField Value using a Custom Action?
Hello you guys!
My question might sound a little confusing but i’ll explain as good and understandable as possible.
So. I made a ModelAdmin Class that works just fine and displays all values as i want.
And i made 2 Custom Action Providers. So, there is a numeric value in the GridField which either should be 0 or 1. And what i wanna do: If you click the button, the Value of this SPECIFIC GridField Row gets modified. How can i do that? How can i select and miodify a value from the row the action runs in? I show you the code, but its basically just standard. Everything works fine. I just need those Buttons “verify” and “decline” to alter the rows specific value…
I could NOT find a way to do it in the SS documentation. Or did i just overread it? Then please let me know! I would be so glad if you could help me out!
<?php
use SilverStripe\View\Requirements;
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
use SilverStripe\Forms\GridField\GridField_ActionProvider;
use SilverStripe\Forms\GridField\GridField_FormAction;
use SilverStripe\Forms\GridField\GridField;
use App\Web\Newsletter\CommentAdmin;
use SilverStripe\Control\Controller;
class CommentAdminButtons implements GridField_ColumnProvider, GridField_ActionProvider
{
public function augmentColumns($gridField, &$columns)
{
if (!in_array('Actions', $columns)) {
$columns[] = 'Actions';
}
}
public function getColumnAttributes($gridField, $record, $columnName)
{
return ['class' => 'grid-field__col-compact'];
}
public function getColumnMetadata($gridField, $columnName)
{
if ($columnName === 'Actions') {
return ['title' => ''];
}
}
public function getColumnsHandled($gridField)
{
return ['Actions'];
}
public function getColumnContent($gridField, $record, $columnName)
{
Requirements::css('resources/themes/efficon/css/admin.css');
if (!$record->canEdit()) {
return;
}
$field = GridField_FormAction::create(
$gridField,
"verify-btn","",
"verify",
array('RecordID' => $record->ID)
);
$field->addExtraClass("verify-btn");
$field->setDescription('Kommentar freigeben');
$declineField = GridField_FormAction::create(
$gridField,
"decline-btn",
"",
"decline",
array('RecordID' => $record->ID)
);
$declineField->addExtraClass("decline-btn");
$declineField->setDescription('Kommentar nicht veröffentlichen');
return $field->Field(). $declineField->Field();
}
public function getActions($gridField)
{
return array('verify', 'decline');
}
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
if(strtolower($actionName) == "verify") {
// WANT TO MODIFY GRIDFIELD VALUE
return $ruck;
}
if(strtolower($actionName) == "decline") {
// WANT TO MODIFY GRIDFIELD VALUE
return;
}
// output a success message to the user
Controller::curr()->getResponse()->setStatusCode(
200,
'Do Custom Action Done.'
);
}
}
?>