From f2c4ffba3cc3f6dbc2b9fc4b33a9852d9293b7cc Mon Sep 17 00:00:00 2001 From: Andy Babic Date: Tue, 9 Jul 2019 11:22:17 +0100 Subject: [PATCH] Override label_from_instance for PageChoiceField --- wagtailmenus/api/form_fields.py | 10 ++++++++++ wagtailmenus/api/v1/forms.py | 1 + 2 files changed, 11 insertions(+) diff --git a/wagtailmenus/api/form_fields.py b/wagtailmenus/api/form_fields.py index 2b64526a5..8f97508c9 100644 --- a/wagtailmenus/api/form_fields.py +++ b/wagtailmenus/api/form_fields.py @@ -100,8 +100,18 @@ class PageChoiceField(forms.ModelChoiceField): def __init__(self, *args, **kwargs): if 'queryset' not in 'kwargs': kwargs['queryset'] = Page.objects.none() + self.indent_choice_labels = kwargs.pop('indent_choice_labels', True) super().__init__(*args, **kwargs) + def label_from_instance(self, obj): + if self.indent_choice_labels: + if obj.depth > 1: + indent = ''.join(' - ' for i in range(obj.depth-2)) + else: + indent = '' + return '{indent}{page}'.format(indent=indent, page=obj) + return str(obj) + class SiteChoiceField(forms.ModelChoiceField): diff --git a/wagtailmenus/api/v1/forms.py b/wagtailmenus/api/v1/forms.py index c41bef352..78797afb6 100644 --- a/wagtailmenus/api/v1/forms.py +++ b/wagtailmenus/api/v1/forms.py @@ -259,6 +259,7 @@ class SectionMenuGeneratorArgumentForm(BaseMenuGeneratorArgumentForm): section_root_page = api_form_fields.PageChoiceField( label=_("Section root page"), required=False, + indent_choice_labels=False, help_text=_( "The root page for the 'section', whose children and other " "decendents you want show menu items for. If not supplied, the "