Skip to content

Instantly share code, notes, and snippets.

@bdecarne
Last active December 15, 2015 21:59
Show Gist options
  • Save bdecarne/5329562 to your computer and use it in GitHub Desktop.
Save bdecarne/5329562 to your computer and use it in GitHub Desktop.
Drupal : Add profile2 form directly on the user edit form
<?php
/**
* Implements hook_menu_alter().
*/
function MY_MODULE_menu_alter(&$items) {
$items['user/%user_category/edit/profile']['access callback'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter() for the user edit form.
* Merge the profile2 form with the user account form itself :
*/
function MY_MODULE_form_user_profile_form_alter(&$form, &$form_state) {
if (($type = profile2_get_types('profile')) && $type->userCategory) {
if (empty($form_state['profiles'])) {
$profile = profile2_load_by_user($form['#user'], 'profile');
if (empty($profile)) {
$profile = profile_create(array('type' => 'profile', 'uid' => $form['#user']->uid));
}
$form_state['profiles'][$profile->type] = $profile;
}
profile2_attach_form($form, $form_state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment