0

I am using Installation Profiles to automate the process of installing Drupal 7 websites

I would like to create a user via the installation profile and assign it a username, password and email address, plus give it a role (This will be a special administrative user that I want every website to have).

How can I do this? This is the closest tutorial I can find. However, it requires you to set a username/password and email address manually. I would like to store a default values for all these fields.

2

If you still want a configuration form, then you can build upon the "Client step" and change the function brochure_client_form_submit($form, &$form_state) so that it suits your needs (all code is untested):

function brochure_client_form_submit($form, &$form_state) {

  // Setup the user account array to programatically create a new user.
  $account = array(
    'name' => YOUR_NAME,
    'pass' => YOUR_PASS,
    'mail' => YOUR_MAIL,
    'status' => 1,
    'init' => 'install profile', // Just an example
  );
  $account = user_save(null, $account);
}

If you don't need a separate configuration form during install, you can define a "formless" task like this:

/**
 * Implements hook_install_tasks().
 */
function mymodule_install_tasks() {
  $tasks['mymodule_setup_admin_account'] = array(
    'display_name' => st('Setup admin account'),
  );
  return $tasks;
}

function mymodule_setup_admin_account() {
  // Setup the user account array to programatically create a new user.
  $account = array(
    'name' => YOUR_NAME,
    'pass' => YOUR_PASS,
    'mail' => YOUR_MAIL,
    'status' => 1,
    'init' => 'install profile', // Just an example
  );
  $account = user_save(null, $account);
}

You still have to create the role and add it to the created user. If you have created the role prior to the user creation, then you can also pass it directly to user_save:

$account = array(
  'name' => YOUR_NAME,
  'pass' => YOUR_PASS,
  'mail' => YOUR_MAIL,
  'status' => 1,
  'init' => 'install profile', // Just an example
  'roles' => array(
    ROLE_ID => ROLE_ID,
  ),
);
$account = user_save(null, $account);

More on install profiles: How to Write a Drupal 7 Installation Profile

  • Thanks! Where does the formless task code go? In .install or .profile? – big_smile Feb 8 '14 at 19:21
  • @big_smile In the .profile. – berliner Feb 8 '14 at 19:23
  • Thanks, I tried this but get this error during install SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'ROLE_ID' for column 'rid' at row 1 – big_smile Feb 9 '14 at 18:47
  • @big_smile You have to replace ROLE_ID with the internal role id of the role that you want to give to the user. See this thread for an example: drupal.org/node/312919 – berliner Feb 10 '14 at 9:35

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.