Skip to content

Instantly share code, notes, and snippets.

@drupalista-br
Created December 6, 2012 09:23
Show Gist options
  • Save drupalista-br/4223201 to your computer and use it in GitHub Desktop.
Save drupalista-br/4223201 to your computer and use it in GitHub Desktop.
Drupal 7 Profile2 Custom User Tab Form ( MYMODULE_profile2_MYPROFILE2TYPE.inc )
<?php
/**
* @file
* User Profile2 MYPROFILE2TYPE.
*/
/**
* Page callback for user profile2 MYPROFILE2TYPE form.
*/
function MYMODULE_load_profile2_MYPROFILE2TYPE_form($form, &$form_state, $user = NULL) {
module_load_include('inc', 'profile2_page', 'profile2_page');
$profile2 = profile2_by_uid_load($user->uid, 'MYPROFILE2TYPE');
$form = entity_ui_get_form('profile2', $profile2, 'edit');
if (empty($form_state['profiles'])) {
$profile = profile2_load_by_user($user, 'MYPROFILE2TYPE');
if (empty($profile)) {
$profile = profile2_create(array('type' => 'MYPROFILE2TYPE', 'uid' => $user->uid));
}
$form_state['profiles'][$profile->type] = $profile;
profile2_attach_form($form, $form_state);
}
$form['#submit'] = array('MYMODULE_load_profile2_MYPROFILE2TYPE_form_submit');
$title = 'MY CUSTOM TITLE';
drupal_set_title($title);
return $form;
}
/**
* Submit handler that builds and saves profile2 MYPROFILE2TYPE form.
*/
function MYMODULE_load_profile2_MYPROFILE2TYPE_form_submit($form, &$form_state) {
foreach ($form_state['input']['profile_MYPROFILE2TYPE'] as $field_name => $field_value) {
if (isset($form_state['profiles']['MYPROFILE2TYPE']->{$field_name})) {
$form_state['profiles']['MYPROFILE2TYPE']->{$field_name} = $field_value;
}
}
// This is needed as some submit callbacks like user_register_submit() rely on
// clean form values.
profile2_form_submit_cleanup($form, $form_state);
profile2_save($form_state['profiles']['MYPROFILE2TYPE']);
drupal_set_message(t('The changes have been saved.'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment