Breadcrumbs.php 4.84 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\widgets;
Qiang Xue committed
9 10 11

use Yii;
use yii\base\Widget;
12
use yii\base\InvalidConfigException;
Qiang Xue committed
13 14 15
use yii\helpers\Html;

/**
16 17 18 19 20 21
 * Breadcrumbs displays a list of links indicating the position of the current page in the whole site hierarchy.
 *
 * For example, breadcrumbs like "Home / Sample Post / Edit" means the user is viewing an edit page
 * for the "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home"
 * to return to the homepage.
 *
22
 * To use Breadcrumbs, you need to configure its [[links]] property, which specifies the links to be displayed. For example,
23 24
 *
 * ~~~
25
 * // $this is the view object currently being used
Alexander Makarov committed
26 27 28
 * echo Breadcrumbs::widget([
 *     'links' => [
 *         ['label' => 'Sample Post', 'url' => ['post/edit', 'id' => 1]],
29
 *         'Edit',
Alexander Makarov committed
30 31
 *     ],
 * ]);
32 33
 * ~~~
 *
34 35
 * Because breadcrumbs usually appears in nearly every page of a website, you may consider placing it in a layout view.
 * You can use a view parameter (e.g. `$this->params['breadcrumbs']`) to configure the links in different
36 37 38
 * views. In the layout view, you assign this view parameter to the [[links]] property like the following:
 *
 * ~~~
39
 * // $this is the view object currently being used
Alexander Makarov committed
40 41 42
 * echo Breadcrumbs::widget([
 *     'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
 * ]);
43
 * ~~~
Qiang Xue committed
44 45 46 47 48 49 50
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Breadcrumbs extends Widget
{
	/**
Qiang Xue committed
51
	 * @var string the name of the breadcrumb container tag.
Qiang Xue committed
52
	 */
Qiang Xue committed
53 54 55
	public $tag = 'ul';
	/**
	 * @var array the HTML attributes for the breadcrumb container tag.
56
	 * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
Qiang Xue committed
57
	 */
Alexander Makarov committed
58
	public $options = ['class' => 'breadcrumb'];
Qiang Xue committed
59
	/**
60
	 * @var boolean whether to HTML-encode the link labels.
Qiang Xue committed
61 62 63
	 */
	public $encodeLabels = true;
	/**
Qiang Xue committed
64 65
	 * @var array the first hyperlink in the breadcrumbs (called home link).
	 * Please refer to [[links]] on the format of the link.
66 67
	 * 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.
Qiang Xue committed
68 69 70
	 */
	public $homeLink;
	/**
71 72 73
	 * @var array 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 link in the breadcrumbs
	 * with the following structure:
Qiang Xue committed
74
	 *
75
	 * ~~~
Alexander Makarov committed
76
	 * [
77
	 *     'label' => 'label of the link',  // required
78
	 *     'url' => 'url of the link',      // optional, will be processed by Url::to()
Alexander Makarov committed
79
	 * ]
80 81
	 * ~~~
	 *
Alexander Makarov committed
82
	 * If a link is active, you only need to specify its "label", and instead of writing `['label' => $label]`,
83
	 * you should simply use `$label`.
Qiang Xue committed
84
	 */
Alexander Makarov committed
85
	public $links = [];
Qiang Xue committed
86
	/**
87 88
	 * @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.
Qiang Xue committed
89
	 */
90
	public $itemTemplate = "<li>{link}</li>\n";
Qiang Xue committed
91
	/**
92 93
	 * @var string 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.
Qiang Xue committed
94 95 96 97
	 */
	public $activeItemTemplate = "<li class=\"active\">{link}</li>\n";

	/**
98
	 * Renders the widget.
Qiang Xue committed
99 100 101
	 */
	public function run()
	{
102
		if (empty($this->links)) {
Qiang Xue committed
103 104
			return;
		}
Alexander Makarov committed
105
		$links = [];
Qiang Xue committed
106
		if ($this->homeLink === null) {
Alexander Makarov committed
107
			$links[] = $this->renderItem([
108
				'label' => Yii::t('yii', 'Home'),
Qiang Xue committed
109
				'url' => Yii::$app->homeUrl,
Alexander Makarov committed
110
			], $this->itemTemplate);
Qiang Xue committed
111
		} elseif ($this->homeLink !== false) {
Qiang Xue committed
112
			$links[] = $this->renderItem($this->homeLink, $this->itemTemplate);
Qiang Xue committed
113 114
		}
		foreach ($this->links as $link) {
115
			if (!is_array($link)) {
Alexander Makarov committed
116
				$link = ['label' => $link];
117
			}
Qiang Xue committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
			$links[] = $this->renderItem($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate);
		}
		echo Html::tag($this->tag, implode('', $links), $this->options);
	}

	/**
	 * 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 InvalidConfigException if `$link` does not have "label" element.
	 */
	protected function renderItem($link, $template)
	{
		if (isset($link['label'])) {
			$label = $this->encodeLabels ? Html::encode($link['label']) : $link['label'];
		} else {
			throw new InvalidConfigException('The "label" element is required for each link.');
		}
		if (isset($link['url'])) {
Alexander Makarov committed
138
			return strtr($template, ['{link}' => Html::a($label, $link['url'])]);
Qiang Xue committed
139
		} else {
Alexander Makarov committed
140
			return strtr($template, ['{link}' => $label]);
Qiang Xue committed
141 142
		}
	}
Qiang Xue committed
143
}