NEW Allow GridFieldEditButton to have configurable HTML classes. Change edit icon.

This commit is contained in:
Sacha Judd 2017-09-06 10:58:06 +12:00 committed by Damian Mooyman
parent 546b4dedcf
commit c707fccf69
3 changed files with 83 additions and 2 deletions

View File

@ -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.
*

View File

@ -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>

View File

@ -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());
}
}