Merge pull request #7343 from creative-commoners/pulls/4.0/add-extra-class

NEW Allow GridFieldEditButton to have configurable HTML classes. Change edit icon.
This commit is contained in:
Damian Mooyman 2017-09-13 15:39:38 +12:00 committed by GitHub
commit d05d22abc2
3 changed files with 83 additions and 2 deletions

View File

@ -19,6 +19,16 @@ use SilverStripe\View\SSViewer;
*/ */
class GridFieldEditButton implements GridField_ColumnProvider 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' * 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. // which can make the form readonly if no edit permissions are available.
$data = new ArrayData(array( $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__); $template = SSViewer::get_templates_by_class($this, '', __CLASS__);
return $data->renderWith($template); 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. * Handle the actions and apply any changes to the GridField.
* *

View File

@ -1,5 +1,5 @@
<a <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' %>" href="$Link" title="<%t SilverStripe\\Forms\\GridField\\GridFieldEditButton_ss.EDIT 'Edit' %>"
> >
<span class="sr-only"><%t SilverStripe\\Forms\\GridField\\GridFieldEditButton_ss.EDIT 'Edit' %></span> <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'); $editLinks = $content->getBySelector('.edit-link');
$this->assertEquals(3, count($editLinks), 'Edit links should show when logged in.'); $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());
}
} }