1

My Drupal 7 site utilizes the email_registration and logintobbagan modules. It works fine to register users with just their e-mail address. However, on a separate page I am using a newsletter subscribe form called 'news_subscribe' which is of the following form:

function news_subscribe_form($form, &$form_state) {
    $user = $GLOBALS['user']->mail;
    $email = "";
    if(!empty($user))
    {
        $email = $user;
    }
    $form['email'] = array(
        '#type' => 'textfield',
        '#title' => t('E-mail'),
        '#required' => TRUE,
        '#default_value' => $email,
        '#size' => 20,
        '#maxlength' => 20,
        '#element_validate' => array('news_subscribe_email_element_validate'),
    );
    $form['id'] = $id;
    $form['submit_button'] = array(
        '#type' => 'submit',
        '#value' => t('Subscribe!'),
    );
  return $form;
}

By default this then submits the data to the drupal databases and saves the values for the given user if they are already registered in our system via hook_form_submit().

function news_subscriptions_form_submit($form, &$form_state) {
    //Get the values
    $email = $form_state['values']['email'];
    $id = $form_state['build_info']['args'][0];
    $uid = $GLOBALS['user']->uid;

    //Get the database information
    $collection = variable_get('mongo_user_preferences_collection',DEFAULT_USER_PREFERENCES_COLLECTION);
    $savedarrayname = variable_get('mongo_saved_data_array',DEFAULT_SAVED_DATA_ARRAY);

    //Generate the conditions and queries
    $where = array("uid"=>intval($uid));
    $query = array($savedarrayname=>$id);

    //Insert the data into the datbase
    upsertData($where,$query,$collection,$database=NULL);
}

If the user is not currently registered in the system I need to first automatically register them and then retrieve their user id. I know that normally I can use a function such as

$new_user = array(
  'name' => 'JohnDoe',
  'mail' => '[email protected]',
  'pass' => 'password123',
  'status' => 1,
  'field_custom_first_name' => array(LANGUAGE_NONE => array(array('value' => 'John'))),
  'access' => REQUEST_TIME,
  'roles' => array(), // No other roles than Authenticated
);
user_save(NULL, $new_user);

However, I want to integrate it with the current setup which requires just an email address, auto generates a password and then sends a mail to the user with a temporary login link where they can change the password. This is the current setup on the user registration form. I have been looking for a hook into the email_registration module but cannot find one. Can anyone provide some insight?

Basically I am trying to register a user from a non-registration form (if they are not already registered). I then need to retrieve their assigned uid which is used in subsequent post-processing.

2
  • What exactly are you trying to do? Change the text in the registration mail? Commented Jul 23, 2014 at 6:24
  • If you change user_save(NULL, $new_user); to $user = user_save(NULL, $new_user); you will have the $user and can access the created users UID by $user->uid. Commented Jul 23, 2014 at 7:16

2 Answers 2

2

In your custom submit function you can do the following,

$email_entered = $form_state['values']['email'];

$query = db_select('users', 'u');
$query->fields('u', array('uid'));
$query->condition('u.email', $email_entered);
$row_count = $q->countQuery()->execute()->fetchField();

if ($row_count == 0) {
  // User doesn't exist, Save the user.
  $parts = explode("@", $email_entered);
  $account = new stdClass;
  $account->is_new = TRUE;
  $account->name = $parts[0];
  $account->pass = user_hash_password('bar');
  $account->mail = $email_entered;
  $account->init = $email_entered;
  $account->status = TRUE;
  $account->roles = array(DRUPAL_AUTHENTICATED_RID => TRUE);
  $account->timezone = variable_get('date_default_timezone', '');
  user_save($account);
} 
else {
  // User exists.
}

As you can see there is no way that you can get the name of the user so you have to use the email as the username. This will be stored in users.name field. You also might want to mail the password to the user.

1

I need to first automatically register them and then retrieve their user id.

If you change:

user_save(NULL, $new_user);

to:

$user = user_save(NULL, $new_user);

you will have the $user and can access the created users UID by $user->uid.

@PontusNilsson

Your Answer

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

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