Package: collective.examples.userdata
IntroductionWith Plone 5, the registration and personal information forms arez3c.formforms.
These can be extended to allow any additional data to be collected on the forms.This product aims to show how you could extend or modify the default schema
provided byplone.app.users, and add new fields to the registration form.If you are using a Plone version previous to Plone 5, then you need to look at the older version ofcollective.examples.userdata here.Note for upgradersPreviously, yourIEnhancedUserDataSchemawas registered via. aIUserDataSchemaProviderutility you registered incomponentregistry.xml. If this is something you did, then be sure to remove
this utility, ideally before upgrading.wildcard.fixpersistentutilitiescould help here.Adding custom userdata fieldsThe code below is snippets from the source code from the package. Look there to
see more examples.Creating a schemaWe create a schema for our fields in the same manner as any other schema:class IEnhancedUserDataSchema(model.Schema):
    country = schema.TextLine(
        title=_(u'label_country', default=u'Country'),
        description=_(u'help_country',
                      default=u"Fill in the country you live in."),
        required=False,
        )Extending the userdata formTo add this schema to the form, we need to define a form extender forUserDataPanelwhich allows us to register any new fields we want to:class UserDataPanelExtender(extensible.FormExtender):
    adapts(Interface, IDefaultBrowserLayer, UserDataPanel)
    def update(self):
        fields = field.Fields(IEnhancedUserDataSchema)
        fields = fields.omit('accept') # Users have already accepted.
        self.add(fields)And register this in configure.zcml:<adapter
  factory=".userdataschema.UserDataPanelExtender"
  provides="plone.z3cform.fieldsets.interfaces.IFormExtender" />Storing / retrieving custom fieldsTo store the values alongside default fields, we need to add fields toprofiles/default/memberdata_properties.xml. For example:<?xml version="1.0"?>
<object name="portal_memberdata" meta_type="Plone Memberdata Tool">
  <property name="country" type="string"></property>
</object>We don’t define the “accept” field here, since that is only for signup.
They have to have accepted to have a user in the system.Before values can be read and written by the form, there needs to be a data
manager to fetch the values. The default manager will read/write any field
defined in the schema, so most of the work is done for you:from plone.app.users.browser.account import AccountPanelSchemaAdapter

class EnhancedUserDataSchemaAdapter(AccountPanelSchemaAdapter):
    schema = IEnhancedUserDataSchemaIf you want to do something different, add a property for that field to
override the default behavior. The source code shows this for thebirthdatefield.Finally, register the data manager in ZCML:<adapter
  provides=".userdataschema.IEnhancedUserDataSchema"
  for="plone.app.layout.navigation.interfaces.INavigationRoot"
  factory=".adapter.EnhancedUserDataSchemaAdapter"
  />Extending the registration formTo extend the registration form, you have 2 choices. Either using the
@@member-registration view to manipulate which of the default fields are
visible, or for full control you can register another form extender.Defining registration field FormExtendersRegistering an extender forBaseRegistrationFormwill allow us to add
fields at any point to the registration form. This is done in the same way
as before:class RegistrationPanelExtender(extensible.FormExtender):
    adapts(Interface, IDefaultBrowserLayer, BaseRegistrationForm)
    def update(self):
        fields = field.Fields(IEnhancedUserDataSchema)
        #NB: Not omitting the accept field this time, we want people to check it
        self.add(fields)And register this in configure.zcml:<adapter
  factory=".userdataschema.RegistrationPanelExtender"
  provides="plone.z3cform.fieldsets.interfaces.IFormExtender" />The data manager is attached to the schema, so will be shared with the user
data form. If we used a different schema, then we would have to define another
data manager too.Various other field examplesThere are various other extra fields with which you could extend your users’
profile. Inuserdataschema.pyyou will find examples for:a Date field (birthdate)a Boolean field (newsletter)a Choice field (gender)The “Accept Terms” fieldA special case is theacceptfield. This is a Boolean field which is
required for signup. We implement it by adding aconstraintto the schema:def validateAccept(value):
    if not value == True:
        return False
    return True

class IEnhancedUserDataSchema(IUserDataSchema):
    # ...
    accept = schema.Bool(
        title=_(u'label_accept', default=u'Accept terms of use'),
        description=_(u'help_accept',
                      default=u"Tick this box to indicate that you have found,"
                      " read and accepted the terms of use for this site. "),
        required=True,
        constraint=validateAccept,
        )Because this field can be ignored once registration is complete, we don’t add
it to the memberdata properties. We also hide it from the userdata forms.Changelog2.1 (2014-07-03)Fix broken setup.py so we generate documentation & install properly again.
[lentinj]2.0 (2014-07-03)Convert to show customisation of z3c.form-based plone.app.users.
[lentinj, vipod, thet]0.5 (unreleased)Nothing changed yet.0.4 (2013-12-18)Fix gender vocabulary and translate it properly. (thet)Dont’ overwrite existing user registration fields, just add accept and
country. (thet)Add field “birthyear” for use cases where asking for the exact birth date is
too much. (thet)Using locales directory instead of i18n and general cleanup. (thet)Moved to github (Kees Hink)Added German translation (Pavel Bogdanovic, 2011-10-28).0.3 (2010-09-26)Make the custom fields available only when the product is installed through
Generic Setup, by overriding the utility in componentregistry.xml (Elizabeth
Leddy).Added a metadata.xml with a profile version.0.2 (2010-04-12)Override plone.app.users’ adapter class, so @@personal-information will show
the extra fields defined in this product.
This requires plone.app.users >= 1.0b7.0.1 (2010-02-28)Initial releaseTo doUninstalling the product currently breaks the site. An uninstall step should
be added to take the fields that the product added out of the ZMI’s user
properties.