diff --git a/docs/reference/hooks.rst b/docs/reference/hooks.rst index 52421ce35..374ad0723 100644 --- a/docs/reference/hooks.rst +++ b/docs/reference/hooks.rst @@ -566,12 +566,33 @@ Hooks for customising the way users are directed through the process of creating Modify the final list of action menu items on the page creation and edit views. The callable passed to this hook receives a list of ``ActionMenuItem`` 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_action_menu') 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_action_menu: + + Can also be used to customize default action menu button. The last item in menu_item variable is chosen as the default action. An example on changing the default to publish can be seen below. + + .. code-block:: python + + from wagtail.admin.action_menu import UnpublishMenuItem, DeleteMenuItem, SubmitForModerationMenuItem, SaveDraftMenuItem, PublishMenuItem + + @hooks.register('construct_page_action_menu') + def make_publish_default_action(menu_items, request, context): + menu_items[:] = [ + UnpublishMenuItem(order=10), + DeleteMenuItem(order=20), + SubmitForModerationMenuItem(order=30), + SaveDraftMenuItem(order=40), + PublishMenuItem(order=50), + ] + + .. construct_page_listing_buttons: ``construct_page_listing_buttons`` diff --git a/wagtail/admin/action_menu.py b/wagtail/admin/action_menu.py index e59e6d526..0dfae25bf 100644 --- a/wagtail/admin/action_menu.py +++ b/wagtail/admin/action_menu.py @@ -57,6 +57,7 @@ class ActionMenuItem(metaclass=MediaDefiningClass): class PublishMenuItem(ActionMenuItem): + label = "Publish" name = 'action-publish' template = 'wagtailadmin/pages/action_menu/publish.html' @@ -121,6 +122,17 @@ class DeleteMenuItem(ActionMenuItem): return reverse('wagtailadmin_pages:delete', args=(context['page'].id,)) +class SaveDraftMenuItem(ActionMenuItem): + name = 'action-save-draft' + label = _("Save Draft") + template = 'wagtailadmin/pages/action_menu/save_draft.html' + + def get_context(self, request, parent_context): + context = super().get_context(request, parent_context) + context['is_revision'] = (context['view'] == 'revisions_revert') + return context + + BASE_PAGE_ACTION_MENU_ITEMS = None @@ -137,6 +149,7 @@ def _get_base_page_action_menu_items(): DeleteMenuItem(order=20), PublishMenuItem(order=30), SubmitForModerationMenuItem(order=40), + SaveDraftMenuItem(order=50), ] for hook in hooks.get_hooks('register_page_action_menu_item'): BASE_PAGE_ACTION_MENU_ITEMS.append(hook()) @@ -163,13 +176,16 @@ class PageActionMenu: for hook in hooks.get_hooks('construct_page_action_menu'): hook(self.menu_items, self.request, self.context) + self.default_item = self.menu_items.pop() if self.menu_items else SaveDraftMenuItem(order=50) + def render_html(self): return render_to_string(self.template, { + 'default_menu_item': self.default_item.render_html(self.request, self.context), 'show_menu': bool(self.menu_items), 'rendered_menu_items': [ menu_item.render_html(self.request, self.context) for menu_item in self.menu_items - ] + ], }, request=self.request) @cached_property diff --git a/wagtail/admin/templates/wagtailadmin/pages/action_menu/menu.html b/wagtail/admin/templates/wagtailadmin/pages/action_menu/menu.html index ad0ed361f..9347c22f2 100644 --- a/wagtail/admin/templates/wagtailadmin/pages/action_menu/menu.html +++ b/wagtail/admin/templates/wagtailadmin/pages/action_menu/menu.html @@ -1,6 +1,11 @@ {% if show_menu %} + {{ default_menu_item }}