Nice work on this module. Here's a hack I had to make to get it to work for my situation. I notice other people with similar needs and thought I'd contribute the simple idea back.

Problem: I needed to display a specific content profile on the registration form based on custom URL formats.

Originally I tried the solution here: http://drupal.org/node/431710#comment-3274092 , but it wasn't working in my case. It probably had to do with the way the content type was created.

Solution: Added a new hook in the user_register part of hook_form_alter() in content_profile_registration.module which passes the $default_types by reference. Since module_invoke_all() does not support passing in by reference, I used the same code the user module uses for hook_user().

The comments in the form_alter hook that already existed threw me off. Maybe there is a way to achieve this already, but I didn't see how. The comment was: "Allow other modules to customize the used profile types, so modules can easily customize the registration form."

Here is my solution: Note the code between NEW CODE START and NEW CODE END

custom_profile_register.module
<?PHP
/**
* Implementation of hook_form_alter().
*/
function content_profile_registration_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_register') {
require_once drupal_get_path('module', 'node') .'/node.pages.inc';

// Allow other modules to customize the used profile types, so modules
// can easily customize the registration form.
$default_types = content_profile_get_types('names', (arg(0) == 'admin' ? 'admin_user_create_use' : 'registration_use'));

/** NEW CODE START **/
// Create hook_content_profile_reg_types
// Not using module_invoke_all so $default_types can be passed by reference (as done in the user module for hook_user)
foreach (module_list() as $module) {
$function = $module .'_content_profile_reg_types';
if (function_exists($function)) {
$function($default_types);
}
}
/** NEW CODE END **/

$form += array('#content_profile_registration_use_types' => $default_types);

foreach ($form['#content_profile_registration_use_types'] as $type => $typename) {
content_profile_registration_add_profile_form($type, $form, $form_state);
}
}
elseif ($form_id == 'content_profile_admin_settings') {
$type = $form_state['type'];
// Let other modules add registration child elements before us!
$form += array('registration' => array());
$form['registration'] += array(
'#type' => 'fieldset',
'#title' => t('User Registration'),
'#description' => t('Customize how this content profile shows up on the user registration page.'),
'#collapsible' => TRUE,
);
$form['registration']['registration_use'] = array(
'#type' => 'checkbox',
'#title' => t('Use on Registration'),
'#description' => t('Use this content type on the user registration page'),
'#default_value' => content_profile_get_settings($type, 'registration_use'),
);
$form['registration']['admin_user_create_use'] = array(
'#type' => 'checkbox',
'#title' => t('Use on administrative user creation form'),
'#description' => t('Use this content type when an administrative user creates a new user'),
'#default_value' => content_profile_get_settings($type, 'admin_user_create_use'),
);
$form['registration']['registration_hide'] = array(
'#type' => 'checkboxes',
'#title' => t('Hide form fields'),
'#description' => t('Hide fields from the user registration form. Required fields cannot be hidden and are not shown here.'),
'#options' => _content_profile_registration_get_field_select($type),
'#default_value' => content_profile_get_settings($type, 'registration_hide'),
);
array_unshift($form['#submit'], 'content_profile_registration_admin_form_submit');
}
}
?>

Now in my own module, I can load the specific content profile by URL or any logic I want.

Example:

/**
 * Implementation of hook_content_profile_reg_types().
 */
function mymodule_content_profile_reg_types(&$types) {
  // let's say we have 2 content types, content_type_foo and content_type_bar
  // and we only want to display content_type_foo if the url is /user/register/foo
  if (arg(2) == 'foo') {
    unset($types['content_type_bar']);
  }
  elseif (arg(2) == 'bar') {
    unset($types['content_type_foo']);
  }
}

Ultimately a setting in the content profile edit screen might be a more user friendly place for a textarea for either urls or custom php, but I think keeping a hook is a nice feature as well.

Comments

YK85’s picture

subscribing

MVRider’s picture

I like this feature it helped greatly buy making content profile more flexible.