2012-03-06 12:57:16 +01:00
|
|
|
<?php
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms\GridField;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\SSViewer;
|
|
|
|
|
2012-03-06 12:57:16 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Provides the entry point to editing a single record presented by the
|
2013-05-20 12:18:07 +02:00
|
|
|
* {@link GridField}.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Doesn't show an edit view on its own or modifies the record, but rather
|
|
|
|
* relies on routing conventions established in {@link getColumnContent()}.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* The default routing applies to the {@link GridFieldDetailForm} component,
|
2013-05-20 12:18:07 +02:00
|
|
|
* which has to be added separately to the {@link GridField} configuration.
|
2012-03-06 12:57:16 +01:00
|
|
|
*/
|
2022-02-01 23:14:33 +01:00
|
|
|
class GridFieldEditButton extends AbstractGridFieldComponent implements GridField_ColumnProvider, GridField_ActionProvider, GridField_ActionMenuLink
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2022-02-01 23:14:33 +01:00
|
|
|
use GridFieldStateAware;
|
2019-06-13 04:51:43 +02:00
|
|
|
|
2017-09-06 00:58:06 +02:00
|
|
|
/**
|
|
|
|
* HTML classes to be added to GridField edit buttons
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $extraClass = [
|
|
|
|
'grid-field__icon-action--hidden-on-hover' => true,
|
|
|
|
'font-icon-edit' => true,
|
2018-05-29 06:10:52 +02:00
|
|
|
'btn--icon-large' => true,
|
|
|
|
'action-menu--handled' => true
|
2017-09-06 00:58:06 +02:00
|
|
|
];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2018-05-29 06:10:52 +02:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getTitle($gridField, $record, $columnName)
|
|
|
|
{
|
|
|
|
return _t(__CLASS__ . '.EDIT', "Edit");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getGroup($gridField, $record, $columnName)
|
|
|
|
{
|
|
|
|
return GridField_ActionMenuItem::DEFAULT_GROUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getExtraData($gridField, $record, $columnName)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
"classNames" => "font-icon-edit action-detail edit-link"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2022-07-17 22:49:08 +02:00
|
|
|
* @param bool $addState DEPRECATED: Should be removed in major release
|
2018-05-29 06:10:52 +02:00
|
|
|
*/
|
2021-07-18 17:54:22 +02:00
|
|
|
public function getUrl($gridField, $record, $columnName, $addState = true)
|
2018-05-29 06:10:52 +02:00
|
|
|
{
|
2019-06-13 04:51:43 +02:00
|
|
|
$link = Controller::join_links(
|
2018-11-18 23:06:47 +01:00
|
|
|
$gridField->Link('item'),
|
|
|
|
$record->ID,
|
2019-06-13 04:51:43 +02:00
|
|
|
'edit'
|
2018-11-18 23:06:47 +01:00
|
|
|
);
|
2019-06-13 04:51:43 +02:00
|
|
|
|
2022-07-17 22:49:08 +02:00
|
|
|
return $gridField->addAllStateToUrl($link, $addState);
|
2018-05-29 06:10:52 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Add a column 'Delete'
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param array $columns
|
|
|
|
*/
|
|
|
|
public function augmentColumns($gridField, &$columns)
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
if (!in_array('Actions', $columns ?? [])) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$columns[] = 'Actions';
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Return any special attributes that will be used for FormField::create_tag()
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param DataObject $record
|
|
|
|
* @param string $columnName
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getColumnAttributes($gridField, $record, $columnName)
|
|
|
|
{
|
2018-05-29 06:10:52 +02:00
|
|
|
return ['class' => 'grid-field__col-compact'];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Add the title
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param string $columnName
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getColumnMetadata($gridField, $columnName)
|
|
|
|
{
|
|
|
|
if ($columnName == 'Actions') {
|
2018-05-29 06:10:52 +02:00
|
|
|
return ['title' => ''];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Which columns are handled by this component
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getColumnsHandled($gridField)
|
|
|
|
{
|
2018-05-29 06:10:52 +02:00
|
|
|
return ['Actions'];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Which GridField actions are this component handling.
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getActions($gridField)
|
|
|
|
{
|
2018-05-29 06:10:52 +02:00
|
|
|
return [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param DataObject $record
|
|
|
|
* @param string $columnName
|
|
|
|
* @return string The HTML for the column
|
|
|
|
*/
|
|
|
|
public function getColumnContent($gridField, $record, $columnName)
|
|
|
|
{
|
|
|
|
// No permission checks, handled through GridFieldDetailForm,
|
|
|
|
// which can make the form readonly if no edit permissions are available.
|
2012-12-17 00:47:23 +01:00
|
|
|
|
2018-05-29 06:10:52 +02:00
|
|
|
$data = new ArrayData([
|
2021-07-18 17:54:22 +02:00
|
|
|
'Link' => $this->getURL($gridField, $record, $columnName, false),
|
2017-09-06 00:58:06 +02:00
|
|
|
'ExtraClass' => $this->getExtraClass()
|
2018-05-29 06:10:52 +02:00
|
|
|
]);
|
2012-03-06 12:57:16 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
$template = SSViewer::get_templates_by_class($this, '', __CLASS__);
|
|
|
|
return $data->renderWith($template);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-09-06 00:58:06 +02:00
|
|
|
/**
|
|
|
|
* Get the extra HTML classes to add for edit buttons
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getExtraClass()
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return implode(' ', array_keys($this->extraClass ?? []));
|
2017-09-06 00:58:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an extra HTML class
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function addExtraClass($class)
|
|
|
|
{
|
|
|
|
$this->extraClass[$class] = true;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an HTML class
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function removeExtraClass($class)
|
|
|
|
{
|
|
|
|
unset($this->extraClass[$class]);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Handle the actions and apply any changes to the GridField.
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param string $actionName
|
|
|
|
* @param mixed $arguments
|
|
|
|
* @param array $data - form data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
|
|
|
|
{
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|