Drupal 6: List Selection Zero Value Fix

I discovered a little issue recently with the Profile module in Drupal 6 and after finding the solution I thought I would post it here in case it helps anyone else.

The problem occurs when using the list selection form item in a specific way. This form item works find for just about every situation, except one special case. Lets assume that you wanted to ask a user a question which requires a number between zero and four; you enter the following into the selection options box.


This is saved fine, but when the form is rendered it doesn't actually print out the option to select 0, which is odd as the value is definitely saved in the database and can be reedited in the form admin.

The problem occurs because when the items are being checked before they get rendered the if statement that checks to see if they contain a value just checks to see if they are true. So when the value '0' is checked it is seen as false and skipped. After quite a while trying to figure out how to alter this using hooks I decided the best course of action was to alter the core core and correct what I saw as a bug.

There are a few places where you'll need to change this in the code as it isn't just about printing out the list, it is also about how validation is handled. The first thing to do is to alter the initialisation of the options array so that if the list is not mandatory we will still see the first default item of '--'. All of the following changes arre done to the profile module file at /modules/profile/profile.module.

In line 395 of the file change this code:

$options = $field->required ? array() : array('--');

To this:

$options = $field->required ? array() : array('--' => '--');

We now need to alter the way in which the values are looked at as they are added to the array of values. Start by changing line 398:

if ($line = trim($line)) {

To this:

$line = trim($line);
if (!is_null($line) && $line != '') {

Change line 436:

if ($edit[$field->name]) {

To this:

if (isset($edit[$field->name])) {

Change line 493:

if ($field->value) {

To this:

if (isset($field->value)) {

Change line 518:

if ($field->value) {

To this:

if (isset($field->value)) {

Once you save these changes the profile module will work with zero values.


Thanks Philip, you've saved me a lot of time with that!


Add new comment

The content of this field is kept private and will not be shown publicly.
2 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.