5

Since all of the Profile2 registration forms have the same id 'user_register_form' (even when using Profile2_RegPath module) how can I specify a Profile2 type when calling:

print(drupal_render(drupal_get_form('user_register_form')));

I need to be able to let the user create an account without having him leave the page. I'm able to print the Register and The Profile Fields to be edited, but they each have their own Action button which is what I do not want.

  • Are you creating different user registration page for different user role and which version of drupal you are using? – Jayendra Kainthola Apr 30 '13 at 5:23
  • Profile2 is for Drupal 7. What I personally want to do is make a login/register popup with ctools where I load the regular login form and the register form for a particular profile 2 type. – ipwa May 2 '13 at 23:53
7
+25

After looking through the Profile2 Git repository viewer for v1.3. I believe you want to do something like the following using profile2_attach_form. The `profile2_attach_form has the following API comment and function signature:

 /**
 * Attaches the profile forms of the profiles set in
 * $form_state['profiles'].
 *
 * Modules may alter the profile2 entity form regardless to which form it is
 * attached by making use of hook_form_profile2_form_alter().
 *
 * @param $form
 *   The form to which to attach the profile2 form. For each profile the form
 *   is added to @code $form['profile_' . $profile->type] @endcode. This helper
 *   also adds in a validation and a submit handler caring for the attached
 *   profile forms.
 *
 * @see hook_form_profile2_form_alter()
 * @see profile2_form_validate_handler()
 * @see profile2_form_submit_handler()
 */
function profile2_attach_form(&$form, &$form_state) { /* ... */ }

So, you could use it as follows by slightly modifying your code:

$user_register_form = drupal_get_form('user_register_form');
$form_state['profiles'] = array(
    'CUSTOM_PROFILE_TYPE_NAME' => profile2_get_types('CUSTOM_PROFILE_TYPE_NAME')
);
profile2_attach_form($user_register_form, $form_state);
print(drupal_render($user_register_form));

If you need to tweak any profile entity form displayed in some of your registration page(s), you may want to implement the specific FORM_ID version of hook_form alter, hook_form_profile2_form_alter().

EDIT 01/15/2015

minor API clarification and code cleanup from comments.

  • This example is a little misleading in that profile2_attach_form() has no return so $registration_with_profile_field_forms will be empty. Also, profile2_attach_form() expects $form_state['profiles'] to be an array of objects, keyed by the profile machine names. Better to set $form_state['profiles']['<PROFILE_TYPE_NAME>'] = profile2_get_types('<PROFILE_TYPE_NAME'); – selfsimilar Jan 14 '15 at 21:10
  • I have updated the entry based upon your feedback. – tenken Jan 14 '15 at 21:27
  • Please don't confuse your own business logic with the original question. The original question wanted to include profile fields into a site registration form for new users (not user Edit profile pages). – tenken Jan 14 '15 at 22:52
2

It sounds like you want to use the Profile2_RegPath module. It lets you define custom registration paths for each profile type. It will load all of the profile fields & the normal user registration fields.

If you really want to do this programmatically take a look at their module for how they attach the profile fields to the user_register form. The function profile2_regpath_attach_profile_fields around line 438 is what does the bulk of the work.

0

This used to be done using the Webform Register module in D6, which integrated with Content Profile, the predecessor of Profile 2. You might look at the D6 module and see if you can upgrade it to D7. (Thay are looking for co-maintainers). The module seems to be stalled at D6 (check this issue).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.