I'm trying to create a group of checkboxes using the profile module. Does anyone have samples or advice?

Thanks in advance.

Comments

sskennel’s picture

Coincidentally, I'm trying to do the same thing right now. As an experiment, I added a checkbox to the 4.4.0 profile module:

--- profile.module.orig 2004-04-20 15:23:30.000000000 -0400
+++ profile.module      2004-04-23 13:10:42.000000000 -0400
@@ -25,6 +25,7 @@
     "homepage"      => array("textfield", t("URL of homepage"), "", 64, 64, t("Make sure you enter a fully qualified URL: remember to include \"http://\".")),
     "biography"     => array("textarea", t("Biography"), "", 64, 4, ""),
     "interests"     => array("textarea", t("Interests"), "", 64, 4, ""),
+    "programmer"     => array("checkbox", t("Are you a programmer?"), "", "", ""),
     "publickey"     => array("textarea", t("Public key"), "", 64, 4, ""),
     "avatar"        => array("", t("Avatar or picture"), t("Your virtual face or picture.  Maximum dimensions are %dimensions and the maximum size is %size kB.", array("%dimensions" => variable_get("profile_avatar_dimensions", "85x85"), "%size" => variable_get("profile_avatar_file_size", "30"))))
   );

---------------

I enabled it and made it public. When I saved my account settings, this was added to my account's users.data:

s:18:"profile_programmer";s:1:"0";

When I checked the new checkbox and saved again, the data didn't change and the checkbox remained unchecked on the page. That's as far as I've gotten.

Roger H. Goun

Brentwood, NH

idilas’s picture

I'm on the same boat.
I can get the checkboxes to display, but that's about it.

Have you checked your html source? It looks like the naming convention for checkboxes is generated differently.

sskennel’s picture

This version correctly updates users.data with a "1" value when the box is checked.

"programmer" => array("checkbox", t("Are you a programmer?"), "1", "", ""),

The box is still not displayed with a check when the users.data already has a "1".

-- Roger

sskennel’s picture

I submitted a bug report with a patch that fixes this problem.

Roger H. Goun
Brentwood, NH

idilas’s picture

Here's a quick fix for those who don't want to wait for a patch... It's not ultra clean, but it works.

The problem is that drupal never sets the "value" for the checkbox

"programmer" => array("checkbox", t("Are you a programmer?"), "yes"),

In the above declaration, I left out the definition field, but you can add one following the value field ("yes" is the value field)

I've included the fix below. Note the if statement for checkboxes. I thought $field[2] would be a safe bet for the value field because the rest of the form element declarations don't seem to be using $field[2]

Hope that helps.

foreach ($profile_fields as $name => $field) {
if ($field[0] && in_array($name, $reg_fields)) {
$f = "form_". $field[0];
$t = "profile_". $name;

if ($field[0]=="checkbox") {
$output .= form_checkbox($field[1], $t, $field[2], $edit[$t]);
}
else {
$output .= $f((in_array($name, $required_fields) ? profile_required($field[1]) : $field[1]), $t, $edit[$t], $field[3], $field[4], $field[5], $field[6]);
}
}
}