Skip to content

Commit

Permalink
Allow addition of extra body classes via multiple ``IBodyClassAdapter…
Browse files Browse the repository at this point in the history
…`` adapter registrations without the need to overload the ILayoutPolicy view.
  • Loading branch information
thet authored and rodfersou committed Oct 13, 2018
1 parent 6ef07a7 commit e08fae8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Breaking changes:

New features:

- *add item here*
- Allow addition of extra body classes via multiple IBodyClassAdapter adapter registrations without the need to overload the ILayoutPolicy view.
[thet, jensens, agitator]

Bug fixes:

Expand Down
5 changes: 5 additions & 0 deletions plone/app/layout/globals/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ class IViewView(Interface):
"""


class IBodyClassAdapter(Interface):
"""Adapter interface for retrieving extra body classes.
"""


class IPatternsSettingsRenderer(Interface):
""" Interface for the adapter that renders the settings for patterns
"""
30 changes: 30 additions & 0 deletions plone/app/layout/globals/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from Products.CMFPlone.interfaces.controlpanel import ISiteSchema
from Products.Five.browser.metaconfigure import ViewMixinForTemplates
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.globals.interfaces import IBodyClassAdapter
from plone.app.layout.globals.interfaces import ILayoutPolicy
from plone.app.layout.globals.interfaces import IViewView
from plone.app.layout.icons.interfaces import IContentIcon
Expand All @@ -16,12 +17,15 @@
from zope.browserpage.viewpagetemplatefile import (
ViewPageTemplateFile as ZopeViewPageTemplateFile
)
from zope.component import adapter
from zope.component import getAdapters
from zope.component import getMultiAdapter
from zope.component import getUtility
from zope.component import queryMultiAdapter
from zope.component import queryUtility
from zope.interface import alsoProvides
from zope.interface import implements
from zope.interface import Interface
from zope.publisher.browser import BrowserView


Expand Down Expand Up @@ -268,4 +272,30 @@ def bodyClass(self, template, view):
if msl or elonw:
body_classes.append('pat-markspeciallinks')

# Add externally defined extra body classes
body_class_adapters = getAdapters(
(self.context, self.request),
IBodyClassAdapter
)
for name, body_class_adapter in body_class_adapters:
extra_classes = body_class_adapter.get_classes() or []
if isinstance(extra_classes, basestring):
extra_classes = extra_classes.split(' ')
body_classes.extend(extra_classes)

return ' '.join(body_classes)


@adapter(Interface)
class DefaultBodyClasses(object):

implements(IBodyClassAdapter)

def __init__(self, context, request):
self.context = context
self.request = request

def get_classes(self):
"""Default body classes adapter.
"""
return []

0 comments on commit e08fae8

Please sign in to comment.