Jump to content

How to hide menu item in the backend?


dotnetic
 Share

Recommended Posts

I try to hide menu items in the Admin/Backend. Any suggestions, how to do this?

The way I do it right now is this:

 public function init()
    {
        $this->addHookAfter('Page::viewable', $this, 'viewable');
    }

      /**
     * Don't give users with custom-role access to Pages page
     */
    public function viewable(HookEvent $event){
        $page = $event->object;
        $user = $this->user;
        if($page->id == 3 && !$user->hasPermission('view-pagetree')){
            $event->return = false;
        }
        if ($page->name == "admin-actions" && !$user->hasPermission('admin-actions')) {
            $event->return = false;
        }

    }

But there are two problems with this approach: Because I return viewable = false for the "Page" menu my Lister Pro item also does not show up, because it resides in "Page".

The second problem is, that viewable does revoke the right to view a page. I need for example the module "Admin Actions" to be viewable but the menuitem should be hidden.

How can I accomplish this?

Link to comment
Share on other sites

1 minute ago, jmartsch said:

@adrian Thank you, I was completely unaware of this option. But it does not solve my first problem with hiding the "pages" menuitem, but showing the Lister Pro menuitem.

If I understand correctly you want to hide a parent menu item (Pages) but display one of its children (A Lister Pro page)?

The only solution would be to move the Lister Pro page directly under the "Admin" parent in the page tree. Then you can go about hiding "Pages" without any problems.

Link to comment
Share on other sites

You understood it correctly. But how can I move the Lister Pro page? It is not a real page, but a saved Lister.

The other problem with this approach is, that if I hide the pages in the Admin > Setup, they are not visible for superusers.

But superusers should still see them.

Link to comment
Share on other sites

9 minutes ago, jmartsch said:

But how can I move the Lister Pro page? It is not a real page, but a saved Lister.

Listers are real pages - take a look - see the "Test Lister" at the bottom? Just move to directly under "Admin"

58d0418de0051_ScreenShot2017-03-20at1_54_11PM.png.57c3b7b1119e766479edd02f05d09e95.png

9 minutes ago, jmartsch said:

The other problem with this approach is, that if I hide the pages in the Admin > Setup, they are not visible for superusers.

Ok, so I guess it's back to the drawing board :)

Let me take a look at your hook code and see if I can get it working for you.

  • Like 1
Link to comment
Share on other sites

@jmartsch - I wasn't finding an easy solution to your menu problem (maybe just not thinking straight), so I added a new option to the AdminActions module. If you create a new permission: "admin-actions-hide-menu" and give that to those roles that you want to allow actions, but not via the Setup > AdminActions menu. 

Remember the user will need to logout and back in again to clear the cache of the menu.

Let me know if it works ok.

 

Link to comment
Share on other sites

@jmartsch - I have a mostly working hack for you:

    $this->wire()->addHookAfter('Page::viewable', null, function (HookEvent $event) {
        if(!$event->return) return;
        $page = $event->object;

        if (strpos($_SERVER['REQUEST_URI'], "admin-actions") === false && $page->name == "admin-actions") {
            $event->return = false;
        }
    });

With this, non-superusers won't see the Setup > Admin Actions menu item unless they are already on an Admin Actions page, eg:

http://mysite.dev/processwire/setup/admin-actions/options?action=CopyContentToOtherField

I was trying to limit the return false to a specific process, but couldn't make it work as expected.

Honestly, I feel like the right way to do this would be to ask Ryan to make: AdminThemeDefaultHelpers::renderTopNavItems (https://github.com/processwire/processwire/blob/35df716082b779de0e53a3fcf7996403c49c9f8a/wire/modules/AdminTheme/AdminThemeDefault/AdminThemeDefaultHelpers.php#L416) hookable, but without that, I think this is the best option, but maybe someone else has a better idea.

PS - If you are running the Reno theme, it turns out that AdminThemeRenoHelpers::topNavItems is hookable. Might be a good argument for making it hookable in the Default and new UiKit themes as well. It think that approach would make this much easier.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...