2

I am trying to build a new form from the user registration form and while the user reg parts are appearing the new fields are not. Any ideas? The code is below. I tried having the fields in the primary array level AND under the account parameter.

When the array prints out it shows the parameters are there. So I am having trouble figuring out why it is not working. I am used to doing this in Drupal 6. But in Drupal 7 it is not cooperating.

function custom_reg_company_registration() {
  return drupal_get_form('custom_reg_company_form');
}

function custom_reg_company_form($form, &$form_state) {
  $form = user_register_form($form, $form_state);

  $form['company_name'] = array(
    '#type' => 'textfield',
    '#title' => 'Company Name',
    '#maxlength' => 60,
    '#description' => 'The name of your company.',
    '#required' => 1,
    '#attributes' => array(
      'class' => array(0 => 'company_name'),
    ),
    '#default_value' => '',
    '#access' => 1,
    '#weight' => -10,
  );

  $form['primary_phone'] = array(
    '#type' => 'textfield',
    '#title' => 'Primary Phone',
    '#maxlength' => 30,
    '#description' => 'The primary phone number for your company.',
    '#required' => 1,
    '#attributes' => array(
      'class' => array(0 => 'primary_phone'),
    ),
    '#default_value' => '',
    '#access' => 1,
    '#weight' => -10,
  );

  $form['secondary_phone'] = array(
    '#type' => 'textfield',
    '#title' => 'Secondary Phone',
    '#maxlength' => 30,
    '#description' => 'The secondary phone number for your company.',
    '#required' => 0,
    '#attributes' => array(
      'class' => array(0 => 'secondary_phone'),
    ),
    '#default_value' => '',
    '#access' => 1,
    '#weight' => -10,
  );

  $form['company_type'] = array(
    '#type' => 'radios',
    '#title' => 'Company Type',
    '#maxlength' => 30,
    '#description' => 'The type of company you are setting up.',
    '#required' => 1,
    '#default_value' => 0,
    '#weight' => -10,
    '#options' => array(
      0 => 'Carrier',
      1 => 'Broker & FF',
      2 => 'Shipper',
    ),
  );

  $form['#action'] = url('register/company');

  echo "<pre>" . print_r($form, true) . "</pre>";

  return $form;
}
1
  • I owe everyone an apology. I did not realize that I was loading my block wrong and ended up loading up the user registration form instead of my customized one. I will still distribute points as much as I can. Sorry about that.
    – Patrick
    Feb 12, 2012 at 23:09

3 Answers 3

1
+50

$form = user_register_form($form, $form_state);

This line is strange and should probably be : $form = drupal_get_form('user_register_form');

Also, your function looks like a form_alter hook, with the relevant parameters, but it won't be called because it isn't named correctly.

If you want to alter the registering form, you should use the 'your_module_user_register_form_alter' syntax as shown here http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_form_alter/7.

EDIT : This code is more standard, and also works :

function yourmodule_menu() {
  $items['stackform'] = array(
    'title' => 'Code Stack Exchange',
    'description' => 'Test SE',
    'page callback' => 'custom_reg_company_form',
    'access callback' => TRUE,    
  ); 
}

function custom_reg_company_form() {
  $form = drupal_get_form('user_register_form');

  $form['company_name'] = array(
    '#type' => 'textfield',
    '#title' => 'Company Name',
    '#maxlength' => 60,
    '#description' => 'The name of your company.',
    '#required' => 1,
    '#attributes' => array(
      'class' => array(0 => 'company_name'),
    ),
    '#default_value' => '',
    '#access' => 1,
    '#weight' => -10,
  );

  $form['primary_phone'] = array(
    '#type' => 'textfield',
    '#title' => 'Primary Phone',
    '#maxlength' => 30,
    '#description' => 'The primary phone number for your company.',
    '#required' => 1,
    '#attributes' => array(
      'class' => array(0 => 'primary_phone'),
    ),
    '#default_value' => '',
    '#access' => 1,
    '#weight' => -10,
  );

  $form['secondary_phone'] = array(
    '#type' => 'textfield',
    '#title' => 'Secondary Phone',
    '#maxlength' => 30,
    '#description' => 'The secondary phone number for your company.',
    '#required' => 0,
    '#attributes' => array(
      'class' => array(0 => 'secondary_phone'),
    ),
    '#default_value' => '',
    '#access' => 1,
    '#weight' => -10,
  );

  $form['company_type'] = array(
    '#type' => 'radios',
    '#title' => 'Company Type',
    '#maxlength' => 30,
    '#description' => 'The type of company you are setting up.',
    '#required' => 1,
    '#default_value' => 0,
    '#weight' => -10,
    '#options' => array(
      0 => 'Carrier',
      1 => 'Broker & FF',
      2 => 'Shipper',
    ),
  );

  $form['#action'] = url('register/company');
  return $form;
}

Note that both snippets work on a D7 install, mine is just better practice.

If your fields don't appear, it probably means some module is resetting the form AFTER your module's intervention. You can try to change your module's weight to prevent this and see what happens.

5
  • There's nothing strange about that code, it's a pretty standard thing to call a function in PHP :) There's no mention of a form alter hook here either, the form is being returned as the result of a menu page callback. For what it's worth OP's code works perfectly when pasted into a standard Drupal installation so the problem must be with a custom/contributed module.
    – Clive
    Feb 10, 2012 at 12:14
  • 1
    I didn't say it was strange PHP, I said it was strange Drupal, and suggested trying to make things in a more common (Drupalish) way could be worth a try. The common way is to fetch forms via drupal_get_form and not to call the function directly.
    – Countzero
    Feb 10, 2012 at 14:54
  • I remember back when I was learning the API. php -l didn't find any errors in my code, so clearly the problem was with Drupal. :-) Countzero's answer is the correct one.
    – paul-m
    Feb 11, 2012 at 5:15
  • @paul-m It isn't though...The OP's code works as is with no changes whatsoever when pasted into a standard Drupal installation, I've tried it. This is also confirmed by acouch in another answer.
    – Clive
    Feb 11, 2012 at 11:53
  • If the OP code works, then is there a question here? :-)
    – paul-m
    Feb 12, 2012 at 5:28
2

Is there a reason you need to use code? You could just add the fields to the user profile and check "required on registration" to make them show up during registration.

6
  • Yes, there is a reason. This is because for this sites registration I am not only creating the user but also the company that the user will be attached to. Then once the company group is created the admin user will create users under that company group. The company group will then work on a subscription basis.
    – Patrick
    Jan 27, 2012 at 23:54
  • are you using organic groups for the companies?
    – uwe
    Jan 28, 2012 at 1:33
  • Yes I am. It seems like the best solution unless someone has a better suggestion.
    – Patrick
    Jan 29, 2012 at 17:15
  • sounds like a good OG use case. I think you could still create the user profile fields in the UI, then use a hook like hook_user_insert to create a new OG (if it doesn't exist already) and assign the user to it. There is a feature request for that but doesn't have a response: drupal.org/node/1125046
    – uwe
    Jan 29, 2012 at 19:20
  • That would create a lot of duplicate data though. I am not fond of that little quirk in the solution.
    – Patrick
    Jan 30, 2012 at 1:57
1

Have you tried clearing the cache? This code creates the form with the "Company Name" and other fields for me.

Is custom_reg_company_registration() a callback from hook_menu?

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.