4

I need to auto-assign newly autocreated users from the content editing form a particular role.

I created this new content-type : Music Track and created a new field - Artist for it. The Artist field is entity referenced to Target type - USER. I was expecting a Roles list under Entity Selection area so that I can restrict only Artist role users to be created when creating a new content and adding new artists but its not available.

Note: Users are being autocreated using Entityreference Autocreate module.

2

Note: I use this in a project and it works (it does not use the Entityreference Autocreate module).

First alter the artist form and add a custom validate and a custom submit

function YOUR_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'artist_node_form') {

            //hidding the Entity Reference field (User), it will be created in the process  
            unset($form['field_user']);

            if (!isset($node->nid) || isset($node->is_new)) {
                $form['field_username'] = array(
                    '#type' => 'container',
                    '#weight' => 3,
                    'field_username' => array(
                        '#type' => 'textfield',
                        '#title' => t('Username'),
                        '#maxlength' => 60,
                        '#description' => t('Spaces are allowed; punctuation is not allowed except for periods, hyphens, apostrophes, and underscores.'),
                        '#required' => TRUE,
                        '#attributes' => array(
                            'class' => array(
                                'username',
                                ),
                            ),
                        '#default_value' => '',
                        '#access' => TRUE,
                        '#weight' => -10,
                        )
                    );

                $form['field_password'] = array( 
                    '#type' => 'container',
                    '#weight' => 4,
                    'field_password' => array(
                        '#type' => 'password_confirm',
                        '#size' => 25,
                        '#description' => t('Provide a password for the new account in both fields.'),
                        '#required' => TRUE,
                        ),
                    );

       $form['#validate'][] = 'artist_node_form_validate';
       $form['#submit'][] = 'artist_node_form_submit';
    }
  }
}

In the custom validate you need to validate the username and check if the username is available.

function artist_node_form_validate($form, &$form_state) {
    $valid_user = user_validate_name($form_state['values']['field_username']);
    if(!is_null($valid_user)) {
        form_set_error('field_username', $valid_user);
    } else {
        $user = user_load_by_name($form_state['values']['field_username']);
        if($user){
            form_set_error('field_username', t('The username @username already exist.', array('@username' => $form_state['values']['field_username'])));
        } else {
            $form['field_username'];
        }
    }
}

In the custom submit you will create the user

function artist_node_form_submit($form, &$form_state) {
    $new_user = array(
        'name' => $form_state['values']['field_username'],
        'pass' => $form_state['values']['field_password'], // note: do not md5 the password
        'mail' => $form_state['values']['field_email'][LANGUAGE_NONE][0]['email'],
        'status' => 1
        );

    //The first parameter is sent blank so a new user is created.
    $new_user = user_save('', $new_user);

    //assigning the role
    $role = user_role_load_by_name("artist");
    user_multiple_role_edit(array($new_user->uid), 'add_role', $role->rid);  

    //field_user is the Entity Reference field
    $form_state['values']['field_user'][LANGUAGE_NONE][0]['target_id'] = $new_user->uid;

}

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.