I'm using drupal 7.
All i want to do is adding a submit handler for user_profile_form. the following is what i do:

function arkitheme_menu()
{
	$items['user/selectrole/individual'] = array(  
		'title' => 'Select Account Type',
		'description' => 'Select Account Type', 
		'page callback' => 'arkitheme_user_select_role_individual',
		'access callback' => 'user_access',
		'access arguments' => array('access content'), 
		'type' => MENU_CALLBACK
	);
}
function arkitheme_user_select_role_individual()
{
	global $user;
	$form_id = 'user_profile_form';
	$form_state = array();
	$form_state['build_info']['args'] = array(0=>user_load($user->uid));

	form_load_include($form_state, 'inc', 'user', 'user.pages');

	$form = drupal_build_form($form_id, $form_state);
	$form['#action'] = '/user/selectrole/individual';
	$form['#submit'][] = "arkitheme_user_add_role_individual";
	
	return $form;
}
//the following function never get called....
function arkitheme_user_add_role_individual($form, &$form_state)
{
	drupal_set_message("callllllllllllllllllllllllllllllllllled");
	global $user;
	$uid = $user->uid;// User ID of user that you want to add role to.
	$role_name = 'individual'; // The name of the role to add.
	if ($role = user_role_load_by_name($role_name)) {
		user_multiple_role_edit(array($uid), 'add_role', $role->rid);
	}
}

function arkitheme_user_add_role_individual is never get called.
Please help me...

Comments

jaypan’s picture

Add your submit handler to the submit button, rather than the entire form.

Contact me to contract me for D7 -> D10/11 migrations.

baiyuxiong’s picture

My code now looks like this:

	$form = drupal_build_form($form_id, $form_state);
	$form['#action'] = '/user/selectrole/individual?destination=user/selectrole/getstarted';

	$form['#submit'][] = "arkitheme_user_add_role_individual";
	$form['actions']['submit']['#submit'][] = "arkitheme_user_add_role_individual";
	$form['submit']['#submit'][] = "arkitheme_user_add_role_individual";

	return $form;

Still not working.

jaypan’s picture

Sorry, I didn't look too closely at your original code the first time. The problem here is that you are trying to add your submit handler in the wrong place. Submit handlers need to be added in hook_form_alter().

First, in your page callback, you can call drupal_get_form()

function arkitheme_user_select_role_individual()
{
  global $user;

  return drupal_get_form('user_profile_form', $user);
}

This will generate the user_profile_form, passing it the global $user object, meaning the form shown will always be the profile form for the current user.

Next, you add your additional code in hook_form_alter:

function arkitheme_form_alter(&$form, &$form_state, $form_id)
{
  if($form_id == 'user_profile_form')
  {
    $form = drupal_build_form($form_id, $form_state);
    $form['actions']['submit']['#submit'][] = "arkitheme_user_add_role_individual";
  }
}

This will add your submit handler to the user_profile_form. If you need to add conditions as to when to add your submit handler, you will want to do it here.

Contact me to contract me for D7 -> D10/11 migrations.

baiyuxiong’s picture

Many thanks.
It works now.
All code and logic seems correct and reasonable, But not working, I called it a "drupal trap". It is what always driving me crazy!

dtamajon’s picture

Hi! I'm interested in this solution, but when I apply it the hook_form_alter is called in a loop, as it looks that drupal_build_form fires the hook again.

If I don't us the call to drupal_build_form, then my submit action is not modified.

I know it is a lot of time since this question resolution, but any help will be really appreciated.