I briefly worked with Drupal several years ago, and now have come back to it for a single project. For all intents, I'm a total beginner. I need to create a new user from a custom form (html in a block.) I've found some info on how to do the back-end processing (this and this), but can't figure out how to capture the form submission values in my custom module. Can I just use $_POST
values? Is that allowed? Or is there some other way I'm supposed to go about it?
My initial module code looks like this:
function create_vector_user_block_info() {
$blocks['create_vector_user'] = array(
// The name that will appear in the block list.
'info' => t('Create Vector User'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
function create_vector_user_submit() {
//This will generate a random password, you could set your own here
$password = user_password(8);
//set up the user fields
$fields = array(
'name' => 'user_name',
'mail' => '[email protected]',
'pass' => $password,
'status' => 1,
'init' => 'email address',
'roles' => array(
DRUPAL_AUTHENTICATED_RID => 'authenticated user',
),
);
//the first parameter is left blank so a new user is created
$account = user_save('', $fields);
// If you want to send the welcome email, use the following code
// Manually set the password so it appears in the e-mail.
$account->password = $fields['pass'];
// Send the e-mail through the user module.
drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account), variable_get('site_mail', '[email protected]'));
}
What I need to do is get the value of a username and email field in my custom block and pass them to the elements in the $fields
array.
hook_form
, create your custom submit handler, and use your form values found in$form_state
.