I'm trying to create a page with a menu which has a different visibility for different users. I have 3 users: Users, Moderators and admins. Could somebody help me with this issue. Here is my code:
echo Menu::widget([
'items' => [
['label' => 'Acties', 'visible' => Yii::$app->user->getIdentity('user')],
['label' => 'Maak afbeeldingen aan', 'url' => ['create'], 'icon' => 'file', 'visible' => Yii::$app->user->getIdentity('moderator')],
['label' => 'Beheer afbeeldingen', 'url' => ['admin'], 'icon' => 'list-alt', 'visible' => Yii::$app->user->getIdentity('admin')],
],
]);
visible option is boolean. Defaults to true.
show this Menu widget
also show the getIdentity()
try below code
echo Menu::widget([
'items' => [
['label' => 'Acties', 'visible' => Yii::$app->user->getIdentity('user') ? true : false],
['label' => 'Maak afbeeldingen aan', 'url' => ['create'], 'icon' => 'file', 'visible' => Yii::$app->user->getIdentity('moderator') ? true : false],
['label' => 'Beheer afbeeldingen', 'url' => ['admin'], 'icon' => 'list-alt', 'visible' => Yii::$app->user->getIdentity('admin') ? true : false],
],
]);
You try to right if condition example:
if(Yii::$app->user->getIdentity('moderator')){
echo Menu::widget([
'items' => [
['label' => 'Maak afbeeldingen aan', 'url' => ['create'], 'icon' => 'file'],
],
]);
}
Related
I created a navigation menu in yii2 and i have links generated by the following code:
<?= HeaderNavigation::widget([
'options' => ['class' => 'nav nav-tabs border-0 flex-column flex-lg-row'],
'itemOptions' => [
'class' => 'nav-item',
],
'items' => [
[
'label' => Yii::t('lang', 'Browse'),
'url' => ['directory/index'],
'icon' => 'home',
'title' => 'HELLO WORLD'
],
],
]) ?>
but title attribute doesn't work. What i m doing wrong??
OK, finally i managed to make it work with the help of Michal Hynčica.
I had to use options key:
'options' => ['title' => 'HELLO WORLD'],
so the code should be:
'items' => [
[
'options' => ['title' => 'HELLO WORLD'],
'label' => Yii::t('lang', 'Browse'),
'url' => ['directory/index'],
'icon' => 'home',
'title' => 'HELLO WORLD'
],
I have customized my gridview in Yii2 to show columns, headers, and pager in a certain way
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'filterSelector' => 'select[name="per-page"]',
'tableOptions'=> ['class'=>'table datatable-header-footer datatable-header-footer'],
'showFooter' => true,
'layout'=>"{items}\n\n{summary}\n\n<div class='text-right'>{pager}</div>\n",
//'summary' => "{begin} - {end} {count} {totalCount} {page} {pageCount}",
'summary' => " <br/> Affichage de {begin} à {end} des {totalCount} lignes <br/> <br/>",
'formatter' => ['class' => 'yii\i18n\Formatter','nullDisplay' => ' - '],
'pager' => [
'nextPageLabel' => '→',
'prevPageLabel' => '←',
'firstPageLabel' => true,
'maxButtonCount' => 5,
'lastPageLabel' => true
],
'columns' => [
// ['class' => 'yii\grid\SerialColumn'],
//'id', ...
['class' => 'yii\grid\ActionColumn',
'contentOptions' => ['style' => 'width:10%;'],
'header'=>'Actions',
'template' => '{all}',
'buttons' => [
'all' => function ($url, $model, $key) {
return ButtonDropdown::widget([
'encodeLabel' => false,
'label' => 'Choisir',
'dropdown' => [
'encodeLabels' => false,
'items' => [
[
'label' => \Yii::t('yii', '<i class="icon-search4"></i> Details'),
'url' => ['view', 'id' => $key],
],
[
'label' => \Yii::t('yii', '<i class="icon-pencil5"></i> Modifier'),
'url' => ['update', 'id' => $key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-list"></i> Annonces'),
'url' => ['annonces', 'agence_id' => $key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-list2"></i> Agents'),
'url' => ['professionnels', 'agence_id' =>$key],
'visible' => true,
],
[
'label' => \Yii::t('yii', '<i class="icon-bin"></i> Supprimer'),
'linkOptions' => [
'data' => [
'method' => 'post',
'confirm' => \Yii::t('yii', 'Are you sure you want to delete this item?'),
],
],
'url' => ['delete', 'id' => $key],
'visible' => true, // same as above
],
],
'options' => [
'class' => 'dropdown-menu-right', // right dropdown
],
],
'options' => [
'class' => 'btn-default',
'style' => 'padding-left: 5px; padding-right: 5px;', // btn-success, btn-info, et cetera
],
'split' => true,
]);
Im wondering if there is a way to create a gridview "style" so I can call and apply this style to every gridview in the project !
Yes, extend GridView class with your own, set all properties, override anything you need and use your class instead of default GridView.
I am using the YII2 Menu Widget and did not find the solution to add attribute options like class , target on created link.
My code is below:
echo Menu::widget(
[
'options' => [
'class' => 'sidebar-menu'
],
'items' => [
[
'label' => Yii::t('backend', 'Admin'),
'url' => Yii::$app->homeUrl,
'icon' => 'fa-list-alt',
'options' => [
'class' => 'treeview',
],
'items' => [
[
'label' => Yii::t('backend', 'External link'),
'url' => 'http://google.com',
'icon' => 'fa-list-alt',
'options' => [
'target' => '_blank',
],
],
]
],
]
]
);
Option target is not added on generated link.
add the target like below through the template setting. The Options you have set in your code are the Html Options of the li element and not the link options.
'items' => [
[
'label' => Yii::t('backend', 'External link'),
'url' => 'http://google.com',
'icon' => 'fa-list-alt',
'template'=> '{label}',
],
]
Suggestions above do not seem to be working (in my case), an alternative solution is:
'linkOptions' => ['target' => '_blank']
For example
[
'url' => \Yii::$app->user->identity->getBlogLink(),
'linkOptions' => ['target' => '_blank'] ,
'label' => \Yii::t('app', 'Blog')
]
If you want to keep the icon in your menu:
'template'=> '{icon}{label}'
As regards the class you must specify it in the options key:
[
'label' => 'Debug',
'icon' => 'fa fa-dashboard',
'url' => ['/debug'],
'options' => ['class' => 'special'],
'template'=> '{icon}{label}',
],
gives the following menu:
<li class="special">
<a target="_blank" href="your_site_url_here/index.php?r=debug">
<i class="fa fa-dashboard"></i>
<span>Debug</span>
</a>
</li>
You can add any url. For Example,
echo Menu::widget([
'items' => [
['label' => 'Home', 'url' => ['http://www.google.com']],
['label' => 'About', 'url' => ['site/about']],
['label' => 'Contact', 'url' => ['site/contact']],
],
'options' => [
'class' => 'navbar-nav nav',
'id'=>'navbar-id',
'style'=>'font-size: 14px;',
'data-tag'=>'yii2-menu',
],
]);
When I use the following code, the end result is " TEST " and should be "TEST" in the tags .
echo Menu::widget([
'options' => ['id' => 'navigate'],
'items' => [
[
'label' => '<span>TEST</span>',
'url' => ['#'],
],
['label' => '<span>TEST2</span>', 'url' => ['#']],
],
]);
I know I should use encodeLabel but does not work and returns an error. Can you do to help?
It works fine for me:
echo Menu::widget([
'options' => ['id' => 'navigate'],
'encodeLabels' => false,
'items' => [
[
'label' => '<span>TEST</span>',
'url' => ['#'],
],
['label' => '<span>TEST2</span>', 'url' => ['#']],
],
]);
In my main.php, I have a menu:
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'Biz Staff', 'url' => ['bizstaff/index'], 'visible' => User::isBizAdmin(), 'items' => [
['label' => 'Staff List', 'url' => 'index.php?r=user/index'],
['label' => 'Add Staff', 'url' => 'index.php?r=user/create'],
]],
['label' => 'Transaction', 'url' => ['transactions/index'], 'visible' => User::isBizAdmin() || User::isBizStaff(), 'items' => [
['label' => 'Transactions', 'url' => 'index.php?r=transactions/index'],
['label' => 'Add Transactions', 'url' => 'index.php?r=transactions/create'],
]],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
Here, I am logged in as the superadmin (note that only Home and Logout menu is visible to superadmin user). Inside the homepage (Home menu) is a List of Biz Admins which is placed inside a GridView widget. It has an Action column where view, update and delete icons are placed. When I click the view icon of a specific Biz Admin, it will then render a detailed view page of that Biz Admin where its Store Name and List of Staff are seen. In this page, there is a View Store button which will redirect to the bizadmin view/page.
Whenever superadmin lands on bizadmin view/page, the menu bar should now change to:
Home, Biz Staff, Transaction, Logout
How do I do this? Is it set in the visible attribute?
Any of your answers would be highly appreciated. I am currently stuck in this problem.
Yes. You should use visible property. 'visible' => true or 'visible' => false.
Or you may assemble an array. Like that:
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
];
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
$menuItems[] = [
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
See in Advanced template - https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/views/layouts/main.php
You can make use of Yii::$app->controller->action->id or Yii::$app->controller->id to render menus based on your path.
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
];
if (Yii::$app->controller->action->id == 'put your id here') {
$menuItems[] = ['render your menu here'];
} else {
'do something here'
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();