Browse Source

Fixes #9796: Initialization of not existing yii\grid\ActionColumn default buttons (#12859)

tags/2.0.11
Alexey Rogachev 8 years ago committed by Alexander Makarov
parent
commit
aa6d34a285
  1. 1
      framework/CHANGELOG.md
  2. 55
      framework/grid/ActionColumn.php
  3. 25
      tests/framework/grid/ActionColumnTest.php

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

55
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('<span class="glyphicon glyphicon-eye-open"></span>', $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('<span class="glyphicon glyphicon-pencil"></span>', $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('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
], $additionalOptions, $this->buttonOptions);
$icon = Html::tag('span', '', ['class' => "glyphicon glyphicon-$iconName"]);
return Html::a($icon, $url, $options);
};
}
}

25
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 = '<a href="http://test.com" title="View" aria-label="View" data-pjax="0"><span class="glyphicon glyphicon-eye-open"></span></a>';
$updateButton = '<a href="http://test.com" title="Update" aria-label="Update" data-pjax="0"><span class="glyphicon glyphicon-pencil"></span></a>';
$deleteButton = '<a href="http://test.com" title="Delete" aria-label="Delete" data-pjax="0" data-confirm="Are you sure you want to delete this item?" data-method="post"><span class="glyphicon glyphicon-trash"></span></a>';
$expectedHtml = "<td>$viewButton $updateButton $deleteButton</td>";
$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);
}
}
}

Loading…
Cancel
Save