<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
micschk / silverstripe-groupable-gridfield example snippets
use Restruct\Silverstripe\GroupableGridfield\GridFieldGroupable;
use Restruct\Silverstripe\GroupableGridfield\GridFieldAddNewGroupButton;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
$config = GridFieldConfig::create()
->addComponent(new GridFieldOrderableRows())
->addComponent(new GridFieldAddNewGroupButton('buttons-before-right'))
->addComponent(new GridFieldGroupable(
'Phase', // Field on items holding group key (or many_many extra field)
$this->fieldLabel('Phase'), // Label for group field
'none', // Name for "unassigned" group
null, // Static groups array (null = use MultiValueField)
'Phases' // MultiValueField name on source record
));
use Restruct\Silverstripe\GroupableGridfield\GridFieldGroupable;
use Restruct\Silverstripe\GroupableGridfield\GridFieldAddNewDataObjectGroupButton;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
$groupable = GridFieldGroupable::create(
'SectionID', // FK field on items (e.g., has_one relation)
'Section', // Label for group field
'No Section' // Name for "unassigned" group
)
->setGroupsFromRelation('Sections') // Relation method returning groups
->setGroupTitleField('Name') // Field on group DO for display name
->setGroupMetadataFields(['Code', 'Description']) // Extra fields for display
->setGroupSortField('Sort') // Enable group reordering
->setEditableGroupTitle(true) // Enable click-to-edit titles
->setGroupDeleteBehavior('unassign'); // What happens when group is deleted
// The setters above return the component, not the config: add it once it is configured
$config = GridFieldConfig::create()
->addComponent(new GridFieldOrderableRows('SortOrder'))
->addComponent($groupable)
->addComponent(new GridFieldAddNewDataObjectGroupButton());
// Options: 'unassign' (default), 'prevent', 'callback'
$groupable->setGroupDeleteBehavior('unassign');
// Custom handler (when mode is 'callback'), passed as the second argument
$groupable->setGroupDeleteBehavior('callback', function ($gridField, $group, $itemsInGroup) {
// Custom logic
return ['success' => true, 'message' => 'Deleted'];
});
$groupable->addGroupAction(
'sync_external', // Action name
'Sync to External System', // Button title
'font-icon-sync', // Icon class
function ($gridField, $sourceRecord, $group, $actionData) {
// $sourceRecord = the record owning the grid, $group = the group DataObject,
// $actionData = POST vars of the action request
return [
'success' => true,
'message' => 'Synced successfully',
// 'redirect' => '/some/url', // Optional redirect
];
}
);