From e97aa20f2776f2c34a9373ebdb1f05447aa7ce53 Mon Sep 17 00:00:00 2001 From: Wyatt Pearsall Date: Thu, 10 Oct 2024 23:07:34 -0700 Subject: [PATCH 1/3] Add wagtail hook to create menu items for each other translation of the given page --- cfgov/v1/wagtail_hooks.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cfgov/v1/wagtail_hooks.py b/cfgov/v1/wagtail_hooks.py index f7899bf6959..3eee0926c93 100644 --- a/cfgov/v1/wagtail_hooks.py +++ b/cfgov/v1/wagtail_hooks.py @@ -8,6 +8,7 @@ from wagtail import hooks from wagtail.admin import messages +from wagtail.admin.action_menu import ActionMenuItem from wagtail.admin.menu import MenuItem from wagtail.snippets.models import register_snippet @@ -44,6 +45,34 @@ logger = logging.getLogger(__name__) +languages = dict(settings.LANGUAGES) + + +class LanguageMenuItem(ActionMenuItem): + icon_name = "globe" + + def __init__(self, label, url): + self.label = label + self.url = url + + def get_url(self, context): + return self.url + + +@hooks.register("construct_page_action_menu") +def add_language_links(menu_items, request, context): + page = context["page"] + return menu_items.extend( + [ + LanguageMenuItem( + f"Edit {languages[translation.language]} page", + f"/admin/pages/{translation.pk}/edit/", + ) + for translation in page.get_translations() + if translation.language != page.language + ] + ) + @hooks.register("after_delete_page") def log_page_deletion(request, page): From c6de8b9cc51e2d979b5331995e3aa01c5f0c7e3e Mon Sep 17 00:00:00 2001 From: Wyatt Pearsall Date: Fri, 11 Oct 2024 09:04:07 -0700 Subject: [PATCH 2/3] Ignore key errors when page isn't in context --- cfgov/v1/wagtail_hooks.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/cfgov/v1/wagtail_hooks.py b/cfgov/v1/wagtail_hooks.py index 3eee0926c93..ef43943f133 100644 --- a/cfgov/v1/wagtail_hooks.py +++ b/cfgov/v1/wagtail_hooks.py @@ -61,17 +61,20 @@ def get_url(self, context): @hooks.register("construct_page_action_menu") def add_language_links(menu_items, request, context): - page = context["page"] - return menu_items.extend( - [ - LanguageMenuItem( - f"Edit {languages[translation.language]} page", - f"/admin/pages/{translation.pk}/edit/", - ) - for translation in page.get_translations() - if translation.language != page.language - ] - ) + try: + page = context["page"] + return menu_items.extend( + [ + LanguageMenuItem( + f"Edit {languages[translation.language]} page", + f"/admin/pages/{translation.pk}/edit/", + ) + for translation in page.get_translations() + if translation.language != page.language + ] + ) + except KeyError: + pass @hooks.register("after_delete_page") From 0409d0e92ef83116d08563a32ec937a90b1418d2 Mon Sep 17 00:00:00 2001 From: Wyatt Pearsall Date: Fri, 18 Oct 2024 10:39:58 -0700 Subject: [PATCH 3/3] Add language links to three dots menu in header --- cfgov/v1/wagtail_hooks.py | 40 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/cfgov/v1/wagtail_hooks.py b/cfgov/v1/wagtail_hooks.py index ef43943f133..72a1e183a53 100644 --- a/cfgov/v1/wagtail_hooks.py +++ b/cfgov/v1/wagtail_hooks.py @@ -7,8 +7,7 @@ from django.utils.html import format_html_join from wagtail import hooks -from wagtail.admin import messages -from wagtail.admin.action_menu import ActionMenuItem +from wagtail.admin import messages, widgets from wagtail.admin.menu import MenuItem from wagtail.snippets.models import register_snippet @@ -48,33 +47,18 @@ languages = dict(settings.LANGUAGES) -class LanguageMenuItem(ActionMenuItem): - icon_name = "globe" - - def __init__(self, label, url): - self.label = label - self.url = url - - def get_url(self, context): - return self.url - - -@hooks.register("construct_page_action_menu") -def add_language_links(menu_items, request, context): - try: - page = context["page"] - return menu_items.extend( - [ - LanguageMenuItem( - f"Edit {languages[translation.language]} page", - f"/admin/pages/{translation.pk}/edit/", - ) - for translation in page.get_translations() - if translation.language != page.language - ] +@hooks.register("register_page_header_buttons") +def page_header_buttons(page, user, view_name, next_url=None): + return [ + widgets.Button( + f"Edit {languages[translation.language]} page", + f"/admin/pages/{translation.pk}/edit/", + priority=1000, + icon_name="globe", ) - except KeyError: - pass + for translation in page.get_translations() + if translation.language != page.language + ] @hooks.register("after_delete_page")