diff --git modules/field/tests/field.test modules/field/tests/field.test index ee6351e..f125770 100644 --- modules/field/tests/field.test +++ modules/field/tests/field.test @@ -988,11 +988,12 @@ class FieldInfoTestCase extends FieldTestCase { */ function testFieldInfo() { // Test that field_test module's fields, widgets, and formatters show up. - $field_test_info = field_test_field_info(); - $formatter_info = field_test_field_formatter_info(); - $widget_info = field_test_field_widget_info(); - $storage_info = field_test_field_storage_info(); + $field_test_info = field_test_field_info(); + // We need to account for the existence of user_field_info_alter(). + foreach (array_keys($field_test_info) as $name) { + $field_test_info[$name]['instance_settings']['user_register_form'] = FALSE; + } $info = field_info_field_types(); foreach ($field_test_info as $t_key => $field_type) { foreach ($field_type as $key => $val) { @@ -1001,6 +1002,7 @@ class FieldInfoTestCase extends FieldTestCase { $this->assertEqual($info[$t_key]['module'], 'field_test', t("Field type field_test module appears")); } + $formatter_info = field_test_field_formatter_info(); $info = field_info_formatter_types(); foreach ($formatter_info as $f_key => $formatter) { foreach ($formatter as $key => $val) { @@ -1009,6 +1011,7 @@ class FieldInfoTestCase extends FieldTestCase { $this->assertEqual($info[$f_key]['module'], 'field_test', t("Formatter type field_test module appears")); } + $widget_info = field_test_field_widget_info(); $info = field_info_widget_types(); foreach ($widget_info as $w_key => $widget) { foreach ($widget as $key => $val) { @@ -1017,6 +1020,7 @@ class FieldInfoTestCase extends FieldTestCase { $this->assertEqual($info[$w_key]['module'], 'field_test', t("Widget type field_test module appears")); } + $storage_info = field_test_field_storage_info(); $info = field_info_storage_types(); foreach ($storage_info as $s_key => $storage) { foreach ($storage as $key => $val) { @@ -1180,6 +1184,10 @@ class FieldInfoTestCase extends FieldTestCase { */ function testSettingsInfo() { $info = field_test_field_info(); + // We need to account for the existence of user_field_info_alter(). + foreach (array_keys($field_test_info) as $name) { + $field_test_info[$name]['instance_settings']['user_register_form'] = FALSE; + } foreach ($info as $type => $data) { $this->assertIdentical(field_info_field_settings($type), $data['settings'], "field_info_field_settings returns {$type}'s field settings"); $this->assertIdentical(field_info_instance_settings($type), $data['instance_settings'], "field_info_field_settings returns {$type}'s field instance settings"); diff --git modules/field_ui/field_ui.admin.inc modules/field_ui/field_ui.admin.inc index 33924f7..8836a95 100644 --- modules/field_ui/field_ui.admin.inc +++ modules/field_ui/field_ui.admin.inc @@ -1731,6 +1731,7 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { drupal_set_title($instance['label']); $form['#field'] = $field; + $form['#instance'] = $instance; if (!empty($field['locked'])) { $form['locked'] = array( diff --git modules/user/user.js modules/user/user.js index 1336d44..3d884ce 100644 --- modules/user/user.js +++ modules/user/user.js @@ -173,4 +173,25 @@ Drupal.evaluatePasswordStrength = function (password, translate) { }; +/** + * Field instance settings screen: force the 'Display on registration form' + * checkbox checked whenever 'Required' is checked. + */ +Drupal.behaviors.fieldUserRegistration = { + attach: function (context, settings) { + var $checkbox = $('form#field-ui-field-edit-form input#edit-instance-settings-user-register-form'); + + if ($checkbox.size()) { + $('input#edit-instance-required', context).once('user-register-form-checkbox', function () { + $(this).bind('change', function (e) { + if ($(this).attr('checked')) { + $checkbox.attr('checked', true); + } + }); + }); + + } + } +}; + })(jQuery); diff --git modules/user/user.module modules/user/user.module index cb29b70..8120295 100644 --- modules/user/user.module +++ modules/user/user.module @@ -177,6 +177,18 @@ function user_uri($user) { } /** + * Implements hook_field_info_alter(). + */ +function user_field_info_alter(&$info) { + // Add the 'user_register_form' instance setting to all field types. + foreach ($info as $field_type => $field_type_info) { + $info[$field_type]['instance_settings'] += array( + 'user_register_form' => FALSE, + ); + } +} + +/** * Implements hook_field_extra_fields(). */ function user_field_extra_fields() { @@ -3520,6 +3532,54 @@ function user_block_user_action(&$entity, $context = array()) { } /** + * Implements hook_form_FORM_ID_alter(). + * + * Add a checkbox for the 'user_register_form' instance settings on the 'Edit + * field instance' form. + */ +function user_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) { + $instance = $form['#instance']; + + if ($instance['entity_type'] == 'user') { + $form['instance']['settings']['user_register_form'] = array( + '#type' => 'checkbox', + '#title' => t('Display on user registration form.'), + '#description' => t("This is compulsory for 'required' fields."), + '#default_value' => $instance['settings']['user_register_form'], + // Display just below the 'required' checkbox. + '#weight' => $form['instance']['required']['#weight'] + .1, + // Disabled when the 'required' checkbox is checked. + '#states' => array( + 'enabled' => array('input[name="instance[required]"]' => array('checked' => FALSE)), + ), + // Checked when the 'required' checkbox is checked. This is done through + // a custom behavior, since the #states system would also synchronize on + // uncheck. + '#attached' => array( + 'js' => array(drupal_get_path('module', 'user') . '/user.js'), + ), + ); + + // Before Field UI saves the instance, make sure the 'user_register_form' + // setting is set for required fields. + array_unshift($form['#submit'], 'user_form_field_ui_field_edit_form_submit'); + } +} + +/** + * Additional submit handler for the 'Edit field instance' form. + * + * Make sure the 'user_register_form' setting is set for required fields. + */ +function user_form_field_ui_field_edit_form_submit($form, &$form_state) { + // @todo : on edit, this will silently push existing fields on user_register form + $instance = $form_state['values']['instance']; + if (!empty($instance['required'])) { + form_set_value($form['instance']['settings']['user_register_form'], 1, $form_state); + } +} + +/** * Form builder; the user registration form. * * @ingroup forms @@ -3546,6 +3606,15 @@ function user_register_form($form, &$form_state) { // Start with the default user account fields. user_account_form($form, $form_state); + // Attach field widgets, and hide the ones where the 'user_register_form' + // setting is not on. + field_attach_form('user', $form['#user'], $form, $form_state); + foreach (field_info_instances('user', 'user') as $field_name => $instance) { + if (empty($instance['settings']['user_register_form'])) { + $form[$field_name]['#access'] = FALSE; + } + } + if ($admin) { // Redirect back to page which initiated the create request; // usually admin/people/create. @@ -3558,6 +3627,7 @@ function user_register_form($form, &$form_state) { '#value' => t('Create new account'), ); + $form['#validate'][] = 'user_register_validate'; // Add the final user registration form submit handler. $form['#submit'][] = 'user_register_submit'; @@ -3565,6 +3635,13 @@ function user_register_form($form, &$form_state) { } /** + * Validation function for the user registration form. + */ +function user_register_validate($form, &$form_state) { + entity_form_field_validate('user', $form, $form_state); +} + +/** * Submit handler for the user registration form. * * This function is shared by the installation form and the normal registration form, @@ -3583,13 +3660,21 @@ function user_register_submit($form, &$form_state) { } $notify = !empty($form_state['values']['notify']); + // Remove unneeded values. form_state_values_clean($form_state); $form_state['values']['pass'] = $pass; $form_state['values']['init'] = $form_state['values']['mail']; $account = $form['#user']; - $account = user_save($account, $form_state['values']); + + entity_form_submit_build_entity('user', $account, $form, $form_state); + + // Populate $edit with the properties of $account, which have been edited on + // this form by taking over all values, which appear in the form values too. + $edit = array_intersect_key((array) $account, $form_state['values']); + $account = user_save($account, $edit); + // Terminate if an error occurred during user_save(). if (!$account) { drupal_set_message(t("Error saving user account."), 'error');