0

I am writing a profile task which allows the installer to create a user account during the installation process. I have built a form for that task which uses the confirm_password field.

Unfortunately when I get to the task during the installation process, the form does not show the password strength bar or any other element that should be part of the password confirmation process.

However if I hit reload, then the form does show the confirm password fields properly. If the php form validation function is called, the resulting form display again shows the confirmation password fields properly. At this point I am at my wits end, why the password validation is not showing the JavaScript based elements upon initial load. Below is the code that I have written for this, and I desperately hope that someone can figure out what the devil is going on with this.

function development_install_tasks($install_state) {

    $tasks['development_create_default_owner_form'] = array(
        'display' => TRUE,
        'display_name' => st('Create Site Owner'),
        'type' => 'form',
    );

    return $tasks;

}

function development_create_default_owner_form($form, &$form_state, &$install_state) {

  drupal_set_title(st('Configure owner'));


  drupal_add_js(drupal_get_path('module', 'system') . '/system.js');
  drupal_add_js('misc/timezone.js');
  drupal_add_js(array('copyFieldValue' => array('edit-site-mail' => array('edit-account-mail'))), 'setting');
  drupal_add_js('jQuery(function () { Drupal.cleanURLsInstallCheck(); });', 'inline');
  drupal_add_js('jQuery(function () { Drupal.hideEmailAdministratorCheckbox() });', 'inline');
  menu_rebuild();

  drupal_get_schema(NULL, TRUE);

  // Return the form.
return _development_create_default_owner_form($form, $form_state, $install_state);

}


function _development_create_default_owner_form($form, &$form_state, &$install_state) {

    include_once DRUPAL_ROOT . '/includes/locale.inc';

    $form['owner_account'] = array(
        '#type' => 'fieldset',    
            '#title' => st('Site owner account'),
            '#collapsible' => FALSE,
        );

        $form['owner_account']['account']['#tree'] = TRUE;
        $form['owner_account']['account']['name'] = array(
            '#type' => 'textfield',
            '#title' => st('Username'),
            '#maxlength' => USERNAME_MAX_LENGTH,
            '#description' => st('Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.'),
            '#required' => TRUE,
            '#weight' => -10,
            '#attributes' => array('class' => array('username')),
        );

        $form['owner_account']['account']['mail'] = array(
            '#type' => 'textfield',
            '#title' => st('E-mail address'),
            '#maxlength' => EMAIL_MAX_LENGTH,
            '#required' => TRUE,
            '#weight' => -5,
        );
        $form['owner_account']['account']['pass'] = array(
            '#type' => 'password_confirm',
            '#required' => TRUE,
            '#size' => 25,
            '#weight' => 0,
            '#description' => st('To create a new user password, enter the new password in both fields.'),
        );

        $form['actions'] = array('#type' => 'actions');
        $form['actions']['submit'] = array(
            '#type' => 'submit',
            '#value' => st('Save and continue'),
            '#weight' => 15,
        );

        return $form;

    }



    function development_create_default_owner_form_validate($form, &$form_state) {
        if ($error = user_validate_name($form_state['values']['account']['name'])) {
            form_error($form['owner_account']['account']['name'], $error);
        }
        if ($error = user_validate_mail($form_state['values']['account']['mail'])) {
            form_error($form['owner_account']['account']['mail'], $error);
        }
    }


    function development_create_default_owner_form_submit($form, &$form_state) {

        // Setup the user account array to programatically create a new user.
        $account = array(
            'name' => $form_state['values']['account']['name'],
            'pass' => !empty($form_state['values']['account']['pass']) ? $form_state['values']['account']['pass'] : user_password(),
            'mail' => $form_state['values']['account']['mail'],
            'status' => 1,
            'init' => $form_state['values']['account']['mail'],
        );

        $account = user_save(null, $account);

        // Assign the client to the "owner" role.
        $role = user_role_load_by_name('administrator');
        db_insert('users_roles')
            ->fields(array('uid' => $account->uid, 'rid' => $role->rid))
            ->execute();
    }
0

Ok. I am not sure what went wrong, but after I shut down my server and restarted it, the code worked as advertised. The only explanation I have must be that the code was cached and doing a restart cleared it. I know that I had restarted the apache server multiple times, but for some strange reason that was not enough. Who knows. But rather than sheepishly deleting my own post, I figured I'd leave this available for anyone else who would like to reuse the code. Chalking this up to me being stupid.

-1

In hook_menu change value of page callback to drupal_get_form.

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.