Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Wagtail CMS Snippets but Hide in Admin Panel

I am building custom AdminModels based on Wagtail Snippets and have a custom menu in the AdminPanel for my models. How do I hide/remove the Snippet selection from AdminPanel without disabling? Thank you.

enter image description here

like image 740
Charles Smith Avatar asked Feb 16 '17 02:02

Charles Smith


People also ask

What is @Register_snippet in wagtail?

@register_snippet tells Wagtail to treat the model as a snippet. The panels list defines the fields to show on the snippet editing page. It’s also important to provide a string representation of the class through def __str__ (self): so that the snippet objects make sense when listed in the Wagtail admin.

How to customize the Admin panels in a wagtail site?

Today, let's have a look at how we can customize the admin panels in a Wagtail site. To make the fields appear in the admin interface, they need to be added to one of the panels. You can also make the panel collapsed by default by adding the class name collapsed. For a field panel, I also added the classes full and title to the FieldPanel.

How do I format a panel in wagtail?

By default, panels are formatted as inset fields. The CSS class full can be used to format the panel so it covers the full width of the Wagtail page editor. The CSS class title can be used to give the field a larger text size, suitable for representing page titles and section headings.

What is the use of inlinepanel in wagtail?

class wagtail.admin.panels.InlinePanel(relation_name, panels=None, classname='', heading='', label='', help_text='', min_num=None, max_num=None) ¶ This panel allows for the creation of a “cluster” of related objects over a join to a separate model, such as a list of related links or slides to an image carousel.


2 Answers

Since item.name in menu_items can be blank, better solution is:

from wagtail.snippets.wagtail_hooks import SnippetsMenuItem

@hooks.register('construct_main_menu')
def hide_snippets_menu_item(request, menu_items):
    menu_items[:] = [item for item in menu_items if not isinstance(item, SnippetsMenuItem)]
like image 153
diveru4i Avatar answered Oct 23 '22 11:10

diveru4i


Put the following hook into wagtail_hooks.py file of your Wagtail CMS app:

from wagtail.wagtailcore import hooks

@hooks.register('construct_main_menu')
def hide_snippets_menu_item(request, menu_items):
  menu_items[:] = [item for item in menu_items if item.name != 'snippets']

And you're basically done! You can use this approach to hide any item from the admin menu.

I described it recently on my blog: http://timonweb.com/posts/how-to-remove-snippets-menu-item-from-wagtail-cms-admin-menu/

like image 26
timonweb Avatar answered Oct 23 '22 12:10

timonweb