I need to set up multiple user in the last step of my install profile. My idea was to create a form like form_example_tutorial_9
But every time I click on the add_teammember button my form is submit and go to the last task which display the link "Visit your site". Although the user as been created I'm unable to display the form again in order to add other team members
So here is my code in the .profile file:
**
* Implements hook_install_tasks().
*/
function drupal_base_install_tasks() {
$tasks = array(
'team_members_form' => array(
'display_name' => st('Team members'),
'type' => 'form',
),
);
return $tasks;
}
function team_members_form($form, &$form_state) {
$form['#tree'] = TRUE;
$form['description'] = array(
'#type' => 'item',
'#title' => st('Setup team members accounts'),
);
if (empty($form_state['num_members'])) {
$form_state['num_members'] = 1;
}
// Build the number of member fieldsets indicated by $form_state['num_members']
for ($i = 1; $i <= $form_state['num_members']; $i++) {
$form['member'][$i] = array(
'#type' => 'fieldset',
'#title' => st('Member #@num', array('@num' => $i)),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['member'][$i]['name'] = array(
'#type' => 'textfield',
'#title' => st('Name'),
'#description' => st("Enter member's name."),
'#size' => 20,
'#maxlength' => 20,
'#required' => TRUE,
);
$form['member'][$i]['email'] = array(
'#type' => 'textfield',
'#title' => st("Enter member's email"),
'#required' => TRUE,
);
$form['member'][$i]['password'] = array(
'#type' => 'textfield',
'#title' => "Enter member's password",
'#required' => TRUE,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save and continue',
);
// Adds "Add another member" button
$form['add_members'] = array(
'#type' => 'submit',
'#value' => st('Add another member'),
'#submit' => array('add_another_team_member'),
);
// If we have more than one member, this button allows removal of the
// last member.
if ($form_state['num_members'] > 1) {
$form['remove_member'] = array(
'#type' => 'submit',
'#value' => st('Remove latest member'),
'#submit' => array('remove_latest_team_member'),
// Since we are removing a member, don't validate until later.
'#limit_validation_errors' => array(),
);
}
return $form;
}
function team_members_form_validate(&$form, &$form_state) {
for ($i = 1; $i <= $form_state['num_members']; $i++) {
if (!valid_email_address($form_state['values']['member'][$i]['email'])) {
form_set_error('email', t('Please enter a valid email address'));
}
}
}
function team_members_form_submit($form, &$form_state) {
$values = $form_state['values'];
for ($i = 1; $i <= $form_state['num_members']; $i++) {
$account = array(
'name' => $values['member'][$i]['name'],
'pass' => !empty($values['member'][$i]['password']) ? $values['member'][$i]['password'] : user_password(),
'mail' => $values['member'][$i]['email'], 'status' => 1,
'init' => $values['member'][$i]['email'],
);
$account = user_save(null, $account);
// Assign the member of the team to the "administrator" role.
$role = user_role_load_by_name('administrator');
db_insert('users_roles')
->fields(array('uid' => $account->uid, 'rid' => $role->rid))
->execute();
}
}
function add_another_team_member($form, &$form_state) {
//reload the same step
//$install_state['task_not_complete'] = TRUE;
$form_state['num_members']++;
$install_state['task_not_complete'] = TRUE;
return drupal_render($form);
}
function remove_latest_team_member($form, &$form_state) {
if ($form_state['num_members'] > 1) {
$form_state['num_members']--;
}
$form_state['rebuild'] = TRUE;
}
My question:
Inside the function add_another_team_member, I don't want the installation to go to the next step but I want it to display the form again. That's the reason I used $install_state['task_not_complete'] = TRUE. But the installation continue to the next and final step anyway.
Thanks.
$install_state
, please also make it clear how that's involved. – Clive♦ Jul 17 '13 at 11:07global $install_state;
to theadd_another_team_member()
function to access the global var – Clive♦ Jul 17 '13 at 16:55