gpt4 book ai didi

php - 将自定义操作的按钮添加到 ModelAdmin

转载 作者:可可西里 更新时间:2023-11-01 13:21:16 25 4
gpt4 key购买 nike

在 Silverstripe 3 中使用 ModelAdmin 管理 Dataobject 时,我想知道如何将自定义操作的按钮添加到 ListView 和编辑页面.

通过在 ModelAdminGridField 类上设置 ItemRequest,我已经成功地为编辑页面添加了一个操作按钮使用以下代码:

class MyModelAdmin extends ModelAdmin
{
//...

public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);
$form
->Fields()
->fieldByName($this->sanitiseClassName($this->modelClass))
->getConfig()
->getComponentByType('GridFieldDetailForm')
->setItemRequestClass('MyModelGridFieldDetailForm_ItemRequest');

return $form;
}

}

MyModelGridFieldDetailForm_ItemRequest.php

class MyModelGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
{
function ItemEditForm()
{
$form = parent::ItemEditForm();
$formActions = $form->Actions();

$button = FormAction::create('myAction');
$button->setTitle('My Custom Action');
$button->addExtraClass('ss-ui-action-constructive');
$formActions->push($button);

$form->setActions($formActions);
return $form;
}


function myAction($data, $form)
{

//do things

$form->sessionMessage('My Action has been successful', 'good');

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

}

那么问题是:

我可以在 ModelAdmin GridField ListView 中为相同的操作设置一个按钮吗?使其出现在编辑和删除按钮旁边:

ModelAdmin GridField actions

最佳答案

您可以通过创建自己的 GridFieldComponent 实现 GridField_ColumnProviderGridField_ActionProvider 来做到这一点。

在您的情况下,您几乎可以复制 GridFieldDeleteAction 类并使用您自己的 GridField_FormAction 编辑 getColumnContent() 并编辑 handleAction () 施展你的魔法。

关于php - 将自定义操作的按钮添加到 ModelAdmin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18349377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com