Jean Zombie
Jean Zombie

Reputation: 627

Wagtail: How to remove summary items on the admin homepage

I am trying to remove/customize the summary items on the admin homepage with the recommended hook construct_homepage_summary_items.

Add or remove items from the ‘site summary’ bar on the admin homepage (which shows the number of pages and other object that exist on the site).

I manage to add my own items but struggle to remove all of the default items.

@hooks.register("construct_homepage_summary_items")
def add_my_summary_items(request, items):
    items.append(MySummaryItem(request))
    items.pop(0)

items only includes my custom items and wagtail.admin.site_summary.PagesSummaryItem but not those for images and documents. Removing items with pop doesn't seem to be as elegant as it could be either. I hoped for some way along the lines of the construct_main_menu hook:

menu_items[:] = [item for item in menu_items if item.name != 'explorer']

But I could not find a name identifier or an equivalent. How would I go on about it?

Upvotes: 0

Views: 684

Answers (1)

allcaps
allcaps

Reputation: 11238

Does this work? It should remove all summary items.

@hooks.register("construct_homepage_summary_items")
def remove_all_summary_items(request, items):
    items.clear()

Update

If you need your hooks to run in a particular order, you can pass the order parameter.

@hooks.register('name_of_hook', order=1)  # This will run after every hook in the wagtail core
def my_hook_function(arg1, arg2...)
    # your code here

It seems that page item hook is executed before your hook and image and document items after. This surprises me as it would be logical that all Wagtail hooks are ran at the same time: before any custom code.

The order of apps has influence on the order of execution too. Alternatively to the order param you can create a new app (named hooks?) and place it after Wagtail apps in INSTALLED_APPS. It only needs an __init__.py and wagtail_hooks.py file. The hooks in this app will execute after Wagtail hooks. Page, doc and image items are in items and can be removed.

from wagtail.core import hooks

@hooks.register("construct_homepage_summary_items")
def remove_all_summary_items(request, items):
    items.clear()
    items.append(YourSummaryItem)

Upvotes: 1

Related Questions