1

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' => 'user_name@example.com',
    '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', 'noreply@example..com'));
}

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.

3
  • There are many examples of using the Drupal Form API. I'd suggest you take a look at the "examples" module.
    – AKS
    Apr 20 '15 at 18:20
  • Where do I find that? I searched drupal.org modules for "example" but didn't find any with that name, just tons with the word example somewhere in them.
    – EmmyS
    Apr 20 '15 at 18:47
  • Here: drupal.org/project/examples
    – AKS
    Apr 20 '15 at 18:48
0

As AyeshK pointed out there are several examples out there. This should have all the elements you need.

First you must create a form to use in your block with hook_form, create your custom submit handler, and use your form values found in $form_state.

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.