Create custom editable user profile page with drupal core validation for fields

Public

Add
first_name
last_name
phone
cck fields to user account

Get raw version
php
  1. // Put this code on your mymodulename.module file and replace userprofile with your module name
  2. <?php
  3. function userprofile_menu() {
  4.  
  5. $items['profile'] = array(
  6. 'title' => t('My Profile'),
  7. 'page callback' => 'drupal_get_form',
  8. 'page arguments' => array('_userprofile_get_user_profile', 1),
  9. 'access callback' => 'user_is_logged_in',
  10. 'file' => 'userprofile.inc',
  11. 'file path' => drupal_get_path('module', 'userprofile') . '/includes',
  12. );
  13.  
  14. return $items;
  15. }
  16. ?>
  17. // put this code inside mymodulename/includes/mymodulename.inc
  18. // do not forget to replace userprofile with mymodulename
  19. <?php
  20.  
  21. function _user_profile_data($current_user, $op) {
  22. switch ($op) {
  23. case 'first_name':
  24. if (isset($current_user->field_first_name['und'][0]['value'])) {
  25. return $current_user->field_first_name['und'][0]['value'];
  26. }
  27. else {
  28. return '';
  29. }
  30. case 'last_name':
  31. if (isset($current_user->field_last_name['und'][0]['value'])) {
  32. return $current_user->field_last_name['und'][0]['value'];
  33. }
  34. else {
  35. return '';
  36. }
  37. case 'phone':
  38. if (isset($current_user->field_phone['und'][0]['value'])) {
  39. return $current_user->field_phone['und'][0]['value'];
  40. }
  41. else {
  42. return '';
  43. }
  44. }
  45. }
  46.  
  47. function _userprofile_get_user_profile($form, $form_state) {
  48.  
  49. global $user;
  50. $current_user = user_load($user->uid);
  51. $register = ($user->uid > 0 ? FALSE : TRUE);
  52.  
  53. $form['about_me'] = array(
  54. '#type' => 'markup',
  55. '#markup' => "About Me",
  56. '#prefix' => '<div id="about_me_text">',
  57. '#suffix' => '</div>',
  58. );
  59.  
  60. // Picture/avatar.
  61. $form['picture'] = array(
  62. '#type' => 'container',
  63. '#access' => (!$register && variable_get('user_pictures', 0)),
  64. );
  65. $form['picture']['picture'] = array(
  66. '#type' => 'value',
  67. '#value' => isset($current_user->picture) ? $current_user->picture : NULL,
  68. '#prefix' => '<div id="user_picture">',
  69. '#suffix' => '</div>',
  70. );
  71. $form['picture']['picture_current'] = array(
  72. '#markup' => theme('user_picture', array('account' => $current_user)),
  73. );
  74. $form['picture']['picture_upload'] = array(
  75. '#type' => 'file',
  76. '#title' => t('Replace picture'),
  77. '#size' => 10,
  78. '#prefix' => '<div id="user_picture_upload">',
  79. '#suffix' => '</div>',
  80. );
  81.  
  82. $form['#validate'][] = 'user_validate_picture';
  83.  
  84. $form['user_details'] = array(
  85. '#type' => 'container',
  86. '#collapsible' => false,
  87. '#collapsed' => false,
  88. );
  89. $form['user_details']['first_name'] = array(
  90. '#title' => 'First Name',
  91. '#type' => 'textfield',
  92. '#default_value' => _user_profile_data($current_user, 'first_name'),
  93. '#size' => 40,
  94. '#prefix' => '<div id="first_name">',
  95. '#suffix' => '</div>',
  96. );
  97.  
  98. $form['user_details']['last_name'] = array(
  99. '#title' => 'Last Name',
  100. '#type' => 'textfield',
  101. '#size' => 40,
  102. '#default_value' => _user_profile_data($current_user, 'last_name'),
  103. '#prefix' => '<div id="last_name">',
  104. '#suffix' => '</div>',
  105. );
  106.  
  107. $form['user_details']['email'] = array(
  108. '#title' => 'Email',
  109. '#type' => 'textfield',
  110. '#size' => 40,
  111. '#maxlength' => EMAIL_MAX_LENGTH,
  112. '#default_value' => $current_user->mail,
  113. '#prefix' => '<div id="user_mail">',
  114. '#suffix' => '</div>',
  115. );
  116. $form['user_details']['phone'] = array(
  117. '#title' => 'Phone',
  118. '#type' => 'textfield',
  119. '#size' => 40,
  120. '#default_value' => _user_profile_data($current_user, 'phone'),
  121. '#prefix' => '<div id="user_phone">',
  122. '#suffix' => '</div>',
  123. );
  124.  
  125. $form['user_details']['password'] = array(
  126. '#type' => 'password_confirm',
  127. '#size' => 40,
  128. '#prefix' => '<div id="user_pass">',
  129. '#suffix' => '</div>',
  130. );
  131.  
  132. $form['user_details']['user_profile_submit_form'] = array(
  133. '#type' => 'submit',
  134. '#value' => t('Update'),
  135. '#prefix' => '<div id="user_details_submit">',
  136. '#suffix' => '</div>',
  137. );
  138. $form['#validate'][] = '_userprofile_validate_user_profile';
  139. $form['#submit'][] = '_userprofile_save_user_profile';
  140. return $form;
  141. }
  142.  
  143. function _userprofile_validate_user_profile($form, $form_state) {
  144. global $user;
  145. $account = user_load($user->uid);
  146. // Trim whitespace from mail, to prevent confusing 'e-mail not valid'
  147. // warnings often caused by cutting and pasting.
  148. $mail = trim($form_state['values']['email']);
  149. form_set_value($form['user_details']['email'], $mail, $form_state);
  150. // Validate the e-mail address, and check if it is taken by an existing user.
  151. if ($error = user_validate_mail($form_state['values']['email'])) {
  152. form_set_error('email', $error);
  153. }
  154. elseif ((bool) db_select('users')->fields('users', array('uid'))->condition('uid', $account->uid, '<>')->condition('mail', db_like($form_state['values']['email']), 'LIKE')->range(0, 1)->execute()->fetchField()) {
  155. // Format error message dependent on whether the user is logged in or not.
  156. if ($GLOBALS['user']->uid) {
  157. form_set_error('mail', t('The e-mail address %email is already taken.', array('%email' => $form_state['values']['email'])));
  158. }
  159. else {
  160. form_set_error('mail', t('The e-mail address %email is already registered. <a href="@password">Have you forgotten your password?</a>', array('%email' => $form_state['values']['email'], '@password' => url('user/password'))));
  161. }
  162. }
  163. }
  164.  
  165. function _userprofile_save_user_profile($form, $form_state) {
  166. //echo '<pre>';print_r($form_state);die("test");
  167. global $user;
  168. $current_user = user_load($user->uid);
  169.  
  170. if ($form_state['values']['password'] != '') {
  171. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  172. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  173. require_once DRUPAL_ROOT . '/includes/password.inc';
  174. $pass_hash = user_hash_password($form_state['values']['password']);
  175. $current_user->pass = $pass_hash;
  176. }
  177. if ($form_state['values']['picture_upload'] != '') {
  178. $current_user->picture = $form_state['values']['picture_upload'];
  179. }
  180. $current_user->field_first_name['und'][0]['value'] = $form_state['values']['first_name'];
  181. $current_user->field_last_name['und'][0]['value'] = $form_state['values']['last_name'];
  182. $current_user->mail = $form_state['values']['email'];
  183. $current_user->field_phone['und'][0]['value'] = $form_state['values']['phone'];
  184. user_save($current_user, array($current_user));
  185. }
  186. ?>
  187.  
  188. // Put this code under mymodulename/css/style.css
  189. #edit-picture {
  190. float: left;
  191. width: 110px;
  192. }
  193.  
  194. #edit-user-details {
  195. float: right;
  196. }
  197.  
  198. //Now clear the cache and visit yoursitename/profile

Comments

ooops's picture

css I have put there is as per bartik theme, you have to change it your way.

nitish's picture

how to save a date field using popup in editable user profile page