Added "dynamic_menu" attribute to menu to be able to build menu dynamically from target contents
authorThierry Florac <thierry.florac@onf.fr>
Mon, 26 Nov 2018 09:32:56 +0100
changeset 1117 60f1d91b671c
parent 1116 f286ddfd87ec
child 1118 ed551ca7c4de
Added "dynamic_menu" attribute to menu to be able to build menu dynamically from target contents
src/pyams_content/features/menu/__init__.py
src/pyams_content/features/menu/interfaces.py
src/pyams_content/features/menu/zmi/__init__.py
--- a/src/pyams_content/features/menu/__init__.py	Mon Nov 26 09:32:10 2018 +0100
+++ b/src/pyams_content/features/menu/__init__.py	Mon Nov 26 09:32:56 2018 +0100
@@ -17,9 +17,10 @@
 
 from pyams_content.component.association.container import AssociationContainer
 from pyams_content.component.association.interfaces import IAssociationInfo
-from pyams_content.component.links import InternalReferenceMixin, InternalLinkAssociationInfoAdapter
+from pyams_content.component.links import InternalLinkAssociationInfoAdapter, InternalReferenceMixin, get_dynamic_link
 from pyams_content.features.menu.interfaces import IMenu, IMenuLink, IMenusContainer
 from pyams_content.reference.pictograms import IPictogramTable
+from pyams_content.shared.site.interfaces import ISiteContainer
 from pyams_utils.adapter import adapter_config
 from pyams_utils.registry import query_utility
 from pyams_utils.request import check_request
@@ -40,6 +41,7 @@
     visible = FieldProperty(IMenu['visible'])
     title = FieldProperty(IMenu['title'])
     reference = FieldProperty(IMenu['reference'])
+    dynamic_menu = FieldProperty(IMenu['dynamic_menu'])
     _pictogram_name = FieldProperty(IMenu['pictogram_name'])
 
     @property
@@ -67,6 +69,12 @@
             if publication_info is not None:
                 return publication_info.is_visible(request)
 
+    def get_visible_items(self, request=None):
+        if self.dynamic_menu and ISiteContainer.providedBy(self.target):
+            yield from map(get_dynamic_link,
+                           self.target.get_visible_items(request))
+        yield from super(Menu, self).get_visible_items(request)
+
     def get_url(self, request=None, view_name=None):
         target = self.get_target()
         if target is not None:
--- a/src/pyams_content/features/menu/interfaces.py	Mon Nov 26 09:32:10 2018 +0100
+++ b/src/pyams_content/features/menu/interfaces.py	Mon Nov 26 09:32:56 2018 +0100
@@ -68,6 +68,12 @@
                                        description=_("Direct reference to menu target"),
                                        required=False)
 
+    dynamic_menu = Bool(title=_("Dynamic menu?"),
+                        description=_("If 'yes', menu items will be built from internal reference navigation items; other "
+                                      "static items will be placed after dynamic items"),
+                        required=False,
+                        default=False)
+
     pictogram_name = Choice(title=_("Pictogram"),
                             description=_("Name of the pictogram associated with this menu"),
                             required=False,
--- a/src/pyams_content/features/menu/zmi/__init__.py	Mon Nov 26 09:32:10 2018 +0100
+++ b/src/pyams_content/features/menu/zmi/__init__.py	Mon Nov 26 09:32:56 2018 +0100
@@ -91,7 +91,7 @@
     legend = _("Add new menu")
     icon_css_class = 'fa fa-fw fa-bars'
 
-    fields = field.Fields(IMenu).select('title', 'reference', 'pictogram_name')
+    fields = field.Fields(IMenu).select('title', 'reference', 'dynamic_menu', 'pictogram_name')
     fields['pictogram_name'].widgetFactory = PictogramSelectFieldWidget
 
     edit_permission = MANAGE_TEMPLATE_PERMISSION
@@ -126,7 +126,7 @@
 
     dialog_class = 'modal-large'
 
-    fields = field.Fields(IMenu).select('title', 'reference', 'pictogram_name')
+    fields = field.Fields(IMenu).select('title', 'reference', 'dynamic_menu', 'pictogram_name')
     fields['pictogram_name'].widgetFactory = PictogramSelectFieldWidget
 
     edit_permission = MANAGE_TEMPLATE_PERMISSION