Class yii\bootstrap5\Breadcrumbs

Inheritanceyii\bootstrap5\Breadcrumbs » yii\widgets\Breadcrumbs
Uses Traitsyii\bootstrap5\BootstrapWidgetTrait
Source Code https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/Breadcrumbs.php

This widget represents a Bootstrap 5 component "Breadcrumb". It displays a list of links indicating the position of the current page in the whole site hierarchy.

echo Breadcrumbs::widget([
    'links' => [
        [
            '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 also https://getbootstrap.com/docs/5.1/components/breadcrumb/.

Public Properties

Hide inherited properties

Property Type Description Defined By
$activeItemTemplate yii\bootstrap5\Breadcrumbs
$clientEvents array The event handlers for the underlying Bootstrap JS plugin. yii\bootstrap5\BootstrapWidgetTrait
$clientOptions array|false The options for the underlying Bootstrap JS plugin/component. yii\bootstrap5\BootstrapWidgetTrait
$itemTemplate yii\bootstrap5\Breadcrumbs
$navOptions array The HTML attributes for the widgets nav container tag. yii\bootstrap5\Breadcrumbs
$tag yii\bootstrap5\Breadcrumbs

Public Methods

Hide inherited methods

Method Description Defined By
activeItemTemplate() The template used to render each active item in the breadcrumbs. The token {link} will be replaced with the actual HTML link for each active item. yii\bootstrap5\Breadcrumbs
encodeLabels() Whether to HTML-encode the link labels. yii\bootstrap5\Breadcrumbs
homeLink() The first hyperlink in the breadcrumbs (called home link). yii\bootstrap5\Breadcrumbs
init() Initializes the widget. yii\bootstrap5\BootstrapWidgetTrait
itemTemplate() 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. yii\bootstrap5\Breadcrumbs
links() List of links to appear in the breadcrumbs. If this property is empty, the widget will not render anything. yii\bootstrap5\Breadcrumbs
options() The HTML attributes for the widget container tag. The following special options are recognized. yii\bootstrap5\Breadcrumbs
run() yii\bootstrap5\Breadcrumbs
tag() The name of the breadcrumb container tag. yii\bootstrap5\Breadcrumbs

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers JS event handlers that are listed in $clientEvents. yii\bootstrap5\BootstrapWidgetTrait
registerPlugin() Registers a specific Bootstrap plugin/component and the related events. yii\bootstrap5\BootstrapWidgetTrait

Property Details

Hide inherited properties

$activeItemTemplate public property
public $activeItemTemplate "<li class=\"breadcrumb-item active\" aria-current=\"page\">{link}</li>\n"
$itemTemplate public property
public $itemTemplate "<li class=\"breadcrumb-item\">{link}</li>\n"
$navOptions public property

The HTML attributes for the widgets nav container tag.

See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.

public array $navOptions = [
    
'aria' => [
        
'label' => 'breadcrumb',
    ],
]
$tag public property
public $tag 'ol'

Method Details

Hide inherited methods

activeItemTemplate() public method

The template used to render each active item in the breadcrumbs. The token {link} will be replaced with the actual HTML link for each active item.

public $this activeItemTemplate ( string $value )
$value string

                public function activeItemTemplate(string $value): self
{
    $this->activeItemTemplate = $value;
    return $this;
}

            
encodeLabels() public method

Whether to HTML-encode the link labels.

public $this encodeLabels ( boolean $value )
$value boolean

                public function encodeLabels(bool $value): self
{
    $this->encodeLabels = $value;
    return $this;
}

            
homeLink() public method

The first hyperlink in the breadcrumbs (called home link).

Please refer to {@see \yii\bootstrap5\links} on the format of the link.

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.

public $this homeLink ( array|false $value )
$value array|false

init() public method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::init()

Initializes the widget.

This method will register the bootstrap asset bundle. If you override this method, make sure you call the parent implementation first.

public void init ( )
throws \yii\base\InvalidConfigException

                public function init(): void
{
    parent::init();
    if (!isset($this->options['id'])) {
        $this->options['id'] = $this->getId();
    }
}

            
itemTemplate() public method

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 $this itemTemplate ( string $value )
$value string

                public function itemTemplate(string $value): self
{
    $this->itemTemplate = $value;
    return $this;
}

            
links() public method

List of links to appear in the breadcrumbs. If this property is empty, the widget will not render anything.

Each array element represents a single item in the breadcrumbs with the following structure.

public $this links ( array $value )
$value array

options() public method

The HTML attributes for the widget container tag. The following special options are recognized.

{@see \yii\helpers\Html::renderTagAttributes()} for details on how attributes are being rendered.

public $this options ( array $value )
$value array

                public function options(array $value): self
{
    $this->options = $value;
    return $this;
}

            
registerClientEvents() protected method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::registerClientEvents()

Registers JS event handlers that are listed in $clientEvents.

protected void registerClientEvents ( ?string $name null )
$name ?string

                protected function registerClientEvents(?string $name = null): void
{
    if (!empty($this->clientEvents)) {
        $id = $this->options['id'];
        $js = [];
        $appendix = ($name === 'dropdown') ? '.parentElement' : '';
        foreach ($this->clientEvents as $event => $handler) {
            $js[] = "document.getElementById('$id')$appendix.addEventListener('$event', $handler);";
        }
        $this->getView()->registerJs(implode("\n", $js));
    }
}

            
registerPlugin() protected method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::registerPlugin()

Registers a specific Bootstrap plugin/component and the related events.

protected void registerPlugin ( string $name )
$name string

The name of the Bootstrap plugin

                protected function registerPlugin(string $name): void
{
    /**
     * @see https://github.com/twbs/bootstrap/blob/v5.2.0/js/index.esm.js
     */
    $jsPlugins = [
        'alert',
        'button',
        'carousel',
        'collapse',
        'dropdown',
        'modal',
        'offcanvas',
        'popover',
        'scrollspy',
        'tab',
        'toast',
        'tooltip',
    ];
    if (in_array($name, $jsPlugins, true)) {
        $view = $this->getView();
        BootstrapPluginAsset::register($view);
        // 'popover', 'toast' and 'tooltip' plugins not activates via data attributes
        if ($this->clientOptions !== false || in_array($name, ['popover', 'toast', 'tooltip'], true)) {
            $name = ucfirst($name);
            $id = $this->options['id'];
            $options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
            $view->registerJs("(new bootstrap.$name('#$id', $options));");
        }
        $this->registerClientEvents($name);
    }
}

            
run() public method

public string run ( )

                public function run(): string
{
    if (empty($this->links)) {
        return '';
    }
    // Normalize 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 === []) {
        $this->homeLink = null;
    }
    if (!isset($this->options['id'])) {
        $this->options['id'] = "{$this->getId()}-breadcrumb";
    }
    Html::addCssClass($this->options, [
        'widget' => 'breadcrumb',
    ]);
    // parent method not return result
    ob_start();
    parent::run();
    $content = ob_get_clean();
    return Html::tag('nav', $content, $this->navOptions);
}

            
tag() public method

The name of the breadcrumb container tag.

public $this tag ( string $value )
$value string

                public function tag(string $value): self
{
    $this->tag = $value;
    return $this;
}