0

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.

5
  • 3
    Can you post the code for your entire form? It's impossible to debug with just two lines of code. The example you've linked to contains no reference to a variable called $install_state, please also make it clear how that's involved. – Clive Jul 17 '13 at 11:07
  • Thanks, I've added the code. I hope it will help to explain my question. – Lynx Jul 17 '13 at 16:31
  • Certainly does :) Not sure if it will fix the problem completely, but you need to add global $install_state; to the add_another_team_member() function to access the global var – Clive Jul 17 '13 at 16:55
  • Thanks again for your help but unfortunately that doesn't solve the issue. It makes an infinite loop and inevitably finish with a Fatal error: Maximum execution time of 300 seconds exceeded in D:\Developpement\Websites\profile27\includes\common.inc on line 2379 – Lynx Jul 17 '13 at 23:48
  • I decided to try to add new field using #ajax. I though it will not have any page submit and should not go to the next step... Well... Ajax doesn't seems to work inside the installation profile... (I've created a module of my code first so I know that the code should run correctly... Any other idea ? – Lynx Jul 18 '13 at 23:11
0

This patch allows Ajax in install profiles https://drupal.org/node/1334818

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.