dynamicweb/ungleich_page/cms_menus.py
2026-10-02 11:54:47 +01:00

99 lines
No EOL
2.7 KiB
Python

from menus.base import NavigationNode
from menus.menu_pool import menu_pool
from django.utils.translation import gettext_lazy as _
from cms.menu_bases import CMSAttachMenu
from cms.utils.plugins import get_plugins
class GlasfaserMenu(CMSAttachMenu):
name = _("Glasfaser menu")
def get_nodes(self, request):
nodes = []
glasfaser_cms = 'ungleich_page/glasfaser_cms_page.html'
if (
request
and request.current_page
and request.current_page.get_template() == glasfaser_cms
):
placeholder_name_list = [
'Top Section',
'Middle Section',
'Glasfaser Services',
'Glasfaser About',
'Contact Section',
]
plugins_list = [
'SectionWithImage',
'UngelichContactUsSection',
'UngelichTextSection',
'Service',
'About',
]
# Get the PageContent for the current language.
page_content = request.current_page.get_content_obj(
request.current_page.get_language()
)
if not page_content:
return nodes
for placeholder_name in placeholder_name_list:
placeholder = page_content.get_placeholders().filter(
slot=placeholder_name
).first()
if not placeholder:
continue
plugins = get_plugins(
request,
placeholder,
request.current_page.get_template(),
)
for plugin in plugins:
if type(plugin).__name__ not in plugins_list:
continue
section_hash = request.build_absolute_uri()
if not hasattr(plugin, 'menu_text'):
continue
menu_text = plugin.menu_text
if not menu_text or menu_text.strip() == '':
continue
menu_words = menu_text.split()
if menu_words:
section_hash = '{}#{}'.format(
section_hash,
menu_words[0],
)
newnode = NavigationNode(
menu_text,
url=section_hash,
id="{}-{}".format(
request.current_page.id,
plugin.id,
),
)
nodes.append(newnode)
return nodes
menu_pool.register_menu(GlasfaserMenu)