21. Dexterity II: Talks#

In this part you will solve the following task:

  • Create a contenttype 'talk' to store all the data required for a talk

Topics covered:

  • Registration and configuration of content types

  • Schema

  • Fields

  • Widgets

21.1. The type registration#

Add a new file types.xml to your add-on package in profiles/default/.


That is backend/src/ploneconf.site/src/ploneconf/site/profiles/default/types.xml

This will tell Plone that there is a new content type defined in file talk.xml.

<?xml version="1.0"?>
<object name="portal_types" meta_type="Plone Types Tool">
  <object name="talk" meta_type="Dexterity FTI"/>

Plone will now expect a file profiles/default/types/talk.xml and will register that as a new content type.

21.2. The FTI#

Add the file ploneconf/site/profiles/default/types/talk.xml. Note that there is a file types and a folder types.

This is the Factory Type Information that holds the configuration for the content type talk.

 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 <property name="title" i18n:translate="">Talk</property>
 5 <property name="description" i18n:translate=""></property>
 6 <property name="icon_expr">string:${portal_url}/document_icon.png</property>
 7 <property name="factory">talk</property>
 8 <property name="add_view_expr">string:${folder_url}/++add++talk</property>
 9 <property name="link_target"></property>
10 <property name="immediate_view">view</property>
11 <property name="global_allow">True</property>
12 <property name="filter_content_types">True</property>
13 <property name="allowed_content_types"/>
14 <property name="allow_discussion">False</property>
15 <property name="default_view">view</property>
16 <property name="view_methods">
17  <element value="view"/>
18 </property>
19 <property name="default_view_fallback">False</property>
20 <property name="add_permission">cmf.AddPortalContent</property>
21 <property name="klass">ploneconf.site.content.talk.Talk</property>
22 <property name="schema">ploneconf.site.content.talk.ITalk</property>
23 <property name="behaviors">
24  <element value="plone.dublincore"/>
25  <element value="plone.namefromtitle"/>
26  <element value="plone.versioning" />
27 </property>
28 <property name="model_source"></property>
29 <property name="model_file"></property>
30 <property name="schema_policy">dexterity</property>
31 <alias from="(Default)" to="(dynamic view)"/>
32 <alias from="edit" to="@@edit"/>
33 <alias from="sharing" to="@@sharing"/>
34 <alias from="view" to="(selected layout)"/>
35 <action title="View" action_id="view" category="object" condition_expr=""
36    description="" icon_expr="" link_target="" url_expr="string:${object_url}"
37    visible="True">
38  <permission value="View"/>
39 </action>
40 <action title="Edit" action_id="edit" category="object" condition_expr=""
41    description="" icon_expr="" link_target=""
42    url_expr="string:${object_url}/edit" visible="True">
43  <permission value="Modify portal content"/>
44 </action>

Now our package has a new configuration for Generic Setup. Generic Setup loads a lot of different types of configuration for the site from folder profiles/. This configuration is applied to your site upon installing the package. This also means that you will need to reinstall the package once we are finished with the talk.

But the type is not yet complete since the schema (ploneconf.site.content.talk.ITalk) and the class (ploneconf.site.content.talk.Talk) that are referenced in the FTI are not yet there.

21.3. The schema#

The schema holds the definition of the fields that the content type will offer to store data.

In the FTI we referenced the python-path ploneconf.site.content.talk.ITalk.

The module content does not exist. Create a folder content and add a empty __init__.py in it.


From the training root that is backend/src/ploneconf.site/src/ploneconf/site/content/__init__.py

You just created a python module.

In this new folder add a file talk.py with the following content:

 1from plone.app.textfield import RichText
 2from plone.autoform import directives
 3from plone.dexterity.content import Container
 4from plone.namedfile.field import NamedBlobImage
 5from plone.schema.email import Email
 6from plone.supermodel import model
 7from z3c.form.browser.checkbox import CheckBoxFieldWidget
 8from z3c.form.browser.radio import RadioFieldWidget
 9from zope import schema
10from zope.interface import implementer
11from zope.schema.vocabulary import SimpleTerm
12from zope.schema.vocabulary import SimpleVocabulary
15class ITalk(model.Schema):
16    """Dexterity-Schema for Talks"""
18    directives.widget(type_of_talk=RadioFieldWidget)
19    type_of_talk = schema.Choice(
20        title='Type of talk',
21        values=['Talk', 'Training', 'Keynote'],
22        required=True,
23    )
25    details = RichText(
26        title='Details',
27        description='Description of the talk (max. 2000 characters)',
28        max_length=2000,
29        required=True,
30    )
32    directives.widget(audience=CheckBoxFieldWidget)
33    audience = schema.Set(
34        title='Audience',
35        value_type=schema.Choice(
36            values=['Beginner', 'Advanced', 'Professional'],
37        ),
38        required=False,
39    )
41    speaker = schema.TextLine(
42        title='Speaker',
43        description='Name (or names) of the speaker',
44        required=False,
45    )
47    company = schema.TextLine(
48        title='Company',
49        required=False,
50    )
52    email = Email(
53        title='Email',
54        description='Email adress of the speaker',
55        required=False,
56    )
58    website = schema.TextLine(
59        title='Website',
60        required=False,
61    )
63    twitter = schema.TextLine(
64        title='Twitter name',
65        required=False,
66    )
68    github = schema.TextLine(
69        title='Github username',
70        required=False,
71    )
73    image = NamedBlobImage(
74        title='Image',
75        description='Portrait of the speaker',
76        required=False,
77    )
79    speaker_biography = RichText(
80        title='Speaker Biography (max. 1000 characters)',
81        max_length=1000,
82        required=False,
83    )
87class Talk(Container):
88    """Talk instance class"""

The first class ITalk is the schema for talks and defines quite a lot of different fields for different kinds of data.

  • The fields in the schema are mostly from zope.schema.

  • The most basic field is schema.TextLine which can store text.

  • In the next chapter you will find a reference of all field-types available in Plone.

  • In directives.widget(level=RadioFieldWidget) we change the default widget for a Choice field from a dropdown to radio-boxes.

21.4. The instance class#

The second class Talk in talk.py will be the class of instances for each talk. It inherits from Container which is one of the default classes of dexterity. Container is used for items that can contain other items. It does nothing so far but it can be useful later when we want to add methods or properties to it that can be used directly from a talk instance.

21.5. Try the new type#

Now all pieces should be in place and you can enable the new type Talk.

  • Restart Plone (to load the new Python code and the changed zcml)

  • You do not need to restart the Volto frontend since we did not do any changes there.

  • Re-install the package ploneconf.site (deactivate and activate) to load the type registration and type configuration.

Now the new types should be visible in the add-menu.

You can test the type in the frontend (http://localhost:3000/add?type=talk) and in the backend (http://localhost:8080/Plone/++add++talk).


By default the frontend dies not render all fields, only the title and descriptions is visible. The server-side-rendered template instead iterates over all fields in your schema and displays them in a default way.

In one of the next chapters you will create a custom view for the new type.


Adding a talk in the frontend#


Adding a talk in the backend#

  • Test the type by adding a talk. Add some values in the fields, save it, look at the view and edit it again.

  • Compare all the fields you see to the code in the schema.

  • You can also make changes in the schema. After restarting the backend these changes are effective immediatley

  • Find the tool portal_types in the ZMI

  • Look at the FTI for talk and inspect the configuration taken from the FTI.

  • You can make changes to the FTI here. Some of the configuration are also available in plone control panels where it makes sense. For example the dexterity-controlpanel http://localhost:3000/controlpanel/dexterity-types can modify the behaviors (defined in <property name="behaviors">) and http://localhost:8080/@@content-controlpanel has a checkbox for the setting <property name="global_allow">.

21.6. Summary#

  • You created a custom content type.

  • You can now control the data that will be stored for talks.

  • You can reuse and adapt these examples to model data for your own use-cases.

  • Next up: After looking at even more fields that are available in Plone you will learn to change how talks are displayed.