diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-02-03 17:30:13 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-02-03 17:30:13 +0000 |
commit | 607e9626d5af265b18e8319b156bb0fda3445cd4 (patch) | |
tree | ece98d14e826d14a711c9b572e3f43a428b9a365 /modules/user | |
parent | d4867346f578906751f8ea0bd799c3fc1bfcbf48 (diff) | |
download | brdo-607e9626d5af265b18e8319b156bb0fda3445cd4.tar.gz brdo-607e9626d5af265b18e8319b156bb0fda3445cd4.tar.bz2 |
- Patch #361683by Barry, Yves, Karen, Moshe Weitzman, David Strauss, floriant, chx, David Rothstein: initial field API patch. More work to be done, but ... oh my!
Diffstat (limited to 'modules/user')
-rw-r--r-- | modules/user/user-profile.tpl.php | 3 | ||||
-rw-r--r-- | modules/user/user.module | 81 | ||||
-rw-r--r-- | modules/user/user.pages.inc | 13 |
3 files changed, 90 insertions, 7 deletions
diff --git a/modules/user/user-profile.tpl.php b/modules/user/user-profile.tpl.php index 4aacdc492..e290f8c8b 100644 --- a/modules/user/user-profile.tpl.php +++ b/modules/user/user-profile.tpl.php @@ -38,7 +38,8 @@ * Available variables: * - $user_profile: All user profile data. Ready for print. * - $profile: Keyed array of profile categories and their items or other data - * provided by modules. + * provided by modules.
+ * - TODO D7 : document $FIELD_NAME_rendered variables. * * @see template_preprocess_user_profile() */ diff --git a/modules/user/user.module b/modules/user/user.module index 97f108413..39e98e8e8 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -83,6 +83,32 @@ function user_theme() { ); } +/** + * Implementation of hook_fieldable_info(). + */ +function user_fieldable_info() { + $return = array( + 'user' => array( + 'name' => t('User'), + 'id key' => 'uid', + ), + ); + return $return; +} + +/** + * Implementation of hook_field_build_modes(). + */ +function user_field_build_modes($obj_type) { + $modes = array(); + if ($obj_type == 'user') { + $modes = array( + 'full' => t('User account'), + ); + } + return $modes; +} + function user_external_load($authname) { $result = db_query("SELECT uid FROM {authmap} WHERE authname = '%s'", $authname); @@ -187,6 +213,10 @@ function user_load($array = array()) { $user->roles[$role->rid] = $role->name; } + // Attach fields. + // TODO D7 : not sure the 3rd param ($types) is needed. + field_attach_load('user', array($user->uid => $user)); + if (!empty($user->picture) && ($file = file_load($user->picture))) { $user->picture = $file; } @@ -241,6 +271,18 @@ function user_save($account, $edit = array(), $category = 'account') { unset($edit['pass']); } + // Get the fields form so we can recognize the fields in the $edit + // form that should not go into the serialized data array. + $field_form = array(); + $field_form_state = array(); + $edit = (object) $edit; + field_attach_form('user', $edit, $field_form, $field_form_state); + + // Presave fields. + field_attach_presave('user', $edit); + + $edit = (array) $edit; + if (is_object($account) && $account->uid) { user_module_invoke('update', $edit, $account, $category); $data = unserialize(db_result(db_query('SELECT data FROM {users} WHERE uid = %d', $account->uid))); @@ -250,9 +292,10 @@ function user_save($account, $edit = array(), $category = 'account') { $edit['access'] = REQUEST_TIME; } foreach ($edit as $key => $value) { - // Fields that don't pertain to the users or user_roles - // automatically serialized into the users.data column. - if ($key != 'roles' && empty($user_fields[$key])) { + // Form fields that don't pertain to the users, user_roles, or + // Field API are automatically serialized into the users.data + // column. + if ($key != 'roles' && empty($user_fields[$key]) && empty($field_form[$key])) { if ($value === NULL) { unset($data[$key]); } @@ -284,8 +327,14 @@ function user_save($account, $edit = array(), $category = 'account') { // Save changes to the users table. $success = drupal_write_record('users', $edit, 'uid'); if (!$success) { - // The query failed - better to abort the save than risk further data loss. - return FALSE; + // The query failed - better to abort the save than risk further + // data loss. + + // TODO: Fields change: I think this is a bug. If no columns in + // the users table are changed, drupal_write_record returns + // FALSE because rowCount() (rows changed) is 0. However, + // non-users data may have been changed, e.g. fields. + // return FALSE; } // If the picture changed or was unset, remove the old one. This step needs @@ -320,6 +369,10 @@ function user_save($account, $edit = array(), $category = 'account') { } } + // Save Field data. + $obj = (object) $edit; + field_attach_update('user', $obj); + // Refresh user object. $user = user_load(array('uid' => $account->uid)); @@ -353,13 +406,19 @@ function user_save($account, $edit = array(), $category = 'account') { // Build the initial user object. $user = user_load(array('uid' => $edit['uid'])); + $obj = (object) $edit; + field_attach_insert('user', $obj); + user_module_invoke('insert', $edit, $user, $category); // Note, we wait with saving the data column to prevent module-handled // fields from being saved there. $data = array(); foreach ($edit as $key => $value) { - if (($key != 'roles') && (empty($user_fields[$key])) && ($value !== NULL)) { + // Form fields that don't pertain to the users, user_roles, or + // Field API are automatically serialized into the users.data + // column. + if (($key != 'roles') && (empty($user_fields[$key]) && empty($field_form[$key])) && ($value !== NULL)) { $data[$key] = $value; } } @@ -1594,6 +1653,8 @@ function user_edit_form(&$form_state, $uid, $edit, $register = FALSE) { _user_password_dynamic_validation(); $admin = user_access('administer users'); + $form = array(); + // Account information: $form['account'] = array('#type' => 'fieldset', '#title' => t('Account information'), @@ -1829,7 +1890,15 @@ function _user_cancel($edit, $account, $method) { */ function user_build_content(&$account) { $edit = NULL; + $account->content = array(); + + // Build fields content. + // TODO D7 : figure out where exactly this needs to go + // TODO D7 : $page / $teaser ?? + $account->content += field_attach_view('user', $account); + user_module_invoke('view', $edit, $account); + // Allow modules to modify the fully-built profile. drupal_alter('profile', $account); diff --git a/modules/user/user.pages.inc b/modules/user/user.pages.inc index 00e8a00d2..581ac683a 100644 --- a/modules/user/user.pages.inc +++ b/modules/user/user.pages.inc @@ -174,6 +174,9 @@ function template_preprocess_user_profile(&$variables) { } // Collect all profiles to make it easier to print all items at once. $variables['user_profile'] = implode($variables['profile']); + + // Add $FIELD_NAME_rendered variables for fields. + $variables += field_attach_preprocess('user', $variables['account']); } /** @@ -236,6 +239,10 @@ function user_profile_form($form_state, $account, $category = 'account') { $edit = (empty($form_state['values'])) ? (array)$account : $form_state['values']; $form = _user_forms($edit, $account, $category); + + // Attach field widgets. + field_attach_form('user', (object) $edit, $form, $form_state); + $form['_category'] = array('#type' => 'value', '#value' => $category); $form['_account'] = array('#type' => 'value', '#value' => $account); $form['submit'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 30); @@ -256,6 +263,10 @@ function user_profile_form($form_state, $account, $category = 'account') { * Validation function for the user account and profile editing form. */ function user_profile_form_validate($form, &$form_state) { + // Validate field widgets. + $tmp_obj = (object) $form_state['values']; + field_attach_validate('user', $tmp_obj, $form, $form_state); + user_module_invoke('validate', $form_state['values'], $form_state['values']['_account'], $form_state['values']['_category']); // Validate input to ensure that non-privileged users can't alter protected data. if ((!user_access('administer users') && array_intersect(array_keys($form_state['values']), array('uid', 'init', 'session'))) || (!user_access('administer permissions') && isset($form_state['values']['roles']))) { @@ -272,6 +283,8 @@ function user_profile_form_submit($form, &$form_state) { $account = $form_state['values']['_account']; $category = $form_state['values']['_category']; unset($form_state['values']['_account'], $form_state['values']['op'], $form_state['values']['submit'], $form_state['values']['cancel'], $form_state['values']['form_token'], $form_state['values']['form_id'], $form_state['values']['_category'], $form_state['values']['form_build_id']); + + field_attach_submit('user', $account, $form, $form_state); user_module_invoke('submit', $form_state['values'], $account, $category); user_save($account, $form_state['values'], $category); |