content_profile_registration.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/content_profile/modules/content_profile_registration.module

Allows exposure and processing of content_profile node fields at user registration

Functions & methods

NameDescription
content_profile_registration_add_profile_formAdds in the profile form of the given content type to the registration form
content_profile_registration_admin_form_submit
content_profile_registration_alter_weightsPre render callback that makes sure our elements are grouped together. The ordering in between the single elements is kept.
content_profile_registration_content_profile_settingsImplementation of hook_content_profile_settings().
content_profile_registration_form_alterImplementation of hook_form_alter().
content_profile_registration_user_register_submitSubmits the user registration form
content_profile_registration_user_register_validateValidates the user registration form
_content_profile_registration_get_fieldsHelper function to return all available fields for a particular content type.
_content_profile_registration_get_field_selectReturns an array of selectable form elements that may be hidden, mostly containing CCK fields.
_content_profile_registration_remove_valuesRecursivly removes all form values created by this element or its children.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Allows exposure and processing of content_profile node fields at user registration
  5. */
  6. /**
  7. * Implementation of hook_form_alter().
  8. */
  9. function content_profile_registration_form_alter(&$form, &$form_state, $form_id) {
  10. if ($form_id == 'user_register') {
  11. require_once drupal_get_path('module', 'node') .'/node.pages.inc';
  12. // Allow other modules to customize the used profile types, so modules
  13. // can easily customize the registration form.
  14. $default_types = content_profile_get_types('names', (arg(0) == 'admin' ? 'admin_user_create_use' : 'registration_use'));
  15. $form += array('#content_profile_registration_use_types' => $default_types);
  16. foreach ($form['#content_profile_registration_use_types'] as $type => $typename) {
  17. content_profile_registration_add_profile_form($type, $form, $form_state);
  18. }
  19. }
  20. elseif ($form_id == 'content_profile_admin_settings') {
  21. $type = $form_state['type'];
  22. // Let other modules add registration child elements before us!
  23. $form += array('registration' => array());
  24. $form['registration'] += array(
  25. '#type' => 'fieldset',
  26. '#title' => t('User Registration'),
  27. '#description' => t('Customize how this content profile shows up on the user registration page.'),
  28. '#collapsible' => TRUE,
  29. );
  30. $form['registration']['registration_use'] = array(
  31. '#type' => 'checkbox',
  32. '#title' => t('Use on Registration'),
  33. '#description' => t('Use this content type on the user registration page'),
  34. '#default_value' => content_profile_get_settings($type, 'registration_use'),
  35. );
  36. $form['registration']['admin_user_create_use'] = array(
  37. '#type' => 'checkbox',
  38. '#title' => t('Use on administrative user creation form'),
  39. '#description' => t('Use this content type when an administrative user creates a new user'),
  40. '#default_value' => content_profile_get_settings($type, 'admin_user_create_use'),
  41. );
  42. $form['registration']['registration_hide'] = array(
  43. '#type' => 'checkboxes',
  44. '#title' => t('Hide form fields'),
  45. '#description' => t('Hide fields from the user registration form. Required fields cannot be hidden and are not shown here.'),
  46. '#options' => _content_profile_registration_get_field_select($type),
  47. '#default_value' => content_profile_get_settings($type, 'registration_hide'),
  48. );
  49. array_unshift($form['#submit'], 'content_profile_registration_admin_form_submit');
  50. }
  51. }
  52. function content_profile_registration_admin_form_submit($form, &$form_state) {
  53. $form_state['values']['registration_hide'] = array_keys(array_filter($form_state['values']['registration_hide']));
  54. }
  55. /**
  56. * Helper function to return all available fields for a particular content type.
  57. *
  58. * @param string $type
  59. * The content type to return fields for.
  60. * @return array
  61. * Associated fields for the given content type.
  62. */
  63. function _content_profile_registration_get_fields($type) {
  64. $typeinfo = content_types($type);
  65. return $typeinfo['fields'];
  66. }
  67. /**
  68. * Returns an array of selectable form elements that may be hidden, mostly containing CCK fields.
  69. *
  70. * @param string $type
  71. * A content type to get the select elements for.
  72. * @return Array
  73. * An array of fields suitable for use in a select field.
  74. */
  75. function _content_profile_registration_get_field_select($type) {
  76. $fields = module_exists('content') ? _content_profile_registration_get_fields($type) : array();
  77. $return = array();
  78. if (!module_exists('auto_nodetitle') || auto_nodetitle_get_setting($type) != AUTO_NODETITLE_ENABLED) {
  79. $return['title'] = t('Title');
  80. }
  81. foreach ($fields as $fieldname => $info) {
  82. if (!$info['required']) {
  83. $return[$fieldname] = drupal_ucfirst($info['widget']['label']);
  84. }
  85. }
  86. $return['other'] = t('Other form elements (except for required CCK fields)');
  87. return $return;
  88. }
  89. /**
  90. * Adds in the profile form of the given content type to the registration form
  91. *
  92. * @see content_profile_registration_alter_weights()
  93. * @see content_profile_registration_user_register_validate()
  94. * @see content_profile_registration_user_register_submit()
  95. */
  96. function content_profile_registration_add_profile_form($type, &$form, &$form_state) {
  97. // Initialize new node and add in its form.
  98. $node = array('uid' => 0, 'name' => '', 'type' => $type);
  99. // Get the original node form.
  100. $node_form = drupal_retrieve_form($type .'_node_form', $form_state, $node);
  101. drupal_prepare_form($type .'_node_form', $node_form, $form_state);
  102. $node_form += array('#field_info' => array());
  103. $form_add = array();
  104. // If non-CCK form elements are hidden, only copy over the CCK stuff
  105. if (in_array('other', content_profile_get_settings($type, 'registration_hide'))) {
  106. foreach ($node_form['#field_info'] as $field_name => $info) {
  107. if (isset($node_form[$field_name])) {
  108. $form_add[$field_name] = $node_form[$field_name];
  109. }
  110. }
  111. // Copy over any fieldgroups
  112. $keys = array_keys($node_form);
  113. foreach ($keys as $key) {
  114. if (stristr($key, 'group_')) {
  115. $form_add[$key] = $node_form[$key];
  116. }
  117. }
  118. // Add the title
  119. $form_add['title'] = $node_form['title'];
  120. // Set this to the values of one node, as it might be need by some #ahah callbacks
  121. $form_add['#node'] = $node_form['#node'];
  122. $form_add['type'] = $node_form['type'];
  123. }
  124. else {
  125. foreach (array('uid', 'name', 'author', 'buttons', 'language', '#theme', 'options') as $key) {
  126. unset($node_form[$key]);
  127. }
  128. $form_add = $node_form;
  129. }
  130. // Hide fields as configured
  131. foreach (content_profile_get_settings($type, 'registration_hide') as $field_name) {
  132. if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type, $field_name))) {
  133. unset($form_add[$group_name][$field_name]);
  134. if (count(element_children($form_add[$group_name])) == 0) {
  135. unset($form_add[$group_name]);
  136. }
  137. }
  138. else {
  139. unset($form_add[$field_name]);
  140. }
  141. }
  142. // Add in the new form elements into $form.
  143. $form += array('#field_info' => array());
  144. $form['#field_info'] += $node_form['#field_info'];
  145. $form += $form_add;
  146. // Add in further callbacks needed, if not yet done.
  147. if (!isset($form['#content_profile_weights'])) {
  148. $form['#submit'][] = 'content_profile_registration_user_register_submit';
  149. $form['#validate'][] = 'content_profile_registration_user_register_validate';
  150. $form['#pre_render'][] = 'content_profile_registration_alter_weights';
  151. }
  152. // Care for the weights: Make use of the content types weight and sort the fields in behalf
  153. // The weights will be applied by the pre_render callback.
  154. $form += array('#content_profile_weights' => array());
  155. $weight = content_profile_get_settings($type, 'weight') + 1;
  156. foreach (element_children($form_add) as $key) {
  157. $form['#content_profile_weights'] += array($key => $weight);
  158. }
  159. // Set the enctype, if necessary.
  160. if (isset($node_form['#attributes']['enctype'])){
  161. $form['#attributes']['enctype'] = $node_form['#attributes']['enctype'];
  162. }
  163. }
  164. /**
  165. * Pre render callback that makes sure our elements are grouped together.
  166. * The ordering in between the single elements is kept.
  167. *
  168. * @see content_profile_registration_add_profile_form().
  169. */
  170. function content_profile_registration_alter_weights($elements) {
  171. foreach ($elements['#content_profile_weights'] as $key => $weight) {
  172. if (isset($elements[$key]) && is_array($elements[$key])) {
  173. $elements[$key] += array('#weight' => 0);
  174. $elements[$key]['#weight'] = $weight + $elements[$key]['#weight'] / 1000;
  175. }
  176. }
  177. return $elements;
  178. }
  179. /**
  180. * Validates the user registration form
  181. */
  182. function content_profile_registration_user_register_validate($form, &$form_state) {
  183. require_once drupal_get_path('module', 'node') .'/node.pages.inc';
  184. foreach ($form['#content_profile_registration_use_types'] as $type => $typename) {
  185. $node = (object)$form_state['values'];
  186. $node->type = $type;
  187. node_object_prepare($node);
  188. // Make sure there is no user name so we can node_validate
  189. unset($node->name);
  190. if (!in_array('other', content_profile_get_settings($type, 'registration_hide'))) {
  191. node_validate($node, $form);
  192. }
  193. elseif (module_exists('content')) {
  194. content_validate($node, $form);
  195. }
  196. $form_state['content_profile_registration'][$type]['node'] = $node;
  197. }
  198. // Remove our added values
  199. foreach ($form['#content_profile_weights'] as $key => $weight) {
  200. _content_profile_registration_remove_values($key, $form[$key], $form_state);
  201. }
  202. }
  203. /**
  204. * Recursivly removes all form values created by this element or its children.
  205. */
  206. function _content_profile_registration_remove_values($key, $element, &$form_state) {
  207. if ((isset($element['#input']) || !empty($element['#tree'])) && isset($form_state['values'][$key])) {
  208. unset($form_state['values'][$key]);
  209. }
  210. elseif (empty($element['#input']) && empty($element['#tree'])) {
  211. foreach (element_children($element) as $key) {
  212. _content_profile_registration_remove_values($key, $element[$key], $form_state);
  213. }
  214. }
  215. }
  216. /**
  217. * Submits the user registration form
  218. */
  219. function content_profile_registration_user_register_submit($form, &$form_state) {
  220. foreach ($form['#content_profile_registration_use_types'] as $type => $typename) {
  221. if ($node = &$form_state['content_profile_registration'][$type]['node']) {
  222. // Set user's information for the node.
  223. if (empty($node->title) && (!module_exists('auto_nodetitle') || auto_nodetitle_get_setting($type) != AUTO_NODETITLE_OPTIONAL)) {
  224. $node->title = $form_state['user']->name;
  225. }
  226. $node->uid = $form_state['user']->uid;
  227. $node->name = $form_state['user']->name;
  228. // Create the node.
  229. $node = node_submit($node);
  230. node_save($node);
  231. // Give us a nice log message.
  232. if ($node->nid) {
  233. watchdog('content', 'Content Profile: added %user %type upon registration.', array('%user' => $node->name, '%type' => $type), WATCHDOG_NOTICE, l(t('view'), "node/$node->nid"));
  234. }
  235. }
  236. }
  237. }
  238. /**
  239. * Implementation of hook_content_profile_settings().
  240. */
  241. function content_profile_registration_content_profile_settings() {
  242. return array(
  243. 'registration_use' => FALSE,
  244. 'admin_user_create_use' => FALSE,
  245. 'registration_hide' => array(),
  246. );
  247. }