Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Related leadimage behavior
# -*- coding: utf-8 -*-
from pnz.www_pnz_de import _
from zope import schema
from plone.autoform.interfaces import IFormFieldProvider
from plone.supermodel import directives
from plone.supermodel import model
from plone.app.multilingual.dx.interfaces import ILanguageIndependentField
from plone.dexterity.interfaces import IDexterityContent
from z3c.relationfield.schema import RelationChoice
from plone.app.vocabularies.catalog import CatalogSource
from plone.app.z3cform.widget import RelatedItemsFieldWidget
from plone.autoform import directives
from zope.interface import provider
from zope.component import adapter
from zope.interface import implementer
from zope.interface import alsoProvides
from zope.interface import Interface
from ComputedAttribute import ComputedAttribute
from pnz.www_pnz_de.utils import default_pattern_options
from pnz.www_pnz_de.utils import default_base_path
from pnz.www_pnz_de.utils import get_linenumber
import logging
log = logging.getLogger(__name__)
class IRelatedTeaserImage(Interface):
pass
@provider(IFormFieldProvider)
class IRelatedTeaserImageBehavior(model.Schema):
related_image = RelationChoice(
title=u"Related Teaser Image",
description=u"Add a teaser image",
required=False,
default=None,
vocabulary='plone.app.vocabularies.Catalog'
)
image_caption = schema.TextLine(
title=_(u'label_image_caption', default=u'Image Caption'),
description=u'',
required=False,
)
directives.widget( 'related_image'
, RelatedItemsFieldWidget
, pattern_options={ 'recentlyUsed': True
, 'basePath': default_base_path
, 'mode': 'auto'
, 'favorites': default_pattern_options
, 'folderTypes': ['Folder', 'LIF', 'LRF']
, 'selectableTypes' : ['Image']
, }
)
alsoProvides(IRelatedTeaserImageBehavior['related_image'], ILanguageIndependentField)
@implementer(IRelatedTeaserImageBehavior)
@adapter(IDexterityContent)
class RelatedTeaserImage(object):
def __init__(self, context):
self.context = context
@ComputedAttribute
def image(self):
image_link = getattr(self.context.aq_explicit, 'related_image', False)
linked_image = image_link and self.context.related_image.to_object.image or ''
return linked_image
<?xml version='1.0' encoding='utf8'?>
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
xmlns:plone="http://namespaces.plone.org/plone"
i18n_domain="pnz.www_pnz_de">
<include package="plone.behavior" file="meta.zcml"/>
<plone:behavior
title="Related Teaser Image Behavior"
description="Add a related image with caption as a teaser"
name="pnz.www_pnz_de.related_teaser_image"
provides=".behaviors.IRelatedTeaserImageBehavior"
factory=".behaviors.RelatedTeaserImage"
marker=".behaviors.IRelatedTeaserImageBehavior"
for="plone.dexterity.interfaces.IDexterityContent"
/>
</configure>
<div class="leadImage" tal:condition="view/available">
<figure class="newsImageContainer"
tal:define="teaser_image python:getattr(context.aq_explicit, 'related_image', False) and context.related_image.to_object;
scale_func teaser_image/@@images;
scaled_image python:teaser_image and scale_func.scale('image', scale='mini')"
tal:condition="python: teaser_image">
<a tal:define="here_url context/@@plone_context_state/object_url;
large_image python: scale_func.scale('image', scale='large');"
class="pat-plone-modal"
data-pat-plone-modal="image: true"
tal:attributes="href large_image/url; data-modal-srcset large_image/srcset_attribute">
<img tal:replace="structure python: scaled_image.tag(css_class='newsImage')" />
<figcaption tal:condition="context/image_caption|nothing"
tal:content="context/image_caption" />
</a>
</figure>
</div>
# -*- coding: utf-8 -*-
from Acquisition import aq_base
from pnz.www_pnz_de.behaviors.behaviors import IRelatedTeaserImageBehavior
from plone.app.layout.viewlets import ViewletBase
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
import logging
log = logging.getLogger(__name__)
class TeaserImageViewlet(ViewletBase):
""" A simple viewlet which renders the related teaser image """
template = ViewPageTemplateFile('./templates/viewlet_teaser_image.pt')
def render(self):
if '@@edit' in self.request.steps:
return ''
return self.index()
def index(self):
context = aq_base(self.context)
if IRelatedTeaserImageBehavior.providedBy(context):
if context.related_image:
return self.template()
return ''
def update(self):
if IRelatedTeaserImageBehavior.providedBy(self.context):
self.context = IRelatedTeaserImageBehavior(self.context)
self.available = True if self.context.related_image else False
else:
self.available = False
#log.info(self.available)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.