Skip to content

Instantly share code, notes, and snippets.

@wilr
Last active December 7, 2020 22:46
Show Gist options
  • Save wilr/32d1d18d7248766575dc67f720339cf1 to your computer and use it in GitHub Desktop.
Save wilr/32d1d18d7248766575dc67f720339cf1 to your computer and use it in GitHub Desktop.
GridField action button (for the whole list)
<?php
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Extensible;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
use SilverStripe\Forms\GridField\GridField_ActionProvider;
use SilverStripe\Forms\GridField\GridField_URLHandler;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridField_FormAction;
/**
* Adds an "Print Licences" button to the bottom or top of a GridField.
*/
class GridFieldPrintLicencesButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler
{
use Extensible;
/**
* Fragment to write the button to.
*
* @var string
*/
protected $targetFragment;
/**
* @param string $targetFragment The HTML fragment to write the button into
*/
public function __construct($targetFragment = "after")
{
$this->targetFragment = $targetFragment;
}
/**
* Place the print button in a <p> tag below the field
*
* @param GridField
*
* @return array
*/
public function getHTMLFragments($gridField)
{
$button = new GridField_FormAction(
$gridField,
'printlicences',
'Print Licences',
'printlicences',
null
);
$button->setForm($gridField->getForm());
$button->addExtraClass('font-icon-print grid-print-button btn btn-secondary');
return array(
$this->targetFragment => $button->Field(),
);
}
/**
* Print is an action button.
*
* @param GridField
*
* @return array
*/
public function getActions($gridField)
{
return array('printlicences');
}
/**
* Handle the print action.
*
* @param GridField $gridField
* @param string $actionName
* @param array $arguments
* @param array $data
* @return DBHTMLText
*/
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
if ($actionName == 'printlicences') {
return $this->handlePrintLicences($gridField);
}
}
/**
* Print is accessible via the url
*
* @param GridField
* @return array
*/
public function getURLHandlers($gridField)
{
return array(
'printlicences' => 'handlePrintLicences',
);
}
/**
* Handle the print, for both the action button and the URL
*
* @param GridField $gridField
* @param HTTPRequest $request
* @return DBHTMLText
*/
public function handlePrintLicences($gridField, $request = null)
{
set_time_limit(60);
Requirements::clear();
$data = $this->generatePrintData($gridField);
$this->extend('updatePrintData', $data);
if ($data) {
return $data->renderWith("LicencePrintController");
}
return null;
}
/**
* Return the title of the printed page
*
* @param GridField
*
* @return array
*/
public function getTitle(GridField $gridField)
{
$form = $gridField->getForm();
$currentController = $gridField->getForm()->getController();
$title = '';
if (method_exists($currentController, 'Title')) {
$title = $currentController->Title();
} else {
if ($currentController->Title) {
$title = $currentController->Title;
} elseif ($form->getName()) {
$title = $form->getName();
}
}
if ($fieldTitle = $gridField->Title()) {
if ($title) {
$title .= " - ";
}
$title .= $fieldTitle;
}
return $title;
}
/**
* Export labels
*
* @param GridField $gridField
* @return ArrayData
*/
public function generatePrintData(GridField $gridField)
{
$items = $gridField->getManipulatedList();
if ($items->dataClass() == 'LicenceTransaction') {
$licenceIds = $items->column('LicenceID');
if ($licenceIds) {
$items = Licence::get()->filter('ID', $licenceIds)
->limit(null)
->leftJoin('LicenceType_Child', 'TypeID = LicenceType_Child.ID')
->leftJoin('Member', 'RiderID = Member.ID')
->sort('PrintSortOrder ASC, Member.Surname ASC');
} else {
$items = ArrayList::create();
}
} else {
$items = $items
->limit(null)
->leftJoin('LicenceType_Child', 'TypeID = LicenceType_Child.ID')
->leftJoin('Member', 'RiderID = Member.ID')
->sort('PrintSortOrder ASC, Member.Surname ASC');
}
$result = ArrayData::create([
'Licences' => $items
]);
return $result;
}
}

$field->getConfig()->addComponent(new GridFieldPrintLicencesButton('buttons-before-left'));

Description

Adds a button to GridField with a custom 'Action'. In this example, we have a custom Print Licences action which takes the user to a custom URL with a formatted print view.

Usage

$grid = GridField::create(...); $grid->getConfig()->addComponent(new GridFieldPrintLicencesButton('buttons-before-left'));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment