- {% minimum_collection_depth collections as min_depth %}
- {% for collection in collections %}
+ {% for pk, display_name in collections.get_indented_choices %}
diff --git a/wagtail/admin/templatetags/wagtailadmin_tags.py b/wagtail/admin/templatetags/wagtailadmin_tags.py
index d7667b2065..2875b06aca 100644
--- a/wagtail/admin/templatetags/wagtailadmin_tags.py
+++ b/wagtail/admin/templatetags/wagtailadmin_tags.py
@@ -7,7 +7,6 @@ from django.conf import settings
from django.contrib.admin.utils import quote
from django.contrib.humanize.templatetags.humanize import intcomma, naturaltime
from django.contrib.messages.constants import DEFAULT_TAGS as MESSAGE_TAGS
-from django.db.models import Min, QuerySet
from django.shortcuts import resolve_url as resolve_url_func
from django.template.defaultfilters import stringfilter
from django.templatetags.static import static
@@ -40,7 +39,6 @@ from wagtail.coreutils import (
get_locales_display_names,
)
from wagtail.models import (
- Collection,
CollectionViewRestriction,
Locale,
Page,
@@ -729,30 +727,6 @@ def timesince_last_update(
return _("%(time_period)s ago") % {"time_period": timesince(last_update)}
-@register.simple_tag
-def format_collection(coll: Collection, min_depth: int = 2) -> str:
- """
- Renders a given Collection's name as a formatted string that displays its
- hierarchical depth via indentation. If min_depth is supplied, the
- Collection's depth is rendered relative to that depth. min_depth defaults
- to 2, the depth of the first non-Root Collection.
-
- Example usage: {% format_collection collection min_depth %}
- Example output: " ↳ Child Collection"
- """
- return coll.get_indented_name(min_depth, html=True)
-
-
-@register.simple_tag
-def minimum_collection_depth(collections: QuerySet) -> int:
- """
- Returns the minimum depth of the Collections in the given queryset.
- Call this before beginning a loop through Collections that will
- use {% format_collection collection min_depth %}.
- """
- return collections.aggregate(Min("depth"))["depth__min"] or 2
-
-
@register.filter
def user_display_name(user):
"""
diff --git a/wagtail/documents/templates/wagtaildocs/multiple/add.html b/wagtail/documents/templates/wagtaildocs/multiple/add.html
index 0c3de87e11..8c1e4d23ab 100644
--- a/wagtail/documents/templates/wagtaildocs/multiple/add.html
+++ b/wagtail/documents/templates/wagtaildocs/multiple/add.html
@@ -29,16 +29,9 @@
diff --git a/wagtail/models/collections.py b/wagtail/models/collections.py
index 138e10160d..0aa1c4af35 100644
--- a/wagtail/models/collections.py
+++ b/wagtail/models/collections.py
@@ -11,12 +11,26 @@ from wagtail.search import index
from .view_restrictions import BaseViewRestriction
+class CollectionQuerySet(TreeQuerySet):
+ def get_indented_choices(self):
+ """
+ Return a list of (id, label) tuples for use as a list of choices in a collection chooser
+ dropdown, where the label is formatted with get_indented_name to provide a tree layout.
+ The indent level is chosen to place the minimum-depth collection at indent 0.
+ """
+ min_depth = self.aggregate(models.Min("depth"))["depth__min"] or 2
+ return [
+ (collection.pk, collection.get_indented_name(min_depth, html=True))
+ for collection in self
+ ]
+
+
class BaseCollectionManager(models.Manager):
def get_queryset(self):
- return TreeQuerySet(self.model).order_by("path")
+ return CollectionQuerySet(self.model).order_by("path")
-CollectionManager = BaseCollectionManager.from_queryset(TreeQuerySet)
+CollectionManager = BaseCollectionManager.from_queryset(CollectionQuerySet)
class CollectionViewRestriction(BaseViewRestriction):