Accept Breadcrumbs::$homeLink = false to omit "Home" link (fixed #24)

This commit is contained in:
Simon Karlen 2022-01-05 09:49:44 +01:00
parent a3b52b4519
commit a0c528b496
No known key found for this signature in database
GPG Key ID: 0630C27D666EBCC3
3 changed files with 23 additions and 5 deletions

View File

@ -4,6 +4,7 @@ Yii Framework 2 bootstrap5 extension Change Log
2.0.3 under development 2.0.3 under development
----------------------- -----------------------
- Enh #24: Accept `Breadcrumbs::$homeLink = false` to omit "Home" link (fetus-hina)
- Enh #27: Changed all data- and aria- attributes to short syntax (simialbi) - Enh #27: Changed all data- and aria- attributes to short syntax (simialbi)
- Enh #26: Add Bootstrap icon asset (Krakozaber) - Enh #26: Add Bootstrap icon asset (Krakozaber)
- Enh #18: Add rangeInput(), colorInput() and switch mode to checkbox() in class ActiveField (WinterSilence) - Enh #18: Add rangeInput(), colorInput() and switch mode to checkbox() in class ActiveField (WinterSilence)

View File

@ -40,7 +40,7 @@ class Breadcrumbs extends Widget
*/ */
public $encodeLabels = true; public $encodeLabels = true;
/** /**
* @var array the first hyperlink in the breadcrumbs (called home link). * @var array|bool the first hyperlink in the breadcrumbs (called home link).
* Please refer to [[links]] on the format of the link. * Please refer to [[links]] on the format of the link.
* If this property is not set, it will default to a link pointing to [[\yii\web\Application::homeUrl]] * If this property is not set, it will default to a link pointing to [[\yii\web\Application::homeUrl]]
* with the label 'Home'. If this property is false, the home link will not be rendered. * with the label 'Home'. If this property is false, the home link will not be rendered.
@ -113,7 +113,7 @@ class Breadcrumbs extends Widget
'label' => 'Home', 'label' => 'Home',
'url' => '/', 'url' => '/',
], $this->itemTemplate); ], $this->itemTemplate);
} else { } elseif ($this->homeLink !== false) {
$links[] = $this->renderItem($this->homeLink, $this->itemTemplate); $links[] = $this->renderItem($this->homeLink, $this->itemTemplate);
} }
@ -165,11 +165,11 @@ class Breadcrumbs extends Widget
* If this property is not set, it will default to a link pointing with the label 'Home'. If this property is false, * If this property is not set, it will default to a link pointing with the label 'Home'. If this property is false,
* the home link will not be rendered. * the home link will not be rendered.
* *
* @param array $value * @param array|bool $value
* *
* @return $this * @return $this
*/ */
public function homeLink(array $value): self public function homeLink($value): self
{ {
$this->homeLink = $value; $this->homeLink = $value;
@ -295,5 +295,4 @@ class Breadcrumbs extends Widget
return strtr($template, ['{link}' => $linkHtml]); return strtr($template, ['{link}' => $linkHtml]);
} }
} }

View File

@ -26,7 +26,25 @@ class BreadcrumbsTest extends TestCase
<li class="breadcrumb-item active" aria-current="page">Data</li> <li class="breadcrumb-item active" aria-current="page">Data</li>
</ol></nav> </ol></nav>
HTML; HTML;
$this->assertEqualsWithoutLE($expected, $out);
}
public function testRenderWithoutHomeLink()
{
Breadcrumbs::$counter = 0;
$out = Breadcrumbs::widget([
'homeLink' => false,
'links' => [
['label' => 'Library', 'url' => '#'],
['label' => 'Data']
]
]);
$expected = <<<HTML
<nav aria-label="breadcrumb"><ol id="w0" class="breadcrumb"><li class="breadcrumb-item"><a href="#">Library</a></li>
<li class="breadcrumb-item active" aria-current="page">Data</li>
</ol></nav>
HTML;
$this->assertEqualsWithoutLE($expected, $out); $this->assertEqualsWithoutLE($expected, $out);
} }