Description
How to create settings for your add-on product and how to programmatically add new Plone control panel entries.
This documentation tells you how to create new “configlets” to Plone site setup control panel.
Information
ノート
TODO: Remove pointer to better trunk version README after plone.app.registry is properly released.
Example products
Both plone.app.z3cform and plone.app.registry must have been quick installed at Plone site before you can use any control panel configlets using plone.app.registry framework.
plone.app.registry provides RegistryEditForm class which is a subclass of z3c.form.form.Form.
It has two phases to override which widgets are going to be used for a which field.
Example (collective.gtags project controlpanel.py):
class TagSettingsEditForm(controlpanel.RegistryEditForm):
schema = ITagSettings
label = _(u"Tagging settings")
description = _(u"Please enter details of available tags")
def updateFields(self):
super(TagSettingsEditForm, self).updateFields()
self.fields['tags'].widgetFactory = TextLinesFieldWidget
self.fields['unique_categories'].widgetFactory = TextLinesFieldWidget
self.fields['required_categories'].widgetFactory = TextLinesFieldWidget
def updateWidgets(self):
super(TagSettingsEditForm, self).updateWidgets()
self.widgets['tags'].rows = 8
self.widgets['tags'].style = u'width: 30%;'
You need this if you have started using plone.app.registry before 2010-04.
There is a change considering the 1.0b1 codebase:
try:
# plone.app.registry 1.0b1
from plone.app.registry.browser.form import RegistryEditForm
from plone.app.registry.browser.form import ControlPanelFormWrapper
except ImportError:
# plone.app.registry 1.0b2+
from plone.app.registry.browser.controlpanel import RegistryEditForm
from plone.app.registry.browser.controlpanel import ControlPanelFormWrapper
ノート
Deprecation warning: This is an old best practice and discouraged in new development
Often you need to have a setting whether a certain functionality is enabled on particular content types.
Here are the ingredients
interfaces.py:
from zope import schema
from plone.directives import form
from z3c.form.browser.checkbox import CheckBoxFieldWidget
class ISettings(form.Schema):
""" Define schema for settings of the add-on product """
form.widget(enabled_overrides=CheckBoxFieldWidget)
content_types = schema.List(title=u"Enabled content types",
description=u"On which content types Facebook Like-button should appear",
required=False, default=["Document"],
value_type=schema.Choice(source="mfabrik.like.content_types"),
)
configure.zcml
<!-- make sure that plone.app.registry is loaded -->
<includeDependencies package="." />
<utility
provides="zope.schema.interfaces.IVocabularyFactory"
component=".vocabularies.content_types_vocabulary"
name="mfabrik.like.content_types"
/>
<browser:page
name="like-controlpanel"
for="Products.CMFPlone.interfaces.IPloneSiteRoot"
permission="cmf.ManagePortal"
class=".views.ControlPanelView"
/>
views.py:
try:
# plone.app.registry 1.0b1
from plone.app.registry.browser.form import RegistryEditForm
from plone.app.registry.browser.form import ControlPanelFormWrapper
except ImportError:
# plone.app.registry 1.0b2+
from plone.app.registry.browser.controlpanel import RegistryEditForm
from plone.app.registry.browser.controlpanel import ControlPanelFormWrapper
from mfabrik.like.interfaces import ISettings
from plone.z3cform import layout
class ControlPanelForm(RegistryEditForm):
schema = ISettings
ControlPanelView = layout.wrap_form(ControlPanelForm, ControlPanelFormWrapper)
profiles/default/registry.xml:
<registry>
<records prefix="mfabrik.like" interface="mfabrik.like.interfaces.ISettings">
<!-- Enable on normal pages by default -->
<value key="content_types" purge="false">
<element>Document</element>
</value>
</records>
</registry>
profiles/default/controlpanel.xml:
<?xml version="1.0"?>
<object
name="portal_controlpanel"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="mfabrik.like">
<configlet
title="Facbook Like-button settings"
action_id="mfbarik.like.settings"
appId="mfabrik.like"
category="Products"
condition_expr=""
url_expr="string:${portal_url}/@@like-controlpanel"
icon_expr="string:"
visible="True"
i18n:attributes="title">
<permission>Manage portal</permission>
</configlet>
</object>
Then you can simply check whether a particular portal type is enabled in your settings:
from zope.component import getUtility
from plone.registry.interfaces import IRegistry
from mfabrik.like.interfaces import ISettings
def isEnabledOnContent(self):
"""
@return: True if the current content type supports Like-button
"""
registry = getUtility(IRegistry)
try:
settings = registry.forInterface(ISettings)
except KeyError:
# Registry schema and actual values do not match
# Quick installer has not been run or need to rerun
# to update registry.xml values to database
# http://svn.plone.org/svn/plone/plone.registry/trunk/plone/registry/registry.py
return False
content_types = settings.content_types
# Don't assume that all content items would have portal_type attribute
# available (might be changed in the future / very specialized content)
current_content_type = portal_type = getattr(Acquisition.aq_base(self.context), 'portal_type', None)
return current_content_type in content_types