Comment Form
============
This document explains how to extend the plone.app.discussion comment form with
additional fields in an add-on product.
.. seealso::
* See the github collective for the source code of this howto:
https://github.com/collective/collective.example.commentextender/
.. note::
This howto applies only to plone.app.discussion >= 2.0.4 and >= 1.1.1. Prior
versions will not store the extended fields on the comment.
First, create a new plone package::
$ paster create -t plone my.commentextender
Go to the main directory of the package (my.commentextender/my/commentextender)
and create a new file commentextender.py containing the ICommentExtenderFields
interface definition with an additional "website" field, a persistent
CommentExtenderFields class to actually store the "website" value, a
CommentExtenderFactory to create the CommentExtenderFields, and CommentExtender
class to actually extend the default comment form with the field::
from persistent import Persistent
from z3c.form.field import Fields
from zope import interface
from zope import schema
from zope.annotation import factory
from zope.component import adapts
from zope.interface import Interface
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
from plone.z3cform.fieldsets import extensible
from plone.app.discussion.browser.comments import CommentForm
from plone.app.discussion.comment import Comment
# Interface to define the fields we want to add to the comment form.
class ICommentExtenderFields(Interface):
website = schema.TextLine(title=u"Website", required=False)
# Persistent class that implements the ICommentExtenderFields interface
class CommentExtenderFields(Persistent):
interface.implements(ICommentExtenderFields)
adapts(Comment)
website = u""
# CommentExtenderFields factory
CommentExtenderFactory = factory(CommentExtenderFields)
# Extending the comment form with the fields defined in the
# ICommentExtenderFields interface.
class CommentExtender(extensible.FormExtender):
adapts(Interface, IDefaultBrowserLayer, CommentForm)
fields = Fields(ICommentExtenderFields)
def __init__(self, context, request, form):
self.context = context
self.request = request
self.form = form
def update(self):
# Add the fields defined in ICommentExtenderFields to the form.
self.add(ICommentExtenderFields, prefix="")
# Move the website field to the top of the comment form.
self.move('website', before='text', prefix="")
.. seealso::
* See the plone.z3cform pypi page for more documentation about how to add,
hide, and reorder fields:
http://pypi.python.org/pypi/plone.z3cform#fieldsets-and-form-extenders
Now register the CommentExtenderFactory and CommentExtender Classes we just
created by adding the following lines to your configure.zcml::