23. Behaviors – Mastering Plone 5 development

23. Behaviors#

In this part you will:

  • Add another field to talks by using a behavior

Topics covered:

  • Behaviors

You can extend the functionality of your Dexterity object by writing an adapter that adapts your dexterity object to add another feature or aspect.

But if you want to use this adapter, you must somehow know that an object implements that. Also, adding more fields to an object would not be easy with such an approach.

23.1. Dexterity Approach#

Dexterity has a solution for it, with special adapters that are called and registered by the name behavior.

A behavior can be added to any content type through the web and at runtime.

All default views (e.g. the add- and edit-forms) know about the concept of behaviors. When rendering forms, the views also check whether there are behaviors referenced with the current context and if these behaviors have a schema of their own, these fields get shown in addition.

23.2. Names and Theory#

The name behavior is not a standard term in software development. But it is a good idea to think of a behavior as an aspect. You are adding an aspect to your content type and you want to write your aspect in such a way that it works independently of the content type on which the aspect is applied. You should not have dependencies to specific fields of your object or to other behaviors.

Such an object allows you to apply the open/closed principle to your dexterity objects.

23.3. Practical example#

So, let us write our own small behavior.

In the future, we want some talks, news items or other content be represented on the frontpage similar to what we did with the "hot news" field early on.

So for now, our behavior just adds a new field to store this information.

We want to keep a clean structure, so we create a behaviors directory first, and include it into the zcml declarations of our configure.zcml.

<include package=".behaviors" />

Then, we add an empty behaviors/__init__.py and a behaviors/configure.zcml containing

 1<configure
 2    xmlns="http://namespaces.zope.org/zope"
 3    xmlns:plone="http://namespaces.plone.org/plone"
 4    i18n_domain="ploneconf.site">
 5
 6  <plone:behavior
 7      title="Featured"
 8      name="ploneconf.featured"
 9      description="Control if a item is shown on the frontpage"
10      provides=".featured.IFeatured"
11      />
12
13</configure>

And a behaviors/featured.py containing:

 1# -*- coding: utf-8 -*-
 2from plone.autoform.interfaces import IFormFieldProvider
 3from plone.supermodel import directives
 4from plone.supermodel import model
 5from zope import schema
 6from zope.interface import provider
 7
 8@provider(IFormFieldProvider)
 9class IFeatured(model.Schema):
10
11    directives.fieldset(
12        'featured',
13        label=u'Featured',
14        fields=('featured',),
15    )
16
17    featured = schema.Bool(
18        title=u'Show this item on the frontpage',
19        required=False,
20    )

Let's go through this step by step.

  1. We register a behavior in behaviors/configure.zcml. We do not say for which content type this behavior is valid. You do this through the web or in the GenericSetup profile.

  2. We create a marker interface in behaviors/social.py for our behavior. We make it also a schema containing the fields we want to declare. We could just define schema fields on a zope.interface class, but we use an extended form from plone.supermodel, else we could not use the fieldset features.

  3. We mark our schema as a class that also provides the IFormFieldProvider interface using a decorator. The schema class itself provides the interface, not its instance!

  4. We also add a fieldset so that our fields are not mixed with the normal fields of the object.

  5. We add a normal Bool schema field to control if a item should be displayed on the frontpage.

Note

It can be a bit confusing when to use factories or marker interfaces and when not to.

If you do not define a factory, your attributes will be stored directly on the object. This can result in clashes with other behaviors.

You can avoid this by using the plone.behavior.AnnotationStorage factory. This stores your attributes in an Annotation. But then you must use a marker interface if you want to have custom viewlets, browser views or portlets.

Without it, you would have no interface against which you could register your views.

23.4. Adding it to our talk#

We could add this behavior now via the plone control panel. But instead, we will do it directly and properly in our GenericSetup profile

We must add the behavior to profiles/default/types/talk.xml:

 1<?xml version="1.0"?>
 2<object name="talk" meta_type="Dexterity FTI" i18n:domain="plone"
 3   xmlns:i18n="http://xml.zope.org/namespaces/i18n">
 4   ...
 5 <property name="behaviors">
 6  <element value="plone.dublincore"/>
 7  <element value="plone.namefromtitle"/>
 8  <element value="ploneconf.featured"/>
 9 </property>
10 ...
11</object>