Skip to main content

Showcase for the new (Plone 4) IUserDataSchema. Shows how to extend the user data fields that can be selected for the registration form.

Project description

('Introduction\n============\n\nWith Plone 5, the registration and personal information forms are z3c.form_ forms.\nThese can be extended to allow any additional data to be collected on the forms.\n\nThis product aims to show how you could extend or modify the default schema\nprovided by, and add new fields to the registration form.\n\nIf you are using a Plone version previous to Plone 5, then you need to look at the older version of\n`collective.examples.userdata here <>`__.\n\nAdding custom userdata fields\n-----------------------------\n\nThe code below is snippets from the source code from the package. Look there to\nsee more examples.\n\nCreating a schema\n~~~~~~~~~~~~~~~~~\n\nWe create a schema for our fields in the same manner as any other schema::\n\n class IEnhancedUserDataSchema(model.Schema):\n country = schema.TextLine(\n title=_(u\'label_country\', default=u\'Country\'),\n description=_(u\'help_country\',\n default=u"Fill in the country you live in."),\n required=False,\n )\n\nExtending the userdata form\n~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nTo add this schema to the form, we need to define a form extender for\n``UserDataPanel`` which allows us to register any new fields we want to::\n\n class UserDataPanelExtender(extensible.FormExtender):\n adapts(Interface, IDefaultBrowserLayer, UserDataPanel)\n def update(self):\n fields = field.Fields(IEnhancedUserDataSchema)\n fields = fields.omit(\'accept\') # Users have already accepted.\n self.add(fields)\n\nAnd register this in configure.zcml::\n\n <adapter\n factory=".userdataschema.UserDataPanelExtender"\n provides="plone.z3cform.fieldsets.interfaces.IFormExtender" />\n\nStoring / retrieving custom fields\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nTo store the values alongside default fields, we need to add fields to\n``profiles/default/memberdata_properties.xml``. For example::\n\n <?xml version="1.0"?>\n <object name="portal_memberdata" meta_type="Plone Memberdata Tool">\n <property name="country" type="string"></property>\n </object>\n\nWe don\'t define the "accept" field here, since that is only for signup.\nThey have to have accepted to have a user in the system.\n\nBefore values can be read and written by the form, there needs to be a data\nmanager to fetch the values. The default manager will read/write any field\ndefined in the schema, so most of the work is done for you::\n\n from import AccountPanelSchemaAdapter\n\n class EnhancedUserDataSchemaAdapter(AccountPanelSchemaAdapter):\n schema = IEnhancedUserDataSchema\n\nIf you want to do something different, add a property for that field to\noverride the default behavior. The source code shows this for the ``birthdate``\nfield.\n\nFinally, register the data manager in ZCML::\n\n <adapter\n provides=".userdataschema.IEnhancedUserDataSchema"\n for=""\n factory=".adapter.EnhancedUserDataSchemaAdapter"\n />\n\nExtending the registration form\n-------------------------------\n\nTo extend the registration form, you have 2 choices. Either using the\n@@member-registration view to manipulate which of the default fields are\nvisible, or for full control you can register another form extender.\n\nDefining registration field FormExtenders\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nRegistering an extender for ``BaseRegistrationForm`` will allow us to add\nfields at any point to the registration form. This is done in the same way\nas before::\n\n class RegistrationPanelExtender(extensible.FormExtender):\n adapts(Interface, IDefaultBrowserLayer, BaseRegistrationForm)\n def update(self):\n fields = field.Fields(IEnhancedUserDataSchema)\n #NB: Not omitting the accept field this time, we want people to check it\n self.add(fields)\n\nAnd register this in configure.zcml::\n\n <adapter\n factory=".userdataschema.RegistrationPanelExtender"\n provides="plone.z3cform.fieldsets.interfaces.IFormExtender" />\n\nThe data manager is attached to the schema, so will be shared with the user\ndata form. If we used a different schema, then we would have to define another\ndata manager too.\n\nVarious other field examples\n----------------------------\n\nThere are various other extra fields with which you could extend your users\'\nprofile. In ```` you will find examples for:\n\n- a Date field (``birthdate``)\n- a Boolean field (``newsletter``)\n- a Choice field (``gender``)\n\nThe "Accept Terms" field\n~~~~~~~~~~~~~~~~~~~~~~~~\n\nA special case is the ``accept`` field. This is a Boolean field which is\nrequired for signup. We implement it by adding a ``constraint`` to the schema::\n\n def validateAccept(value):\n if not value == True:\n return False\n return True\n\n class IEnhancedUserDataSchema(IUserDataSchema):\n # ...\n accept = schema.Bool(\n title=_(u\'label_accept\', default=u\'Accept terms of use\'),\n description=_(u\'help_accept\',\n default=u"Tick this box to indicate that you have found,"\n " read and accepted the terms of use for this site. "),\n required=True,\n constraint=validateAccept,\n )\n\nBecause this field can be ignored once registration is complete, we don\'t add\nit to the memberdata properties. We also hide it from the userdata forms.\n\n..\n.. _z3c.form:\n\nChangelog\n=========\n\n2.0 (2014-07-03)\n----------------\n\n- Convert to show customisation of z3c.form-based\n [lentinj, vipod, thet]\n\n0.5 (unreleased)\n----------------\n\n- Nothing changed yet.\n\n\n0.4 (2013-12-18)\n----------------\n\n- Fix gender vocabulary and translate it properly. (thet)\n- Dont\' overwrite existing user registration fields, just add accept and\n country. (thet) \n- Add field "birthyear" for use cases where asking for the exact birth date is\n too much. (thet)\n- Using locales directory instead of i18n and general cleanup. (thet)\n- Moved to github (Kees Hink)\n- Added German translation (Pavel Bogdanovic, 2011-10-28).\n\n0.3 (2010-09-26)\n----------------\n\n- Make the custom fields available only when the product is installed through\n Generic Setup, by overriding the utility in componentregistry.xml (Elizabeth\n Leddy).\n- Added a metadata.xml with a profile version.\n\n0.2 (2010-04-12)\n----------------\n\n- Override\' adapter class, so @@personal-information will show\n the extra fields defined in this product.\n This requires >= 1.0b7.\n\n0.1 (2010-02-28)\n----------------\n\n- Initial release\n\nTo do\n=====\n\n- Uninstalling the product currently breaks the site. An uninstall step should\n be added to take the fields that the product added out of the ZMI\'s user\n properties.\n',)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution (23.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page