From aa6d34a2858b32777b97ae1bf089b20d0f0bcca7 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Fri, 28 Oct 2016 01:11:54 +0600 Subject: [PATCH] Fixes #9796: Initialization of not existing yii\grid\ActionColumn default buttons (#12859) --- framework/CHANGELOG.md | 1 + framework/grid/ActionColumn.php | 55 +++++++++++++++---------------- tests/framework/grid/ActionColumnTest.php | 25 +++++++++++++- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index beeca48..2c599da 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -14,6 +14,7 @@ Yii Framework 2 Change Log - Enh #12807: Added console controller checks for `yii\console\controllers\HelpController` (schmunk42) - Bug #12824: Enabled usage of `yii\mutex\FileMutex` on Windows systems (davidsonalencar) - Enh #11037 yii.js and yii.validation.js should use Regexp.test instead of String.match (arogachev, nkovacs) +- Bug #9796: Initialization of not existing `yii\grid\ActionColumn` default buttons (arogachev) 2.0.10 October 20, 2016 ----------------------- diff --git a/framework/grid/ActionColumn.php b/framework/grid/ActionColumn.php index fcd3326..e4608d9 100644 --- a/framework/grid/ActionColumn.php +++ b/framework/grid/ActionColumn.php @@ -120,7 +120,7 @@ class ActionColumn extends Column */ public $urlCreator; /** - * @var array html options to be applied to the [[initDefaultButtons()|default buttons]]. + * @var array html options to be applied to the [[initDefaultButton()|default button]]. * @since 2.0.4 */ public $buttonOptions = []; @@ -140,36 +140,33 @@ class ActionColumn extends Column */ protected function initDefaultButtons() { - if (!isset($this->buttons['view'])) { - $this->buttons['view'] = function ($url, $model, $key) { - $options = array_merge([ - 'title' => Yii::t('yii', 'View'), - 'aria-label' => Yii::t('yii', 'View'), - 'data-pjax' => '0', - ], $this->buttonOptions); - return Html::a('', $url, $options); - }; - } - if (!isset($this->buttons['update'])) { - $this->buttons['update'] = function ($url, $model, $key) { - $options = array_merge([ - 'title' => Yii::t('yii', 'Update'), - 'aria-label' => Yii::t('yii', 'Update'), - 'data-pjax' => '0', - ], $this->buttonOptions); - return Html::a('', $url, $options); - }; - } - if (!isset($this->buttons['delete'])) { - $this->buttons['delete'] = function ($url, $model, $key) { + $this->initDefaultButton('view', 'eye-open'); + $this->initDefaultButton('update', 'pencil'); + $this->initDefaultButton('delete', 'trash', [ + 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), + 'data-method' => 'post', + ]); + } + + /** + * Initializes the default button rendering callback for single button + * @param string $name Button name as it's written in template + * @param string $iconName The part of Bootstrap glyphicon class that makes it unique + * @param array $additionalOptions Array of additional options + * @since 2.0.11 + */ + protected function initDefaultButton($name, $iconName, $additionalOptions = []) + { + if (!isset($this->buttons[$name]) && strpos($this->template, $name) !== false) { + $this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) { + $title = Yii::t('yii', ucfirst($name)); $options = array_merge([ - 'title' => Yii::t('yii', 'Delete'), - 'aria-label' => Yii::t('yii', 'Delete'), - 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), - 'data-method' => 'post', + 'title' => $title, + 'aria-label' => $title, 'data-pjax' => '0', - ], $this->buttonOptions); - return Html::a('', $url, $options); + ], $additionalOptions, $this->buttonOptions); + $icon = Html::tag('span', '', ['class' => "glyphicon glyphicon-$iconName"]); + return Html::a($icon, $url, $options); }; } } diff --git a/tests/framework/grid/ActionColumnTest.php b/tests/framework/grid/ActionColumnTest.php index a424306..922373d 100644 --- a/tests/framework/grid/ActionColumnTest.php +++ b/tests/framework/grid/ActionColumnTest.php @@ -14,12 +14,35 @@ use yii\grid\ActionColumn; */ class ActionColumnTest extends \yiiunit\TestCase { + public function testInit() + { + $column = new ActionColumn(); + $this->assertEquals(['view', 'update', 'delete'], array_keys($column->buttons)); + + $column = new ActionColumn(['template' => '{show} {edit} {delete}']); + $this->assertEquals(['delete'], array_keys($column->buttons)); + + $column = new ActionColumn(['template' => '{show} {edit} {remove}']); + $this->assertEmpty($column->buttons); + } + public function testRenderDataCell() { $column = new ActionColumn(); $column->urlCreator = function($model, $key, $index) { return 'http://test.com'; }; + $columnContents = $column->renderDataCell(['id' => 1], 1, 0); + $viewButton = ''; + $updateButton = ''; + $deleteButton = ''; + $expectedHtml = "$viewButton $updateButton $deleteButton"; + $this->assertEquals($expectedHtml, $columnContents); + + $column = new ActionColumn(); + $column->urlCreator = function($model, $key, $index) { + return 'http://test.com'; + }; $column->template = '{update}'; $column->buttons = [ 'update' => function($url, $model, $key) { @@ -60,4 +83,4 @@ class ActionColumnTest extends \yiiunit\TestCase $this->assertNotContains('update_button', $columnContents); } -} \ No newline at end of file +}