Add a construct_page_listing_buttons hook

This hook mimics the functiolity provided by `construct_page_action_menu`
in that it constructs the final list of buttons to be shown in the wagtail
admin interface.  This means that within this function button's can be
removed, added or re-ordered.

See #4925
This commit is contained in:
Michael van Tellingen 2019-07-08 09:57:30 +02:00 committed by LB Johnston
parent f207b1c11f
commit 9cd2fc2c82
6 changed files with 46 additions and 2 deletions

View file

@ -4,7 +4,7 @@ Changelog
2.7 LTS (xx.xx.xxxx) - IN DEVELOPMENT
~~~~~~~~~~~~~~~~~~~~
* ...
* Added `construct_page_listing_buttons` hook (Michael van Tellingen)
2.6 (xx.xx.xxxx) - IN DEVELOPMENT

View file

@ -572,6 +572,23 @@ Hooks for customising the way users are directed through the process of creating
def remove_submit_to_moderator_option(menu_items, request, context):
menu_items[:] = [item for item in menu_items if item.name != 'action-submit']
.. construct_page_listing_buttons:
``construct_page_listing_buttons``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Modify the final list of page listing buttons in the page explorer. The
callable passed to this hook receives a list of ``Button`` objects, a request
object and a context dictionary as per ``register_page_action_menu_item``,
and should modify the list of menu items in-place.
.. code-block:: python
@hooks.register('construct_page_listing_buttons')
def remove_page_listing_button_item(buttons, page, page_perms, is_parent=False, context=None):
if is_parent:
buttons.pop() # removes the last 'more' dropdown button on the parent page listing buttons
.. _construct_wagtail_userbar:

View file

@ -17,7 +17,7 @@ What's new
Other features
~~~~~~~~~~~~~~
* ...
* Added ``construct_page_listing_buttons`` hook (Michael van Tellingen)
Bug fixes

View file

@ -426,6 +426,10 @@ def page_listing_buttons(context, page, page_perms, is_parent=False):
buttons = sorted(itertools.chain.from_iterable(
hook(page, page_perms, is_parent)
for hook in button_hooks))
for hook in hooks.get_hooks('construct_page_listing_buttons'):
hook(buttons, page, page_perms, is_parent, context)
return {'page': page, 'buttons': buttons}

View file

@ -190,6 +190,18 @@ class TestPageExplorer(TestCase, WagtailTestUtils):
page_ids = [page.id for page in response.context['pages']]
self.assertEqual(page_ids, [self.child_page.id])
def test_construct_construct_page_listing_buttons_hook(self):
# testapp implements a construct_page_listing_buttons hook
# that add's an dummy button with the label 'Dummy Button' which points
# to '/dummy-button'
response = self.client.get(
reverse('wagtailadmin_explore', args=(self.root_page.id, )),
)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtailadmin/pages/index.html')
self.assertContains(response, 'Dummy Button')
self.assertContains(response, '/dummy-button')
def make_pages(self):
for i in range(150):
self.root_page.add_child(instance=SimplePage(

View file

@ -8,6 +8,7 @@ from wagtail.admin.menu import MenuItem
from wagtail.admin.rich_text import HalloPlugin
from wagtail.admin.rich_text.converters.html_to_contentstate import BlockElementHandler
from wagtail.admin.search import SearchArea
from wagtail.admin.widgets import Button
from wagtail.core import hooks
@ -149,3 +150,13 @@ def register_relax_menu_item(menu_items, request, context):
raise AttributeError('all core sub-classes of ActionMenuItems must have a name attribute', names)
menu_items.append(RelaxMenuItem())
@hooks.register('construct_page_listing_buttons')
def register_page_listing_button_item(buttons, page, page_perms, is_parent=False, context=None):
item = Button(
label="Dummy Button",
url='/dummy-button',
priority=10,
)
buttons.append(item)