How to add raw HTML in Yii CMenu label - html

I create a menu like a Twitter Bootstrap navbar with CMenu widget:
<?php
$this->widget( 'zii.widgets.CMenu', array(
'items' => array(
array(
'label' => 'Home',
'url' => array( '/site/index' ),
),
array(
'label' => 'Dropdown <b class="caret"></b>',
'url' => '#',
'submenuOptions' => array( 'class' => 'dropdown-menu' ),
'items' => array(
array(
'label' => 'Submenu Item 1',
'url' => array( '/user/create' ),
),
array(
'label' => 'Submenu Item 1',
'url' => array( '/user/list' ),
),
),
'itemOptions' => array( 'class' => 'dropdown' ),
'linkOptions' => array( 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown' ),
),
'htmlOptions' => array( 'class' => 'nav' ),
)); ?>
This code generate menu with 2 items in it and 1 submenu for second menu item. Fine. But only thing, which not worked is 'label' => 'Dropdown <b class="caret"></b>', in 9th line. It rendered as Dropdown <b class="caret"></b> on page. So I see caption 'Dropdown <b class="caret"></b>' instead of Dropdown ▼.
How I can change code to show unescaped HTML in menu label?
Thanks for your attention.

You have to set encodeLabel attribute of CMenu to false
<?php
$this->widget('zii.widgets.CMenu', array(
'encodeLabel' => false,
'htmlOptions' => array('class' => 'nav'),
'items' => array(
array(
'label' => 'Home',
'url' => array('/site/index'),
),
array(
'label' => 'Dropdown <b class="caret"></b>',
'url' => '#',
'submenuOptions' => array('class' => 'dropdown-menu'),
'items' => array(
array(
'label' => 'Submenu Item 1',
'url' => array('/user/create'),
),
array(
'label' => 'Submenu Item 1',
'url' => array('/user/list'),
),
),
'itemOptions' => array('class' => 'dropdown'),
'linkOptions' => array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown'),
),
),
));
?>

My solution is creating CMenu extension:
layout/main.php
'submenuOptions'=>array('class'=>'dropdown-menu'),
'itemOptions'=>array('class'=>'dropdown'),
'linkOptions'=>array('class'=>'dropdown-toggle', 'data-toggle'=>'dropdown'),
// Dropdown arrow toggle
'dropdownArrow'=>true,
ext/widgets/BootstrapCMenu.php
class BootstrapCMenu extends CMenu {
protected function renderMenuItem($item)
{
if(isset($item['url']))
{
$item['label'] .= ($item['dropdownArrow']) ? ' <b class="caret"></b>' : '';
$label=$this->linkLabelWrapper===null ? $item['label'] : CHtml::tag($this->linkLabelWrapper, $this->linkLabelWrapperHtmlOptions, $item['label']);
return CHtml::link($label,$item['url'],isset($item['linkOptions']) ? $item['linkOptions'] : array());
}
else
return CHtml::tag('span',isset($item['linkOptions']) ? $item['linkOptions'] : array(), $item['label']);
}
}

In Yii 2.0, for adding glyphicon in navbar menu you can follow the below info.
Edit in vendor\yiisoft\yii2-bootstrap\Nav.php under renderItem function the following code:
if(isset($item['icons']))
$label=Html::tag('span', '', ['class' => 'glyphicon glyphicon-'.$item['icons']]).$label;
Now, you can directly use any icon from your code with icons option as
<?php
$this->widget( 'zii.widgets.CMenu', array(
'items' => array(
array(
'label' => 'Home',
'url' => array( '/site/index' ),
'icons'=> 'home',
),
array(
'label' => 'Dropdown <b class="caret"></b>',
'url' => '#',
'submenuOptions' => array( 'class' => 'dropdown-menu' ),
'items' => array(
array(
'label' => 'Submenu Item 1',
'url' => array( '/user/create' ),
),
array(
'label' => 'Submenu Item 1',
'url' => array( '/user/list' ),
),
),
'itemOptions' => array( 'class' => 'dropdown' ),
'linkOptions' => array( 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown' ),
),
'htmlOptions' => array( 'class' => 'nav' ),
)); ?>
You can make the corresponding changes even in older versions of yii.

Related

Add class to a submenu container in Yii2 Bootstrap4 Nav widget

I'm using the Nav widget in Yii2. I have a dropdown menu as part of the nav menu but the dropdown is very long and extends beyond the bottom of the page and doesn't scroll. To solve this I am trying to add the pre-scrollable class to the submenu container. Try as I might I can't seem to make it work.
In the Yii manual for the Nav widget (https://www.yiiframework.com/extension/yiisoft/yii2-bootstrap/doc/api/2.0/yii-bootstrap-nav) under the $Items public property it says:
dropDownOptions: array, optional, the HTML options that will passed to
the yii\bootstrap\Dropdown widget.
I've also looked at the man page for the Dropdown widget (https://www.yiiframework.com/extension/yiisoft/yii2-bootstrap/doc/api/2.0/yii-bootstrap-dropdown).
The code I have for my Nav widget is like:
echo Nav::widget([
'options' => ['class' => 'navbar-nav ml-auto'],
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
[
'label' => 'Dropdown menu',
//'dropDownOptions' => ['options' => ['class' => 'pre-scrollable']],
//'dropDownOptions' => ['class' => 'pre-scrollable'],
//'dropDownOptions' => ['submenuOptions' => ['class' => 'pre-scrollable']],
//'submenuOptions' => ['class' => 'pre-scrollable'],
//'submenuOptions' => ['options' => ['class' => 'pre-scrollable']],
//'submenuOptions' => ['dropDownOptions' => ['class' => 'pre-scrollable']],
//'dropDownOptions' => ['dropDownOptions' => ['class' => 'pre-scrollable']],
//'submenuOptions' => ['submenuOptions' => ['class' => 'pre-scrollable']],
//'options' => ['submenuOptions' => ['class' => 'pre-scrollable']],
//'options' => ['class' => 'pre-scrollable'],
//'options' => ['dropDownOptions' => ['class' => 'pre-scrollable']],
'items' => [
['label' => 'Dropdown menu item 1', 'url' => '#'],
['label' => 'Dropdown menu item 2', 'url' => '#'],
...
],
],
['label' => 'Contact', 'url' => ['/site/contact']],
],
]);
The commented out lines are some of the different ways I've tried to 'pass the HTML options to the yii\bootstrap\Dropdown widget' (in order of increasing desperation).
Please can someone put me right?
Thanks in anticipation!
The option dropDownOptions is tested successfully on Yii version 2.0.11.2 and Bootstrap v3.3.7:
'dropDownOptions' => ['class' => 'pre-scrollable'],
The result is additional class of tag contained submenu items
<ul id="w4" class="pre-scrollable dropdown-menu">
If you are using yii2-bootstrap4 - that options apparently renamed to dropdownOptions.
See Nav::renderDropdown() in the sources:
/**
* Renders the given items as a dropdown.
* This method is called to create sub-menus.
* #param array $items the given items. Please refer to [[Dropdown::items]] for the array structure.
* #param array $parentItem the parent item information. Please refer to [[items]] for the structure of this array.
* #return string the rendering result.
* #throws \Exception
*/
protected function renderDropdown($items, $parentItem)
{
/** #var Widget $dropdownClass */
$dropdownClass = $this->dropdownClass;
return $dropdownClass::widget([
'options' => ArrayHelper::getValue($parentItem, 'dropdownOptions', []),
'items' => $items,
'encodeLabels' => $this->encodeLabels,
'clientOptions' => false,
'view' => $this->getView(),
]);
}
https://github.com/yiisoft/yii2-bootstrap4/blob/219d19fba47b5499f01764789c3cd3ce7306e0f9/src/Nav.php#L207

Call to undefined method app\models\User::instance() setting up kartik export yii2

Trying to set up kratik grid export in Yii after doing everything I keep getting
Call to undefined method app\models\User::instance()
here is my code
$exportedValues = [['class' => 'kartik\\grid\\SerialColumn'], ['attribute' => 'deduction_date'], ['attribute' => 'deduction_amount'], ['attribute' => 'deduction_remark'], ['class' => 'kartik\\grid\\ActionColumn']];
$export = ExportMenu::widget(['dataProvider' => $dataProvider, 'columns' => $exportedValues, 'noExportColumns' => [0, 3], 'columnSelectorOptions' => ['label' => 'Columns', 'class' => 'btn btn-danger'], 'target' => '_blank', 'fontAwesome' => true, 'dropdownOptions' => ['label' => 'Export', 'class' => 'btn btn-success']]);
echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel,
'columns' => [['class' => 'yii\\grid\\SerialColumn'],
'first_name', 'last_name', 'email_id', 'phone', ['class' => 'yii\\grid\\ActionColumn']]]); ?>
Try to install kartik, or check your kartik version
php composer.phar require kartik-v/yii2-widgets "*"
Had the same issue.
on the source code of ExportMenu.php I replaced:
$model = $modelClass::instance();
by:
$model = new $modelClass;
solved it for me.

Yii2 Kartik SideNav How to set 'active' option

I have Kartik's SideNav (http://demos.krajee.com/sidenav-demo/profile/default) working properly by setting the url's to controller/action, but don't understand how to set the active option so that the menu stays open and the correct menu item stays selected.
I've tried setting 'active'=> 'controller/action' (see below) as well as
'active' => 'controller/action/default#demo' but to no avail. When I do set it as 'employee/employee-dashboard/default#demo' it does keep that one tab highlighted but won't change the others when I do it the same way on those tabs. The original was set to 'active'=> 'home' which I assume is just the action but that didn't work for me either.
$type = SideNav::TYPE_DEFAULT;
$heading = '<i class="glyphicon glyphicon-cog"></i> Employee Menu';
$item = '/employee/employee-dashboard/default#demo';
echo SideNav::widget([
'type' => $type,
'encodeLabels' => false,
'heading' => $heading,
'items' => [
// Important: you need to specify url as 'controller/action',
// not just as 'controller' even if default action is used.
//
// NOTE: The variable `$item` is specific to this demo page that determines
// which menu item will be activated. You need to accordingly define and pass
// such variables to your view object to handle such logic in your application
// (to determine the active status).
//
['label' => 'Dashboard', 'icon' => 'home', 'url' => Url::to(['/employee/employee-dashboard', 'type'=>$type]), 'active' => ($item == '/employee/employee-dashboard/default#demo')]
['label' => 'Paycheck Info', 'icon' => 'book', 'items' => [
['label' => '<span class="pull-right badge">10</span> W2 Forms', 'url' => Url::to(['/employee/w2-form', 'type'=>$type]), 'active' => ($item == 'w2-form')],
'active'=>($item == 'about')
where
$item = Yii::$app->controller->action->id;
This is how i used
<?php
$item = Yii::$app->controller->action->id;
echo SideNav::widget([
'type' => SideNav::TYPE_DEFAULT,
'heading' => 'Options',
'items' => [
[
'url' => Yii::$app->homeUrl,
'label' => 'Home',
'icon' => 'home',
'active'=>($item == 'index')
],
[
'label' => 'Help',
'icon' => 'question-sign',
'items' => [
['label' => 'About', 'icon'=>'info-sign', 'url'=>Yii::$app->homeUrl.'site/about', 'active'=>($item == 'about')],
['label' => 'Contact', 'icon'=>'phone', 'url'=>Yii::$app->homeUrl.'site/contact', 'active'=>($item == 'contact')],
],
],
],
]);
?>
One solution I found if you want to set the active menu item on a per page basis is:
Create a separate view file (e.g. sidemenu.php) with just the SideNav widget, which accepts a parameter called 'currentPage'.
echo SideNav::widget([
'type' => SideNav::TYPE_DEFAULT,
'heading' => 'Options',
'items' => [
[
'url' => Url::toRoute('/site/index'),
'label' => 'Home',
'icon' => 'file',
'active'=>($currentPage == 'home'),
],
[
'url' => Url::toRoute('/site/about'),
'label' => 'About',
'icon' => 'file',
'active'=>($currentPage == 'about'),
],
Then in the view page where you are rendering the menu, set the currentPage property -- for example:
echo $this->render('sidemenu', ['currentPage'=>'home']);
or
echo $this->render('sidemenu', ['currentPage'=>'about']);

Dynamic value graph creation in yii2 using GoogleChart?

Here I wanna draw line chart graph by dynamic value.but in my case for every value of array created different different graph...Please help me I am first time do this task.Thanks in Advances
<?php
$modelEmployee=Employee::find()->select(['id','sales','expenses'])->all();
$arr = array('id'=>array(),
'sales'=>array(),
'expenses'=>array());
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
$arr['id'] = $modEm[$i]['id'];
$arr['sales'] = $modEm[$i]['sales'];
$arr['expenses'] = $modEm[$i]['expenses'];
print_r($arr);
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => array(
array('Year', 'Sales', 'Expenses'),
array($arr['id'],$arr['sales'],$arr['expenses']),
),
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));
?>
first of all the multiple graphs are because you are doing echo inside for loop so it will take only one value and create graph from that.
you have to create an array of values and pass it to the graph widget as following
$graph_data = [];
$graph_data[] = array('Year', 'Sales', 'Expenses');
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
$arr['id'] = $modEm[$i]['id'];
$arr['sales'] = $modEm[$i]['sales'];
$arr['expenses'] = $modEm[$i]['expenses'];
$graph_data[] = array($arr['id'],$arr['sales'],$arr['expenses']); //add the values you require as set in the order of Year, Sales , Expenses
} //loop ends here
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => $graph_data,
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));
Try this
action
$model=Employee::find()->select(['id','sales','expenses'])->all();
$data[]=["id","sales","expenses"];
foreach ($model as $item) {
$data[]=[(string) $item['id'],(int) $item['sales'],(int) $item['expenses']];
}
return $this->render('test',['data'=>$data]);
view
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => $data,
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));

Drupal Html array. How to reference a changing radio selection within a fieldset

I cannot make this Drupal html array fire the visible property on change of radio option. I have moved everything inside of the fieldset, not sure if this makes any difference.
Does anyone know why it isn't firing?
function services_formation_founders($form, &$form_state) {
$form = array();
$form['#tree'] = TRUE;
$form['description'] = array(
'#type' => 'item',
'#title' => t('Founders form'),
);
$form['founder']['add_officer'] = array(
'#type' => 'fieldset',
'#title' => t('Add Founder'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(TRUE,
),
),
);
$form['founder']['add_officer']['founder_type'] = array(
'#type' => 'radios',
'#options' => array(
'individual' => t('Individual'),
'corporate' => t('Corporation'),
),
'#default_value'=>'individual',
'#title' => t('What type of Founder?')
);
if (empty($form_state['num_names'])) {
$form_state['num_names'] = 1;
}
$form['founder']['add_officer']['individual'] = array(
'#type' => 'textfield',
'#title' => t('Individual'),
'#states' => array(
'visible' => array(
':input[name="founder_type"' => array('value' => "individual"),
),
),
);
$form['founder']['add_officer']['corporation'] = array(
'#type' => 'textfield',
'#title' => t('Corporation'),
'#states' => array(
'visible' => array(
':input[name="founder_type"' => array('value' => "corporate"),
),
),
);
return $form;
}
Place $ sign at line no 2
$form = array();