Browse Source

Fixes #219, Fixes #171: Add ability to use custom HTML in navbar-header, Add ability to use a brandImage with the navbar

tags/2.0.8
Razvan Grigore 7 years ago committed by Alexander Makarov
parent
commit
c330c0e2c1
  1. 3
      CHANGELOG.md
  2. 19
      NavBar.php
  3. 28
      tests/NavBarTest.php

3
CHANGELOG.md

@ -4,7 +4,8 @@ Yii Framework 2 bootstrap extension Change Log
2.0.8 under development 2.0.8 under development
----------------------- -----------------------
- no changes in this release. - Enh #219: Add ability to use custom HTML in navbar-header (razvanphp)
- Enh #171: Add ability to use a brandImage with the navbar (razvanphp)
2.0.7 October 09, 2017 2.0.7 October 09, 2017

19
NavBar.php

@ -32,7 +32,7 @@ use yii\helpers\ArrayHelper;
* NavBar::end(); * NavBar::end();
* ``` * ```
* *
* @see http://getbootstrap.com/components/#navbar * @see https://getbootstrap.com/docs/3.3/components/#navbar
* @author Antonio Ramirez <amigo.cobos@gmail.com> * @author Antonio Ramirez <amigo.cobos@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com> * @author Alexander Kochetov <creocoder@gmail.com>
* @since 2.0 * @since 2.0
@ -57,10 +57,16 @@ class NavBar extends Widget
public $containerOptions = []; public $containerOptions = [];
/** /**
* @var string|boolean the text of the brand or false if it's not used. Note that this is not HTML-encoded. * @var string|boolean the text of the brand or false if it's not used. Note that this is not HTML-encoded.
* @see http://getbootstrap.com/components/#navbar * @see https://getbootstrap.com/docs/3.3/components/#navbar
*/ */
public $brandLabel = false; public $brandLabel = false;
/** /**
* @var string|boolean src of the brand image or false if it's not used. Note that this param will override `$this->brandLabel` param.
* @see https://getbootstrap.com/docs/3.3/components/#navbar
* @since 2.0.8
*/
public $brandImage = false;
/**
* @var array|string|boolean $url the URL for the brand's hyperlink tag. This parameter will be processed by [[\yii\helpers\Url::to()]] * @var array|string|boolean $url the URL for the brand's hyperlink tag. This parameter will be processed by [[\yii\helpers\Url::to()]]
* and will be used for the "href" attribute of the brand link. Default value is false that means * and will be used for the "href" attribute of the brand link. Default value is false that means
* [[\yii\web\Application::homeUrl]] will be used. * [[\yii\web\Application::homeUrl]] will be used.
@ -73,6 +79,11 @@ class NavBar extends Widget
*/ */
public $brandOptions = []; public $brandOptions = [];
/** /**
* @var string HTML content to be added in navbar-header div, for example, mobile search form.
* @since 2.0.8
*/
public $headerContent;
/**
* @var string text to show for screen readers for the button to toggle the navbar. * @var string text to show for screen readers for the button to toggle the navbar.
*/ */
public $screenReaderToggleText = 'Toggle navigation'; public $screenReaderToggleText = 'Toggle navigation';
@ -114,10 +125,14 @@ class NavBar extends Widget
$this->containerOptions['id'] = "{$this->options['id']}-collapse"; $this->containerOptions['id'] = "{$this->options['id']}-collapse";
} }
echo $this->renderToggleButton(); echo $this->renderToggleButton();
if ($this->brandImage !== false) {
$this->brandLabel = Html::img($this->brandImage);
}
if ($this->brandLabel !== false) { if ($this->brandLabel !== false) {
Html::addCssClass($this->brandOptions, ['widget' => 'navbar-brand']); Html::addCssClass($this->brandOptions, ['widget' => 'navbar-brand']);
echo Html::a($this->brandLabel, $this->brandUrl === false ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions); echo Html::a($this->brandLabel, $this->brandUrl === false ? Yii::$app->homeUrl : $this->brandUrl, $this->brandOptions);
} }
echo $this->headerContent;
echo Html::endTag('div'); echo Html::endTag('div');
Html::addCssClass($this->containerOptions, ['collapse' => 'collapse', 'widget' => 'navbar-collapse']); Html::addCssClass($this->containerOptions, ['collapse' => 'collapse', 'widget' => 'navbar-collapse']);
$options = $this->containerOptions; $options = $this->containerOptions;

28
tests/NavBarTest.php

@ -31,4 +31,32 @@ EXPECTED;
$this->assertEqualsWithoutLE($expected, $out); $this->assertEqualsWithoutLE($expected, $out);
} }
public function testBrandImage()
{
$out = NavBar::widget([
'brandImage' => '/images/test.jpg',
'brandUrl' => '/',
]);
$this->assertContains('<a class="navbar-brand" href="/"><img src="/images/test.jpg" alt=""></a>', $out);
}
public function testHeaderContent()
{
$testContent = <<<HTML
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
HTML;
$out = NavBar::widget([
'headerContent' => $testContent,
]);
$this->assertContains($testContent, $out);
}
} }

Loading…
Cancel
Save