level51/silverstripe-data-object-actions

为DataObjects自定义GridField详情表单操作

安装次数: 1,343

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:silverstripe-vendormodule

1.0.0 2024-05-06 00:00 UTC

This package is auto-updated.

Last update: 2024-09-06 15:40:21 UTC


README

为Silverstripe 5提供的模块,允许向GridFieldDetailForm添加自定义DataObjects操作。查看0.X版本以支持Silverstripe 4。

安装

composer require level51/silverstripe-data-object-actions

使用方法

namespace My\Awesome\Project;

use Level51\DataObjectActions\DataObjectActionProvider;
use Level51\DataObjectActions\DataObjectAction;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataObject;

// Implement DataObjectActionProvider interface on your DataObject
class MyDataObject extends DataObject implements DataObjectActionProvider {

  // Return a field list containing all custom actions, each one of type DataObjectAction or DataObjectLink
  public function getCustomActions() 
  {
    return FieldList::create(
      [
        DataObjectAction::create('myCustomAction', 'My Custom Action')
          ->addExtraClass('btn-outline-primary font-icon-rocket')
          ->setUseButtonTag(true),
        DataObjectLink::create('externalLink', 'External Link', 'https://lvl51.de')
        	->addExtraClass('btn-outline-dark font-icon-external-link')
          ->setNewWindow(true)
      ]
    );
  }
	
  // Implement the handler method(s)
  public function myCustomAction($data, $form) 
  {
    // Do stuff, e.g. set a property
    // Do NOT call $this->write(), this will be done automatically
		
    // throw a new \SilverStripe\ORM\ValidationResult in case something failed
		
    // Optionally return a success message
    return 'Success message';
  }

}

与DataExtensions一起使用

您可以使用updateCustomActions扩展钩子来更新实现DataObjectActionProvider接口的父类的自定义操作。

public function updateCustomActions(FieldList $fields)
{
    $fields->push(
        DataObjectAction::create(...)
    );
}

要向您无法控制的拥有者类(例如核心Member类)添加自定义操作,您必须使用Level51\DataObjectActions\DataExtension类作为您扩展的基础类。使用这种方法,您可以使用如上所示的getCustomActions方法。

要求

  • SilverStripe ^5.0
  • PHP >= 8.0

维护者