? .svn
? bio-fields-on-registration-182266.patch
Index: bio.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/bio/bio.module,v
retrieving revision 1.2.2.12
diff -u -p -r1.2.2.12 bio.module
--- bio.module	3 Jul 2007 18:28:09 -0000	1.2.2.12
+++ bio.module	30 Nov 2007 04:25:08 -0000
@@ -79,6 +79,82 @@ function bio_form_alter($form_id, &$form
       $form['author']['name']['#description'] = t(' This field is disabled. You cannot alter the author of this entry from within the user area.');
     }
   }
+
+  // Display CCK fields on the user registration form, if they've been
+  // marked as such.
+  if ($form_id == 'user_register' && variable_get('bio_regstration_form', 0)) {
+    $widget_types = _content_widget_types();
+    $fields = _bio_get_fields();
+    $default_values = variable_get('bio_regstration_form_fields', array());
+
+    // Create a dummy node to pass along to the cck hooks.
+    $node = new stdClass();
+    $node->type = variable_get('bio_nodetype', 'bio');
+
+    foreach ($fields as $field_name => $field) {
+      if ($field['required'] || !empty($default_values[$field_name])) {
+        $node_field = content_default_value($node, $field, array());
+      
+        // Figure out what widget function to call.
+        $module = $widget_types[$field['widget']['type']]['module'];
+        $function = $module .'_widget';
+
+        // Get the form field.
+        $form[$field_name] = $function('prepare form values', $node, $field, $node_field);
+        $form[$field_name] = $function('form', $node, $field, $node_field);
+      }
+    }
+
+    // Add custom validate/submit handlers. 
+    $form['#submit']['bio_user_reg_submit'] = array();
+    $form['#validate']['bio_user_register_validate'] = array();
+  }
+}
+
+/**
+ * Validate handler for user registration form. Validate CCK fields.
+ */
+function bio_user_register_validate($form_id, $form_values) {
+  // Create a dummy node to pass along to CCK.
+  $node = new stdClass();
+  $node->type = variable_get('bio_nodetype', 'bio');
+  foreach ($form_values as $field_name => $value) {
+    if (preg_match('/^field_/', $field_name)) {
+      $node->$field_name = $form_values[$field_name];
+    }
+  }
+
+  // Call validation routines on the CCK fields.
+  content_validate($node);
+}
+
+/**
+ * Submit handler for user registration form. Automatically creates a bio node
+ * on registration if any bio fields are set to show on the registration form.
+ */
+function bio_user_reg_submit($form_id, $form_values) {
+  // Create bio node for this user.
+  $node = new StdClass;
+  $node->type = variable_get('bio_nodetype', 'bio');
+  $node->uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $form_values['name']));
+  $node->title = $form_values['name'];
+  $node->name = $form_values['name'];
+  $node->status = 1;
+
+  foreach ($form_values as $field_name => $value) {
+    if (preg_match('/^field_/', $field_name)) {
+      $node->$field_name = $form_values[$field_name];
+    }
+  }
+
+  // Create the node.
+  $node = node_submit($node);
+  node_save($node);
+
+  // Give us a nice log message.
+  if ($node->nid) {
+    watchdog('content', t('Bio: added %user bio upon registration.', array('%user' => $node->name)), WATCHDOG_NOTICE, l(t('view'), "node/$node->nid"));
+  }
 }
 
 function bio_profile_alter(&$account, &$fields) {
@@ -167,7 +243,7 @@ function bio_link($type, $node = NULL, $
         'attributes' => array('title' => t('View @user\'s @bio.', array('@user' => $user->name, '@bio' => node_get_types('name', variable_get('bio_nodetype', 'bio')))))));
     }
   } 
-} 
+}
 
 /**
  * Return node id of the bio for a given user
@@ -222,11 +298,63 @@ function bio_settings() {
     '#description' => t('Display nothing but the bio node on the user profile page.'),
     '#default_value' => variable_get('bio_profile_takeover', 0),
   );
+
+  // Add option to show fields on the registration form.
+  $form['bio_regstration_form'] = array(
+    '#type' => 'radios',
+    '#title' => t('Show fields on registration form'),
+    '#options' => array(t('Disabled'), t('Enabled')),
+    '#default_value' => variable_get('bio_regstration_form', 0),
+    '#description' => t('Enable this option to display bio fields on the user registration form. This will automatically create a bio record for a user when they register.'),
+  );
+
+  // Determine the options and default values.
+  $fields = _bio_get_fields();
+  $default_values = variable_get('bio_regstration_form_fields', array());
+  foreach ($fields as $field_name => $properties) {
+    $options[$field_name] = check_plain($properties['widget']['label']);
+    // Required fields are always shown on registration form.
+    if ($properties['required'] || !empty($default_values[$field_name])) {
+      $default_values[$field_name] = $field_name;
+    }
+    else {
+      $default_vales[$field_name] = 0;
+    }
+  }
+  // Display list of fields.
+  $form['bio_regstration_form_fields'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Registration form fields'),
+    '#options' => $options,
+    '#default_value' => $default_values,
+    '#description' => t('Fields checked here will be displayed on the user registration form. Required fields are always shown.'),
+    '#theme' => 'bio_registration_fields',
+  );
+
   $add_a_submit = system_settings_form($form);
   $add_a_submit['#validate']['bio_settings_validate_xxx'] = array();
   return $add_a_submit;
 }
 
+/**
+ * Theme function for bio registration form options that adds a disabled flag to
+ * required fields.
+ * 
+ * @ingroup themeable
+ */
+function theme_bio_registration_fields($form) {
+  $fields = _bio_get_fields();
+  foreach (element_children($form) as $field_name) {
+    // Disable required fields; they always show up.
+    if ($fields[$field_name]['required']) {
+      $form[$field_name]['#attributes'] = array('disabled' => 'disabled');
+      $form[$field_name]['#value'] = $field_name;
+    }
+  }
+
+  return drupal_render($form);
+}
+
 /*
  * A submit handler for the bio settings page so that we can invalidate the views
  * cache in case the bio node type changed. We need the _xxx suffix to keep it
@@ -238,6 +366,15 @@ function bio_settings_validate_xxx($form
 }
 
 /**
+ * Retrieve field info for bio CCK type.
+ */
+function _bio_get_fields() {
+  $bio_nodetype = variable_get('bio_nodetype', 'bio');
+  $type = content_types($bio_nodetype);
+  return $type['fields'];
+}
+
+/**
  * implementation of hook_views_table_alter()
  *
  * bio uses hook_views_table_alter to clone all the existing fields and handlers
