Extending member properties
How to add member properties to a Plone site
Let's say you want to have a new member property called "gender" that can be
edited by the members on the "Personal Preferences" page.
Step 1: Add the new property to the portal_memberdata tool
On your Plone site, go to /portal_memberdata, click on the Properties tab
and add the new property:
Name = gender, Value = (leave this empty, you could provide a default value
here), Type = string.
Click on "Add" to add the new property.
Step 2: Customize the personalize_form so that your new property becomes
visible
On your Plone site, go to portal_skins/plone_prefs/personalize_form and
click on "Customize" (if you haven't done this already).
Then go to portal_skins/custom/personalize_form and insert the following
code into the Page template:
<div class="row" tal:define="error_gender errors/gender |
nothing; gender python:request.get('gender',
member.getProperty('gender'));">
<div class="label">
<span i18n:translate="label_gender">Gender</span>
<div id="gender_help" i18n:translate="help_gender"
class="help" style="visibility:hidden">
You can specify your gender here.
</div>
</div>
<div class="field" tal:attributes="class
python:test(error_gender, 'field error', 'field')">
<div tal:condition="error_gender" tal:replace="structure
string:$error_gender <br />" />
<input type="text" name="gender" size="25" tabindex=""
value="member.gender html_quote" onfocus="formtooltip(gender_help,1)"
onblur="formtooltip(gender_help,0)" tal:attributes="value gender; tabindex
tabindex/next;" />
</div>
</div>
A good place to do this would be after the closing of the div tag which
starts like this:
<div class="field" tal:define="visible_ids ............
(just search for visible_ids and then scroll down until you see the </div>
tag)
Step 3: Look at our sample and then try your own property
For a sample of what has just been described, go to
http://www.vasudevaserver.org:7091/Dhyani, log in or join if you are not a
member yet. Then click on "my preferences" and then on "Personal
Preferences". Here you see the result. If you want to see the code, go to
the respective Plone folders as described in Step 1 and Step 2.
Now, if you wanted to have another property called name_of_pet, you would go
through Step 1 and Step2, first adding the name_of_pet property as described
in Step 1, then adding the above code to the personalize_form. Obviously,
you would have to overwrite every occurrence of the string "gender" with the
string "name_of_pet".
Step 4: Making the gender property compulsory
Go to portal_skins/custom/personalize_form and click on the Validation tab.
In the Section "Edit Default Validators", where you see the string
"validate_emailaddr, validate_personalize", insert validate_gender:
validate_emailaddr, validate_personalize, validate_gender
Click on "Save"!
Now go to the script portal_skins/plone_form_scripts/validate_emailaddr,
click on Customize, then go to portal_skins/custom and rename this script to
validate_gender. Go inside this script and change the title to "Validates
gender" (instead of "Validates an email") and the Parameter List to
gender='' (instead of email=''). Then delete all its code and insert the
following instead:
reg_tool=context.portal_registration
def invalid(field):
state.setError('gender', 'You did not enter a gender.',
'invalid_gender')
if gender:
pass
else:
invalid('gender')
if state.getErrors():
return state.set(status='failure', portal_status_message='Please correct
the indicated errors.')
else:
return state
And do not forget to have the following attribute set in the field
definition of the gender (inside personalize_form):
tal:attributes="class python:test(error, 'field error', 'field')"
