mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW Allow GridFieldEditButton to have configurable HTML classes. Change edit icon.
This commit is contained in:
parent
546b4dedcf
commit
c707fccf69
@ -19,6 +19,16 @@ use SilverStripe\View\SSViewer;
|
||||
*/
|
||||
class GridFieldEditButton implements GridField_ColumnProvider
|
||||
{
|
||||
/**
|
||||
* 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,
|
||||
'btn--icon-large' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Add a column 'Delete'
|
||||
@ -95,13 +105,50 @@ class GridFieldEditButton implements GridField_ColumnProvider
|
||||
// which can make the form readonly if no edit permissions are available.
|
||||
|
||||
$data = new ArrayData(array(
|
||||
'Link' => Controller::join_links($gridField->Link('item'), $record->ID, 'edit')
|
||||
'Link' => Controller::join_links($gridField->Link('item'), $record->ID, 'edit'),
|
||||
'ExtraClass' => $this->getExtraClass()
|
||||
));
|
||||
|
||||
$template = SSViewer::get_templates_by_class($this, '', __CLASS__);
|
||||
return $data->renderWith($template);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the extra HTML classes to add for edit buttons
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getExtraClass()
|
||||
{
|
||||
return implode(' ', array_keys($this->extraClass));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the actions and apply any changes to the GridField.
|
||||
*
|
||||
|
@ -1,5 +1,5 @@
|
||||
<a
|
||||
class="grid-field__icon-action grid-field__icon-action--hidden-on-hover font-icon-edit-write btn--icon-large action action-detail edit-link"
|
||||
class="grid-field__icon-action {$ExtraClass} action action-detail edit-link"
|
||||
href="$Link" title="<%t SilverStripe\\Forms\\GridField\\GridFieldEditButton_ss.EDIT 'Edit' %>"
|
||||
>
|
||||
<span class="sr-only"><%t SilverStripe\\Forms\\GridField\\GridFieldEditButton_ss.EDIT 'Edit' %></span>
|
||||
|
@ -86,4 +86,38 @@ class GridFieldEditButtonTest extends SapphireTest
|
||||
$editLinks = $content->getBySelector('.edit-link');
|
||||
$this->assertEquals(3, count($editLinks), 'Edit links should show when logged in.');
|
||||
}
|
||||
|
||||
public function testDefaultClassesAreSet()
|
||||
{
|
||||
$button = new GridFieldEditButton;
|
||||
|
||||
$expected = [
|
||||
'grid-field__icon-action--hidden-on-hover',
|
||||
'font-icon-edit',
|
||||
'btn--icon-large'
|
||||
];
|
||||
|
||||
$result = $button->getExtraClass();
|
||||
|
||||
foreach ($expected as $className) {
|
||||
$this->assertContains($className, $result);
|
||||
}
|
||||
}
|
||||
|
||||
public function testAddAndRemoveExtraClass()
|
||||
{
|
||||
$button = new GridFieldEditButton;
|
||||
|
||||
$button->addExtraClass('foobar');
|
||||
$this->assertContains('foobar', $button->getExtraClass());
|
||||
|
||||
$button->removeExtraClass('foobar');
|
||||
$this->assertNotContains('foobar', $button->getExtraClass());
|
||||
|
||||
// Check that duplicates are removed
|
||||
$button->addExtraClass('foobar');
|
||||
$button->addExtraClass('foobar');
|
||||
$button->removeExtraClass('foobar');
|
||||
$this->assertNotContains('foobar', $button->getExtraClass());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user