diff --git a/CHANGELOG.txt b/CHANGELOG.txt index cca665b4f3..c9da5a5828 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -72,6 +72,7 @@ Changelog * Introduce a new auto-updating preview panel inside the page editor (Sage Abdullah) * Add support for Twitter and other text-only embeds in Draftail embed previews (Iman Syed, Paarth Agarwal) * Use new modal dialog component for privacy settings modal (Sage Abdullah) + * Add `menu_item_name` to modify MenuItem's name for ModelAdmin (Alexander Rogovskyy, Vu Pham) * Fix: Typo in `ResumeWorkflowActionFormatter` message (Stefan Hammer) * Fix: Throw a meaningful error when saving an image to an unrecognised image format (Christian Franke) * Fix: Remove extra padding for headers with breadcrumbs on mobile viewport (Steven Steinwand) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index e3a8cf5c1b..8c9fd1009d 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -612,6 +612,8 @@ Contributors * Sandeep M A * Bernd de Ridder * Stefano Silvestri +* Alexander Rogovskyy + Translators =========== diff --git a/docs/reference/contrib/modeladmin/menu_item.md b/docs/reference/contrib/modeladmin/menu_item.md index 1f20bec56e..4159dd2a01 100644 --- a/docs/reference/contrib/modeladmin/menu_item.md +++ b/docs/reference/contrib/modeladmin/menu_item.md @@ -52,3 +52,11 @@ This will only work for individual `ModelAdmin` classes registered with their ow **Expected value**: `True` or `False` If you'd like this model admin to be excluded from the menu, set to `False`. + +(modeladmin_menu_item_name)= + +## `ModelAdmin.menu_item_name` + +**Expected value**: A string or `None` + +Passed on as the `name` parameter when initialising the `MenuItem` for this class, becoming the `name` attribute value for that instance. diff --git a/docs/releases/4.0.md b/docs/releases/4.0.md index 63ba0fc451..894df5a3e7 100644 --- a/docs/releases/4.0.md +++ b/docs/releases/4.0.md @@ -92,6 +92,7 @@ Wagtail’s page preview is now available in a side panel within the page editor * Switch all report workflow, redirects, site settings views to use Wagtail’s reusable header component (Paarth Agarwal) * Add support for Twitter and other text-only embeds in Draftail embed previews (Iman Syed, Paarth Agarwal) * Use new modal dialog component for privacy settings modal (Sage Abdullah) + * Add `menu_item_name` to modify MenuItem's name for ModelAdmin (Alexander Rogovskyy, Vu Pham) ### Bug fixes diff --git a/wagtail/contrib/modeladmin/menus.py b/wagtail/contrib/modeladmin/menus.py index 54bff6b01d..b5f0385067 100644 --- a/wagtail/contrib/modeladmin/menus.py +++ b/wagtail/contrib/modeladmin/menus.py @@ -23,6 +23,7 @@ class ModelAdminMenuItem(MenuItem): super().__init__( label=model_admin.get_menu_label(), url=url, + name=model_admin.get_menu_item_name(), classnames=classnames, icon_name=icon_name, order=order, @@ -50,6 +51,7 @@ class GroupMenuItem(SubmenuMenuItem): super().__init__( label=modeladmingroup.get_menu_label(), menu=menu, + name=modeladmingroup.get_menu_item_name(), classnames=classnames, icon_name=icon_name, order=order, diff --git a/wagtail/contrib/modeladmin/options.py b/wagtail/contrib/modeladmin/options.py index 23b2c24929..687a63adaa 100644 --- a/wagtail/contrib/modeladmin/options.py +++ b/wagtail/contrib/modeladmin/options.py @@ -104,6 +104,7 @@ class ModelAdmin(WagtailRegisterable): model = None menu_label = None + menu_item_name = None menu_icon = None menu_order = None list_display = ("__str__",) @@ -216,6 +217,12 @@ class ModelAdmin(WagtailRegisterable): """ return self.menu_label or self.opts.verbose_name_plural.title() + def get_menu_item_name(self): + """ + Returns the name to be used for the menu item. + """ + return self.menu_item_name + def get_menu_icon(self): """ Returns the icon to be used for the menu item. The value is prepended @@ -699,6 +706,7 @@ class ModelAdminGroup(WagtailRegisterable): items = () menu_label = None + menu_item_name = None menu_order = None menu_icon = None @@ -715,6 +723,9 @@ class ModelAdminGroup(WagtailRegisterable): def get_menu_label(self): return self.menu_label or self.get_app_label_from_subitems() + def get_menu_item_name(self): + return self.menu_item_name + def get_app_label_from_subitems(self): for instance in self.modeladmin_instances: return instance.opts.app_label.title() diff --git a/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py b/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py index 65a78149ae..f97da58cec 100644 --- a/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py +++ b/wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py @@ -26,7 +26,7 @@ from wagtail.test.modeladmintest.models import ( Token, TranslatableBook, ) -from wagtail.test.modeladmintest.wagtail_hooks import BookModelAdmin +from wagtail.test.modeladmintest.wagtail_hooks import BookModelAdmin, EventsAdminGroup from wagtail.test.utils import WagtailTestUtils @@ -1112,3 +1112,42 @@ There are no default tabs on non-Page models so there will be no\ # clean up for future checks delattr(Publisher, "content_panels") delattr(Publisher, "edit_handler") + + +class TestMenuSetting(TestCase, WagtailTestUtils): + fixtures = ["modeladmintest_test.json"] + + def setUp(self): + self.login() + + def test_default_menu_setting_model_admin(self): + modeladmin = BookModelAdmin() + + menu_item = modeladmin.get_menu_item() + self.assertEqual(menu_item.label, "Books") + self.assertEqual(menu_item.name, "books") + + def test_custom_menu_setting_model_admin(self): + modeladmin = BookModelAdmin() + modeladmin.menu_label = "Book Model Label" + modeladmin.menu_item_name = "bookitem" + + menu_item = modeladmin.get_menu_item() + self.assertEqual(menu_item.label, "Book Model Label") + self.assertEqual(menu_item.name, "bookitem") + + def test_default_menu_setting_model_admin_group(self): + modeladmin = EventsAdminGroup() + + menu_item = modeladmin.get_menu_item() + self.assertEqual(menu_item.label, "Events") + self.assertEqual(menu_item.name, "events") + + def test_custom_menu_setting_model_admin_group(self): + modeladmin = EventsAdminGroup() + modeladmin.menu_label = "Event Model Label" + modeladmin.menu_item_name = "eventitem" + + menu_item = modeladmin.get_menu_item() + self.assertEqual(menu_item.label, "Event Model Label") + self.assertEqual(menu_item.name, "eventitem")