I am working on a Drupal 7 site and for a few days now, I've been struggling with this problem: I am trying to create a custom template for the edit profile form, but there are more profile types. I managed to create the template but it does not work properly. I am able to save all the fields except the image field. When I hit remove button, the filed disappears completely, then I have to hit save two times for the image to be actually removed. Also, if I add an image, then press upload, I get a warning:
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'profile2_form' not found or invalid function name in drupal_retrieve_form() (line 841 of /var/www/html/hxl_drupal/includes/form.inc).)
If I hit save twice, the image gets saved.
I tried a lot of websites, including these:
- https://stackoverflow.com/questions/9548116/drupal-7-load-profile2-programmatically
- Profile2 Template for edit page which were the closest ones to my issue.
I should also mention that the form is submitted through AJAX and that I use ctools module to open it in modal.
Below you can see the code.
template.php
/*
* Implements hook_form_alter()
*/
function customtheme_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_profile_form') {
if ($form['#user_category'] === 'partner_user') {
$form['#theme'] = 'profile2_partner_user_edit_form';
} else if ($form['#user_category'] === 'advisor_user') {
$form['#theme'] = 'profile2_advisor_user_edit_form';
}
}
}
/*
*implements hook_theme()
*/
function customtheme_theme() {
return array(
'profile2_partner_user_edit_form' => array(
'arguments' => array(
'form' => NULL,
),
'template' => 'templates/profile2--partner-user-edit',
'render element' => 'form',
),
'profile2_advisor_user_edit_form' => array(
'arguments' => array(
'form' => NULL,
),
'template' => 'templates/profile2--advisor-user-edit',
'render element' => 'form'
),
);
}
custom_module.module
/**
* hook_menu
*/
function custom_module_menu() {
$items['profile/user-advisor_user_edit/%user/%ctools_js'] = array(
'page callback' => 'profile_advisor_user_edit',
'page arguments' => array(2,3),
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'file path' => drupal_get_path('module', 'user'),
'file' => 'user.pages.inc',
);
$items['profile/user-partner_user_edit/%user/%ctools_js'] = array(
'page callback' => 'profile_partner_user_edit',
'page arguments' => array(2,3),
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'file path' => drupal_get_path('module', 'user'),
'file' => 'user.pages.inc',
);
return $items;
}
I will post only the code for this profile type, because except the key word advisor_user.
function profile_advisor_user_edit($user, $js = FALSE ){
if ($js){
ctools_include('user.pages', 'user', '');
ctools_include('modal');
ctools_include('ajax');
$form_state = array(
'ajax' => TRUE,
'title' => t('Edit Profile'),
'user_id' => $user,
);
$form_state['build_info'] = array(
'args' => array($user),
);
$output = ctools_modal_form_wrapper('profile2_advisor_user_edit_form', $form_state);
if (!empty($form_state['executed'])) {
$output = array();
$output[] = ctools_modal_command_dismiss();
}
print ajax_render($output);
exit;
}else{
module_load_include('inc', 'user', 'user.pages');
$user_edit_form = drupal_get_form('profile2_advisor_user_edit_form',$user);
return drupal_render($user_edit_form);
}
}
The profile2--advisor-user-edit.tpl.php
file I will post the code only for this profile type because they are pretty much the same.
module_load_include('inc', 'profile2_page', 'profile2_page'); // Include profile2_page module
$profile2 = profile2_by_uid_load($user->uid, 'advisor_user');
$entity_form = entity_ui_get_form('profile2', $profile2, 'edit'); // Or 'add'
print render($entity_form['profile_advisor_user']['field_picture']);
print render($entity_form['profile_advisor_user']['field_first_name']);
print render($entity_form['profile_advisor_user']['field_last_name']);
print render($entity_form['profile_advisor_user']['field_role']);
print render($entity_form['profile_advisor_user']['field_bio']);
print render($entity_form['profile_advisor_user'] ['field_interest_topics']);
print render($entity_form['profile_advisor_user']['field_interest_areas']);
unset($entity_form['extra_fields']) //here I unset the fields that I don't want to appear in the form
print render($entity_form);