2

For my website, user accounts should only be created by the admin. However, the only fields that should be given by the admin are his full name. All other fields should be supplied by the user himself. For this, I have 2 things I would like to do:

1: Allow the admin to create a new user without supplying required fields.

2: Redirect the user to their profile edit page as long as there are required fields that have not been given.

For 2, I imagine it should be possible with the rules module, although I have not yet tried this. But is there a way to do 1? One thing I have considered is to add a custom page with a simple form to create the user, and to have custom PHP code that will actually create the user. However, I am hoping this is possible without resorting to that.

Edit: The only information I do have are an email, username and full name. All other fields, such as address, profile picture, etc. should be provided by the user themselves.

2
  • 1
    Once the user account has been created, how do you envision the actual person interacting with this account? Without an email address it will be impossible to send a one-time login link via email, so if the administrator adds an account with the name John Smith then how do you expect Mr Smith get informed that the account exists and log into it for the first time? Dec 9, 2015 at 10:06
  • I should have been more clear. The admin does know their email adress, full name and will create the username. Other information, however, such as address and birthdate, should be entered by the user themselves before they can continue using the site.
    – Erik S
    Dec 10, 2015 at 17:59

5 Answers 5

2
+50

It's not possible to create a new user without email, username and password.

The only workaround for this is to create placeholders. E.g. always enter [email protected] in the email field where username is the machine name of the user's full name.

As for creating the user without entering the required fields I think it's best to create a custom form. This form would only contain a textfield Full name.

On submit you dynamically generate the username, email and password (see drupal_clean_css_identifier to easily generate a machine name) and create the user object.

Here's a code example:

$full_name = $form_state['values']['full_name'];
$username = drupal_clean_css_identifier($full_name);
$email = $username . '@example.com';

//This will generate a random password, you could set your own here
$password = user_password(8);

//set up the user fields
$fields = array(
  'name' => $username,
  'mail' => $email,
  'pass' => $password,
  'status' => 1,
  'init' => 'email address',
  'roles' => array(
    DRUPAL_AUTHENTICATED_RID => 'authenticated user',
  ),
  // If you have custom field 'Full name' do this:
  'field_full_name' => array(
    LANGUAGE_NONE => array(
      0 => array(
        'value' => $full_name,
      ),
    ),
  ),
);

//the first parameter is left blank so a new user is created
$account = user_save('', $fields);
3
  • I do have access to the email and username, and the password can be generated. I've updated my question.
    – Erik S
    Dec 9, 2015 at 13:29
  • Apart from that: Would this be a seperate module, or could this be implemented with a default forms module?
    – Erik S
    Dec 9, 2015 at 13:30
  • I would create a new module for this. Dec 9, 2015 at 13:57
1

Your 1. use code in a custom module, to change the behaviour of the user_register form depending on whether the person using it is an administrator (at admin/people/create) or a regular user self-registering themselves.

As suggested at Seperate /user/register fields and admin/people/create something along the lines of:

function MYMODULE_form_user_register_form_alter(&$form, &$form_state) {

  // Is logged in user admin.
  if (user_access('administer users')) {

    // Hide myfield.
    $form['myfield']['#required'] = FALSE;
  }
}

Alternatively:

  • have your admins enter any old value for the required fields, and then use Rules to blank those fields when they have been created by an admin

  • create a new form using Webform or Form API, and change the "Add user" action to link to that form instead

Your 2. can be accomplished by the complete_profile module, or by Rules. See Force existing users to complete profile after adding required fields

2
  • I do have access to the email and username, and the password can be generated. I've updated my question.
    – Erik S
    Dec 9, 2015 at 13:29
  • I've edited my answer to reflect, but options I suggest are still the same
    – Jonathan
    Dec 9, 2015 at 15:35
0

If you already have email and username, then one solution would be :

  • set up your user fields as non required so you can create them easily with admin
  • add one Rule with conditions "If non-admin user log in, and if user fields are empty", and 2 actions : send message error "Please complete your profile in order to access website" and redirect to Profil Edit Page.
2
  • Good idea to redirect with rules! Would these be seperate rules for each required field, or might there be a way to do this in one go (if any required fields are empty, redirect)?
    – Erik S
    Dec 10, 2015 at 13:43
  • You should be able to add "AND" between conditions rules, so it's litterally something like: if (non-admin) && (field1==null) && (field2==null)... then do "add message" && "Redirect"
    – Mat
    Dec 10, 2015 at 14:07
0
  • set the rule to redirect to edit form if user logged in after creating account for user by admin without required fields.
  • edit form with required fields which has to be altered by custom form.
0

I have a simpler solution for you,

we have 2 form for user, user registration form and user edit form

Form Ids of them is

  • user_profile_form (for user edit)
  • user_register_form(for user register)

So what you need is your fields are not required by default but when in edit form ( user_profile_form) you need set them to required with powerful Drupal Form API

and solution form you is something like

function yourmodule_form_alter(&$form, &$form_state, $form_id)
{
    global $user;
    if ($form_id == "user_profile_form") {
       // ym($form);
       // you also can perform this action for specified roles 
        //if (!in_array('administrator', $user->roles))) {
          $form['field_mytest1']['#required'] = TRUE;
          $form['field_mytest2']['#required'] = TRUE;
            // hide other field with this method
        //}
    }

}

if getting required not work take look at this question and my answer there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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