Commit 2782adcc by Qiang Xue

Fixes #4643: Extra options specified in `yii\widgets\Breadcrumbs::links` will be…

Fixes #4643: Extra options specified in `yii\widgets\Breadcrumbs::links` will be treated as HTML attributes for the generated hyperlinks
parent 9eedfd05
......@@ -32,7 +32,8 @@ Yii Framework 2 Change Log
- Enh #4181: Added `yii\bootstrap\Modal::$headerOptions` and `yii\bootstrap\Modal::$footerOptions` (tuxoff, samdark)
- Enh #4263: Added migration and SQL schema files for `yii\log\DbTarget` (samdark)
- Enh #4457: Added support for using noscript for css files registered through asset bundles and Html helper (samdark)
- Enh #4492: Support PostgreSQL-specific syntax for `QueryBuilder::alterColumn()` (qiangxue)
- Enh #4492: Support PostgreSQL-specific syntax for `QueryBuilder::alterColumn()` (qiangxue)
- Enh #4643: Extra options specified in `yii\widgets\Breadcrumbs::links` will be treated as HTML attributes for the generated hyperlinks (qiangxue)
- Enh #4739: Better display of exceptions when the response format is set as "raw" format (qiangxue)
- Enh #5223: Query builder now supports selecting sub-queries as columns (qiangxue)
- Enh #5367: Added `yii\grid\DataColumn::encodeLabel` (SDKiller)
......
......@@ -78,16 +78,29 @@ class Breadcrumbs extends Widget
* the widget will not render anything. Each array element represents a single link in the breadcrumbs
* with the following structure:
*
* ~~~
* ```php
* [
* 'label' => 'label of the link', // required
* 'url' => 'url of the link', // optional, will be processed by Url::to()
* 'template' => 'own template of the item', // optional, if not set $this->itemTemplate will be used
* ]
* ~~~
* ```
*
* If a link is active, you only need to specify its "label", and instead of writing `['label' => $label]`,
* you should simply use `$label`.
* you may simply use `$label`.
*
* Since version 2.0.1, any additional array elements for each link will be treated as the HTML attributes
* for the hyperlink tag. For example, the following link specification will generate a hyperlink
* with CSS class `external`:
*
* ```php
* [
* 'label' => 'demo',
* 'url' => 'http://example.com',
* 'class' => 'external',
* ]
* ```
*
*/
public $links = [];
/**
......@@ -142,11 +155,16 @@ class Breadcrumbs extends Widget
} else {
throw new InvalidConfigException('The "label" element is required for each link.');
}
$issetTemplate = isset($link['template']);
if (isset($link['template'])) {
$template = $link['template'];
}
if (isset($link['url'])) {
return strtr($issetTemplate ? $link['template'] : $template, ['{link}' => Html::a($label, $link['url'])]);
$options = $link;
unset($options['template'], $options['label'], $options['url']);
$link = Html::a($label, $link['url'], $options);
} else {
return strtr($issetTemplate ? $link['template'] : $template, ['{link}' => $label]);
$link = $label;
}
return strtr($template, ['{link}' => $link]);
}
}
......@@ -133,6 +133,18 @@ class BreadcrumbsTest extends \yiiunit\TestCase
$unencodedValue = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
$this->assertEquals("<td><a href=\"http://localhost/yii2\">My-<br>Test-Label</a></td>\n", $unencodedValue);
}
public function testExtraOptions()
{
$link = [
'label' => 'demo',
'url' => 'http://example.com',
'class' => 'external',
];
$method = $this->reflectMethod();
$result = $method->invoke($this->breadcrumbs, $link, $this->breadcrumbs->itemTemplate);
$this->assertEquals('<li><a class="external" href="http://example.com">demo</a></li>' . "\n", $result);
}
/**
* Helper methods
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment