Merge pull request #40 from WinterSilence/patch-2
Breadcrumbs refactoring
This commit is contained in:
commit
65a4d0fd66
@ -9,67 +9,62 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace yii\bootstrap5;
|
namespace yii\bootstrap5;
|
||||||
|
|
||||||
use RuntimeException;
|
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\helpers\ArrayHelper;
|
use yii\helpers\ArrayHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Breadcrumbs represents a Bootstrap 5 version of [[\yii\widgets\Breadcrumbs]]. It displays
|
* This widget represents a Bootstrap 5 component "Breadcrumb". It displays a list of links indicating the
|
||||||
* a list of links indicating the position of the current page in the whole site hierarchy.
|
* position of the current page in the whole site hierarchy.
|
||||||
*
|
|
||||||
* To use Breadcrumbs, you need to configure its [[links]] property, which specifies the links to be displayed. For example,
|
|
||||||
*
|
*
|
||||||
* ```php
|
* ```php
|
||||||
* echo Breadcrumbs::widget([
|
* echo Breadcrumbs::widget([
|
||||||
* 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
|
* 'links' => [
|
||||||
* 'options' => [],
|
* [
|
||||||
|
* 'label' => 'the item label', // required
|
||||||
|
* 'url' => 'the item URL', // optional, will be processed by `Url::to()`
|
||||||
|
* 'template' => 'own template of the item', // optional
|
||||||
|
* ],
|
||||||
|
* ['label' => 'the label of the active item']
|
||||||
|
* ],
|
||||||
|
* 'options' => [...],
|
||||||
* ]);
|
* ]);
|
||||||
* ```
|
* ```
|
||||||
|
* or
|
||||||
|
* ```php
|
||||||
|
* echo Breadcrumbs::widget([
|
||||||
|
* 'links' => [
|
||||||
|
* 'the item URL' => 'the item label',
|
||||||
|
* 0 => 'the label of the active item',
|
||||||
|
* ],
|
||||||
|
* 'options' => [...],
|
||||||
|
* ]);
|
||||||
|
* ```
|
||||||
|
*
|
||||||
* @see https://getbootstrap.com/docs/5.1/components/breadcrumb/
|
* @see https://getbootstrap.com/docs/5.1/components/breadcrumb/
|
||||||
* @author Alexandr Kozhevnikov <onmotion1@gmail.com>
|
* @author Alexandr Kozhevnikov <onmotion1@gmail.com>
|
||||||
* @author Simon Karlen <simi.albi@outlook.com>
|
* @author Simon Karlen <simi.albi@outlook.com>
|
||||||
*/
|
*/
|
||||||
class Breadcrumbs extends Widget
|
class Breadcrumbs extends \yii\widgets\Breadcrumbs
|
||||||
{
|
{
|
||||||
|
use BootstrapWidgetTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string the name of the breadcrumb container tag.
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
public $tag = 'ol';
|
public $tag = 'ol';
|
||||||
/**
|
/**
|
||||||
* @var bool whether to HTML-encode the link labels.
|
* @var array|false the first hyperlink in the breadcrumbs (called home link).
|
||||||
*/
|
|
||||||
public $encodeLabels = true;
|
|
||||||
/**
|
|
||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public $homeLink = [];
|
public $homeLink = [];
|
||||||
/**
|
/**
|
||||||
* @var array list of links to appear in the breadcrumbs. If this property is empty,
|
* {@inheritDoc}
|
||||||
* 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
|
|
||||||
* ]
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public $links = [];
|
|
||||||
/**
|
|
||||||
* @var string the template used to render each inactive item in the breadcrumbs. The token `{link}`
|
|
||||||
* will be replaced with the actual HTML link for each inactive item.
|
|
||||||
*/
|
*/
|
||||||
public $itemTemplate = "<li class=\"breadcrumb-item\">{link}</li>\n";
|
public $itemTemplate = "<li class=\"breadcrumb-item\">{link}</li>\n";
|
||||||
/**
|
/**
|
||||||
* @var string the template used to render each active item in the breadcrumbs. The token `{link}`
|
* {@inheritDoc}
|
||||||
* will be replaced with the actual HTML link for each active item.
|
|
||||||
*/
|
*/
|
||||||
public $activeItemTemplate = "<li class=\"breadcrumb-item active\" aria-current=\"page\">{link}</li>\n";
|
public $activeItemTemplate = "<li class=\"breadcrumb-item active\" aria-current=\"page\">{link}</li>\n";
|
||||||
/**
|
/**
|
||||||
@ -78,54 +73,42 @@ class Breadcrumbs extends Widget
|
|||||||
*/
|
*/
|
||||||
public $navOptions = ['aria' => ['label' => 'breadcrumb']];
|
public $navOptions = ['aria' => ['label' => 'breadcrumb']];
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the widget.
|
|
||||||
* If you override this method, make sure you call the parent implementation first.
|
|
||||||
*/
|
|
||||||
public function init()
|
|
||||||
{
|
|
||||||
parent::init();
|
|
||||||
$this->clientOptions = [];
|
|
||||||
Html::addCssClass($this->options, ['widget' => 'breadcrumb']);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
public function run(): string
|
public function run(): string
|
||||||
{
|
{
|
||||||
if (!isset($this->options['id'])) {
|
|
||||||
$this->options['id'] = "{$this->getId()}-breadcrumb";
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @psalm-suppress InvalidArgument */
|
|
||||||
Html::addCssClass($this->options, ['widget' => 'breadcrumb']);
|
|
||||||
|
|
||||||
if (empty($this->links)) {
|
if (empty($this->links)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Normalize links
|
||||||
$links = [];
|
$links = [];
|
||||||
|
foreach ($this->links as $key => $value) {
|
||||||
|
if (is_array($value)) {
|
||||||
|
$links[] = $value;
|
||||||
|
} else {
|
||||||
|
$links[] = ['label' => $value, 'url' => is_string($key) ? $key : null];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->links = $links;
|
||||||
|
unset($links);
|
||||||
|
|
||||||
if ($this->homeLink === []) {
|
if ($this->homeLink === []) {
|
||||||
$links[] = $this->renderItem([
|
$this->homeLink = null;
|
||||||
'label' => Yii::t('yii/bootstrap5', 'Home'),
|
|
||||||
'url' => Yii::$app->homeUrl,
|
|
||||||
], $this->itemTemplate);
|
|
||||||
} elseif ($this->homeLink !== false) {
|
|
||||||
$links[] = $this->renderItem($this->homeLink, $this->itemTemplate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->links as $link) {
|
if (!isset($this->options['id'])) {
|
||||||
if (!is_array($link)) {
|
$this->options['id'] = "{$this->getId()}-breadcrumb";
|
||||||
$link = ['label' => $link];
|
|
||||||
}
|
}
|
||||||
|
Html::addCssClass($this->options, ['widget' => 'breadcrumb']);
|
||||||
|
|
||||||
$links[] = $this->renderItem($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate);
|
// parent method not return result
|
||||||
}
|
ob_start();
|
||||||
|
parent::run();
|
||||||
|
$content = ob_get_clean();
|
||||||
|
|
||||||
return Html::tag('nav', Html::tag($this->tag, implode('', $links), $this->options), $this->navOptions);
|
return Html::tag('nav', $content, $this->navOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -165,7 +148,7 @@ 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|bool $value
|
* @param array|false $value
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@ -192,16 +175,8 @@ class Breadcrumbs extends Widget
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of links to appear in the breadcrumbs. If this property is empty, the widget will not render anything. Each
|
* List of links to appear in the breadcrumbs. If this property is empty, the widget will not render anything.
|
||||||
* array element represents a single link in the breadcrumbs with the following structure:
|
* Each array element represents a single item 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
|
|
||||||
* ]
|
|
||||||
* ```
|
|
||||||
*
|
*
|
||||||
* @param array $value
|
* @param array $value
|
||||||
*
|
*
|
||||||
@ -259,40 +234,4 @@ class Breadcrumbs extends Widget
|
|||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Renders a single breadcrumb item.
|
|
||||||
*
|
|
||||||
* @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional.
|
|
||||||
* @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the
|
|
||||||
* link.
|
|
||||||
*
|
|
||||||
* @return string the rendering result
|
|
||||||
* @throws RuntimeException if `$link` does not have "label" element.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
protected function renderItem(array $link, string $template): string
|
|
||||||
{
|
|
||||||
$encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels);
|
|
||||||
|
|
||||||
if (array_key_exists('label', $link)) {
|
|
||||||
$label = $encodeLabel ? Html::encode($link['label']) : $link['label'];
|
|
||||||
} else {
|
|
||||||
throw new RuntimeException('The "label" element is required for each link.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($link['template'])) {
|
|
||||||
$template = $link['template'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($link['url'])) {
|
|
||||||
$options = $link;
|
|
||||||
unset($options['template'], $options['label'], $options['url']);
|
|
||||||
$linkHtml = Html::a($label, $link['url'], $options);
|
|
||||||
} else {
|
|
||||||
$linkHtml = $label;
|
|
||||||
}
|
|
||||||
|
|
||||||
return strtr($template, ['{link}' => $linkHtml]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ HTML;
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$expected = <<<HTML
|
$expected = <<<HTML
|
||||||
<nav aria-label="breadcrumb"><ol id="w0" class="breadcrumb"><li class="breadcrumb-item"><a href="/index.php">Start</a></li>
|
<nav aria-label="breadcrumb"><ol id="w0" class="breadcrumb"><li class="breadcrumb-item"><a href="/index.php">Home</a></li>
|
||||||
<li class="breadcrumb-item"><a href="#">Library</a></li>
|
<li class="breadcrumb-item"><a href="#">Library</a></li>
|
||||||
<li class="breadcrumb-item active" aria-current="page">Data</li>
|
<li class="breadcrumb-item active" aria-current="page">Data</li>
|
||||||
</ol></nav>
|
</ol></nav>
|
||||||
|
Loading…
Reference in New Issue
Block a user