Reputation: 2594
How to hide-show menu in wagtail CMS:
Here is my code on register_admin_menu_item hook inside blog/wagtail_hooks.py
from wagtail.core import hooks
from wagtail.admin.menu import MenuItem
@hooks.register('register_admin_menu_item')
def register_custom_admin_menu_item():
return MenuItem(_('Site Admin'), reverse('admin_menu'), classnames='icon icon-folder-inverse',
order=20000)
Upvotes: 3
Views: 1057
Reputation: 2594
For imposing the access on the menu, we can create the custom MenuItem
Class and override the is_shown
method as below:
class CustomAdminMenuItem(MenuItem):
def is_shown(self, request):
return request.user.is_staff
Now use this CustomAdminMenuItem
instead of MenuItem like:
from wagtail.core import hooks
from wagtail.admin.menu import MenuItem
@hooks.register('register_admin_menu_item')
def register_custom_admin_menu_item():
return CustomAdminMenuItem(_('Site Admin'), reverse('admin_menu'), classnames='icon icon-folder-inverse',
order=20000)
You can implement custom permission check also using has_perm
inside is_shown like:
class CustomMenuItem(MenuItem):
def is_shown(self, request):
return (
request.user.has_perm('wagtailsearchpromotions.add_searchpromotion') or
request.user.has_perm('wagtailsearchpromotions.change_searchpromotion') or
request.user.has_perm('wagtailsearchpromotions.delete_searchpromotion')
)
For more details visit the source code here and doc here.
Upvotes: 4