From dc93d287845f9460400598c05b73a5c0e5da6f83 Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Fri, 8 Mar 2024 16:32:12 +0000 Subject: [PATCH] Render edit URL in page usage view as a header button --- wagtail/admin/tests/pages/test_page_usage.py | 8 ++++++++ wagtail/admin/views/pages/usage.py | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/wagtail/admin/tests/pages/test_page_usage.py b/wagtail/admin/tests/pages/test_page_usage.py index 3d85229ae720..954751c7400f 100644 --- a/wagtail/admin/tests/pages/test_page_usage.py +++ b/wagtail/admin/tests/pages/test_page_usage.py @@ -58,6 +58,14 @@ def test_simple(self): ] self.assertBreadcrumbsItemsRendered(items, response.content) + # There should be exactly one edit link, rendered as a header button + edit_url = reverse("wagtailadmin_pages:edit", args=(self.page.id,)) + soup = self.get_soup(response.content) + edit_links = soup.select(f"a[href='{edit_url}']") + self.assertEqual(len(edit_links), 1) + edit_link = edit_links[0] + self.assertIn("w-header-button", edit_link.attrs.get("class")) + def test_has_private_usage(self): PageChooserModel.objects.create(page=self.page) usage_url = reverse("wagtailadmin_pages:usage", args=(self.page.id,)) diff --git a/wagtail/admin/views/pages/usage.py b/wagtail/admin/views/pages/usage.py index 31094e8bc052..005459c5a42d 100644 --- a/wagtail/admin/views/pages/usage.py +++ b/wagtail/admin/views/pages/usage.py @@ -78,6 +78,7 @@ class UsageView(generic.UsageView): pk_url_kwarg = "page_id" header_icon = "doc-empty-inverse" usage_url_name = "wagtailadmin_pages:usage" + edit_url_name = "wagtailadmin_pages:edit" _show_breadcrumbs = True def dispatch(self, request, *args, **kwargs): @@ -88,3 +89,11 @@ def dispatch(self, request, *args, **kwargs): @cached_property def breadcrumbs_items(self): return get_breadcrumbs_items_for_page(self.object, self.request.user) + + def get_breadcrumbs_items(self): + # The generic UsageView will add an edit link for the current object as + # the second-to-last item, but we don't want that because we want to + # link to the explore view instead for consistency with how page + # breadcrumbs work elsewhere. So we only take the last item, which is + # the "self" (Usage) item. + return self.breadcrumbs_items + [super().get_breadcrumbs_items()[-1]]