diff --git a/wagtail/admin/templatetags/wagtailadmin_tags.py b/wagtail/admin/templatetags/wagtailadmin_tags.py index fd2a8607c61b..5afa9f7a992f 100644 --- a/wagtail/admin/templatetags/wagtailadmin_tags.py +++ b/wagtail/admin/templatetags/wagtailadmin_tags.py @@ -40,6 +40,7 @@ get_valid_next_url_from_request, ) from wagtail.admin.views.bulk_action.registry import bulk_action_registry +from wagtail.admin.views.pages.utils import get_breadcrumbs_items_for_page from wagtail.admin.widgets import Button, ButtonWithDropdown, PageListingButton from wagtail.coreutils import ( accepts_kwarg, @@ -55,7 +56,6 @@ Page, PageViewRestriction, ) -from wagtail.permissions import page_permission_policy from wagtail.telepath import JSContext from wagtail.users.utils import get_gravatar_url from wagtail.utils.deprecation import RemovedInWagtail70Warning @@ -91,26 +91,15 @@ def page_breadcrumbs( ): user = context["request"].user - # find the closest common ancestor of the pages that this user has direct explore permission - # (i.e. add/edit/publish/lock) over; this will be the root of the breadcrumb - cca = page_permission_policy.explorable_root_instance(user) - if not cca: - return {"items": Page.objects.none()} - - pages = ( - page.get_ancestors(inclusive=include_self) - .descendant_of(cca, inclusive=True) - .specific() + items = get_breadcrumbs_items_for_page( + page, + user, + url_name, + url_root_name, + include_self, + querystring_value, ) - items = [] - for page in pages: - if page.is_root() and url_root_name: - url = reverse(url_root_name) - else: - url = reverse(url_name, args=(page.id,)) - items.append({"url": url + querystring_value, "label": get_latest_str(page)}) - if trailing_breadcrumb_title: items.append({"label": trailing_breadcrumb_title}) diff --git a/wagtail/admin/views/pages/utils.py b/wagtail/admin/views/pages/utils.py index 4dad1af28d7b..6f1dbad514ff 100644 --- a/wagtail/admin/views/pages/utils.py +++ b/wagtail/admin/views/pages/utils.py @@ -1,2 +1,39 @@ +from django.urls import reverse + # Retain backwards compatibility for imports -from wagtail.admin.utils import get_valid_next_url_from_request # noqa: F401 +from wagtail.admin.utils import ( # noqa: F401 + get_latest_str, + get_valid_next_url_from_request, +) +from wagtail.permissions import page_permission_policy + + +def get_breadcrumbs_items_for_page( + page, + user, + url_name="wagtailadmin_explore", + root_url_name="wagtailadmin_explore_root", + include_self=True, + querystring_value="", +): + # find the closest common ancestor of the pages that this user has direct explore permission + # (i.e. add/edit/publish/lock) over; this will be the root of the breadcrumb + cca = page_permission_policy.explorable_root_instance(user) + if not cca: + return [] + + pages = ( + page.get_ancestors(inclusive=include_self) + .descendant_of(cca, inclusive=True) + .specific() + ) + + items = [] + for page in pages: + if page.is_root() and root_url_name: + url = reverse(root_url_name) + else: + url = reverse(url_name, args=(page.id,)) + items.append({"url": url + querystring_value, "label": get_latest_str(page)}) + + return items