Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically hide parent 'yii\bootstrap\Nav' section if all subitems are hidden #117

Open
arogachev opened this issue Feb 2, 2016 · 4 comments
Labels
status:ready for adoption Feel free to implement this issue. type:enhancement Enhancement

Comments

@arogachev
Copy link

Consider this example.

$menuItems = [
    [
        'label' => 'Miscellaneous',
        'items' => [
            [
                'label' => 'Users',
                'url' => ['/users/index'],
                'visible' => Yii::$app->user->can('users.manage'),
            ],
            [
                'label' => 'Slides',
                'url' => ['/slides/index'],
                'visible' => Yii::$app->user->can('slides.manage'),
            ],          
            [
                'label' => 'News,
                'url' => ['/news/index'],
                'visible' => Yii::$app->user->can('news.manage'),
            ],
        ],        
    ],
];

 echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
    'items' => $menuItems,
]);

If the user doesn't have permissions to manage users, slides and news, Miscellaneous section will be still shown, but it doesn't make sense because it doesn't have a link (click triggers visibility of subitems).

To conditionally hide it we have to add something like this to parent section:

'visible' => Yii::$app->user->can('users.manage') ||
    Yii::$app->user->can('slides.manage') ||
    Yii::$app->user->can('news.manage'),

This is clean violation of DRY principle. Obviously we can have a helper method for checking that, but still we need to list permissions in both places, It's better to be done inside Nav widget itself.

So, if parent section have all subitems with visible property set and visible equals false in all subitems, we can just skip rendering of this whole section.

@samdark
Copy link
Member

samdark commented Feb 2, 2016

Yep, that makes sense.

@samdark samdark added status:ready for adoption Feel free to implement this issue. type:enhancement Enhancement labels Feb 2, 2016
@arogachev
Copy link
Author

OK, I will try to send a PR in this case.

@SilverFire SilverFire added this to the 2.0.x milestone Feb 2, 2016
@arogachev arogachev changed the title Automatically hide parent Nav section if all subitems are hidden Automatically hide parent 'yii\bootstrap\Nav' section if all subitems are hidden Feb 2, 2016
@arogachev arogachev changed the title Automatically hide parent 'yii\bootstrap\Nav' section if all subitems are hidden Automatically hide parent Nav section if all subitems are hidden Feb 2, 2016
@arogachev arogachev changed the title Automatically hide parent Nav section if all subitems are hidden Automatically hide parent 'yii\bootstrap\Nav' section if all subitems are hidden Feb 2, 2016
arogachev added a commit to arogachev/yii2-bootstrap that referenced this issue Feb 2, 2016
@arogachev
Copy link
Author

Done.

@PowerGamer1
Copy link
Contributor

You all forget that \yii\bootstrap\Dropdown supports items specified as a string:

    /**
     * @var array list of menu items in the dropdown. Each array element can be either an HTML string,
     * or an array representing a single menu with the following structure:
     * ...
     * To insert divider use `<li role="presentation" class="divider"></li>`.
     */
    public $items = [];

Currently such items are considered always visible. So if the OP were to put a divider in his example, the Nav won't be hidden which is not what a user would expect from this additional functionality.

@samdark samdark removed this from the 2.0.x milestone Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:enhancement Enhancement
Projects
None yet
4 participants