Empty fields and CiviCRM profiles
We recently had a client that needed to allow anonymous users to update CiviCRM data. No problem right? Right! As long as those anonymous users aren't updating optional fields. This post is here for historical reasons. If you're using a fairly recent version of CiviCRM (3.x), you don't need this module.
A common need is to create a form where you allow your constituents to sign up for something: email, volunteer, taking action, etc. Many of those that fill out the form will already be in CiviCRM as contacts. Further, many if not most of the fields of the form will be optional as to not hinder someone's "action" by asking too much of them at one time. This has historically presented a major problem for CiviCRM because, if a you leave fields in a CiviCRM profile optional and the person filling out the field leaves it blank, CiviCRM will overwrite whatever value was already in the database with a blank value. We see this as a major design flaw because an organization is, without even knowing it, losing valuable data.
There are several options in trying to address this problem:
- Create the profile in which all fields are required. Not a practical solution.
- Complain that it doesn't work and hope someday someone fixes it. Not very practical either.
- Override the core CiviCRM templates to ignore those blank fields.
- Write a Drupal module implementing CiviCRM hooks to ignore those blank fields.
As an exercise and as a way to provide a minimally invasive approach to solving this problem, we chose to write a Drupal module. The module, civicrm_profile_tweak, is very simple. Once the module is enabled, it adds a checkbox "Ignore empty values in this profile" to the profile settings page. This allows for control over individual profiles, since there is a use case for wanting the ability to clear values in the database. Create or edit a profile that has an optional field, something like first name (required), last name (required), phone number (optional). Create a contact with a first name, last name and phone number. Logout and access the form, filling out only the first and last name. Log back in and rejoice that the phone number is still there.
How does it work? It implements three hooks:
hook_civicrm_buildForm(), creates the checkbox hook_civicrm_pre(), stores the checkbox value (enabled or not) hook_civicrm_postProcess(), removes the empty profile values before being submitted to CiviCRM
Take a look at the code for a better understanding of what's going on.
Update: 10/31/2009 to 6.x-1.1-dev includes support for profiles embedded in contribution and event pages.
We often write "throw-away" code for one-off tasks or to scratch our own itch. The source might not be immediately useful for others but the lessons learned are invaluable. We always try to blog, tweet, or share those lessons when appropriate.
- Avalanche Forecast
- Brewfest Mobile App
- CiviCRM Discount
- CiviCRM Profile Permissions
- Danger Rose
- DB Tools
- Image Node Reference
- Linode Drupal StackScript
- Liquid Planner Shell
- OpenVZ Drupal Installer
- Randstring Safari Extension
- RSRA Database
- Ubercart/CiviCRM Integration
- User Login Block
- Webform Download Archive
In the process of writing custom software for our clients, we've also contributed bug fixes, patches, unit tests and documentation to various open source projects.



Comments
Post new comment