From f632f47746ca4ea84a77e9ee631e997e027de0b3 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 6 Dec 2014 09:36:42 -0500 Subject: [PATCH] Fixes #5494: Added support for specifying a menu header as a configuration array in `yii\bootstrap\Dropdown` --- CHANGELOG.md | 1 + Dropdown.php | 41 ++++++++++++++++++++++++----------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b821620..6a18237 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Yii Framework 2 bootstrap extension Change Log - Enh #4146: Added `yii\bootstrap\ButtonDropdown::$containerOptions` (samdark) - Enh #4181: Added `yii\bootstrap\Modal::$headerOptions` and `yii\bootstrap\Modal::$footerOptions` (tuxoff, samdark) - Enh #4450: Added `yii\bootstrap\Nav::renderDropdown()` (qiangxue) +- Enh #5494: Added support for specifying a menu header as a configuration array in `yii\bootstrap\Dropdown` (hiltonjanfield, qiangxue) - Enh #5735: Added `yii\bootstrap\Tabs::renderTabContent` to support manually rendering tab contents (RomeroMsk) - Enh #5799: `yii\bootstrap\ButtonGroup::buttons` can take all options that are supported by `yii\bootstrap\Button` (aleksanderd) - Chg #5874: Upgraded Twitter Bootstrap to 3.3.x (samdark) diff --git a/Dropdown.php b/Dropdown.php index 1803ac2..166b367 100644 --- a/Dropdown.php +++ b/Dropdown.php @@ -10,6 +10,7 @@ namespace yii\bootstrap; use yii\base\InvalidConfigException; use yii\helpers\ArrayHelper; use yii\helpers\Html; +use yii\helpers\Url; /** * Dropdown renders a Bootstrap dropdown menu component. @@ -25,7 +26,8 @@ class Dropdown extends Widget * or an array representing a single menu with the following structure: * * - label: string, required, the label of the item link - * - url: string, optional, the url of the item link. Defaults to "#". + * - url: string|array, optional, the url of the item link. This will be processed by [[Url::to()]]. + * If not set, the item will be treated as a menu header when the item has no sub-menu. * - visible: boolean, optional, whether this menu item is visible. Defaults to true. * - linkOptions: array, optional, the HTML attributes of the item link. * - options: array, optional, the HTML attributes of the item. @@ -40,11 +42,6 @@ class Dropdown extends Widget */ public $encodeLabels = true; - /** - * @var array the HTML attributes for the widget container tag. - * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. - */ - protected $_containerOptions = []; /** * Initializes the widget. @@ -54,7 +51,6 @@ class Dropdown extends Widget { parent::init(); Html::addCssClass($this->options, 'dropdown-menu'); - $this->_containerOptions = $this->options; } /** @@ -62,17 +58,18 @@ class Dropdown extends Widget */ public function run() { - echo $this->renderItems($this->items); + echo $this->renderItems($this->items, $this->options); BootstrapPluginAsset::register($this->getView()); } /** * Renders menu items. * @param array $items the menu items to be rendered + * @param array $options the container HTML attributes * @return string the rendering result. * @throws InvalidConfigException if the label option is not specified in one of the items. */ - protected function renderItems($items) + protected function renderItems($items, $options = []) { $lines = []; foreach ($items as $i => $item) { @@ -89,18 +86,28 @@ class Dropdown extends Widget } $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; $label = $encodeLabel ? Html::encode($item['label']) : $item['label']; - $options = ArrayHelper::getValue($item, 'options', []); + $itemOptions = ArrayHelper::getValue($item, 'options', []); $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); $linkOptions['tabindex'] = '-1'; - $content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions); - if (!empty($item['items'])) { - unset($this->_containerOptions['id']); - $content .= $this->renderItems($item['items']); - Html::addCssClass($options, 'dropdown-submenu'); + $url = array_key_exists('url', $item) ? $item['url'] : null; + if (empty($item['items'])) { + if ($url === null) { + $content = $label; + Html::addCssClass($itemOptions, 'dropdown-header'); + } else { + $content = Html::a($label, $url, $linkOptions); + } + } else { + $submenuOptions = $options; + unset($submenuOptions['id']); + $content = Html::a($label, $url === null ? '#' : $url, $linkOptions) + . $this->renderItems($item['items'], $submenuOptions); + Html::addCssClass($itemOptions, 'dropdown-submenu'); } - $lines[] = Html::tag('li', $content, $options); + + $lines[] = Html::tag('li', $content, $itemOptions); } - return Html::tag('ul', implode("\n", $lines), $this->_containerOptions); + return Html::tag('ul', implode("\n", $lines), $options); } }