2

I am new to drupal.

I need to customize a registration form by adding fields like id,mobile etc.

Can I accomplish this by creating a custom module?

If yes could anyone please help me with a brief idea on creating and overriding the default user registration submit function. I have to insert these details to another table and also have to pass the data as a service request.

Ive created a custom module with function

function module_form_alter(&$form,&$form_state,$form_id){
$form['#submit'] = 'module_form_submit';

if($form_id == 'user_register_form'){ 
    //print_r($form_id);
    $form['email'] = array(
            '#type' => 'textfield',
          '#title' => t('id'),
          '#default_value' => '',
          '#size' => 60,
          '#maxlength' => 15,
          '#required' => TRUE,
     );
    }
}

function module_form_submit($form, &$form_state){
  echo "test";
 exit();
}

module_form_alter is being called and I can see the new field on the registration screen but the submit function is still not called. I need to override the default drupal register submit.

I already have the following function in my theme template.php

function templatename_theme() {
  $items = array();

  $items['user_login'] = array(
    'render element' => 'form',
    'path' => drupal_get_path('theme', 'portal') . '/templates',
    'template' => 'user-login',
    'preprocess functions' => array(
     'portal_preprocess_user_login'
    ),
  );
  $items['user_register_form'] = array(
    'render element' => 'form',
    'path' => drupal_get_path('theme', 'portal') . '/templates',
    'template' => 'user-register-form',
    'preprocess functions' => array(
      'portal_preprocess_user_register_form'
    ),
  );
  $items['user_pass'] = array(
    'render element' => 'form',
    'path' => drupal_get_path('theme', 'portal') . '/templates',
    'template' => 'user-pass',
    'preprocess functions' => array(
      'portal_preprocess_user_pass'
    ),
  );

  return $items;
}

user-register-form.tpl.php

    <div class="form-group">
    <?php print drupal_render_children($form); ?>
    </div>

And page--user--register.tpl.php with the html

<div id="login-page">
    <div class="container">     
             <div class="form-login" >
            <h2 class="form-login-heading"><a href="<?php echo $home; ?>"><img src="<?php echo drupal_get_path('theme', 'portal') . '/images/logo.png'; ?>"  width="100"></a><?php echo $createaccount; ?></h2>
            <div class="login-wrap">
            <?php  
            $elements = drupal_get_form("user_register_form"); 
            $form = drupal_render($elements);
            echo  $form ?>

     <?php if ($messages):?>
        <div id="messages-console" class="clearfix">
            <div class="grid_12">
                <div class="mt-grid-fix">
                <?php print $messages; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>



        </div>
                </div>

    </div>
  </div>

Is this approach fine or should I change this approach inorder to make my custom module functional?

One problem, I need to validate this form in client side, Ive added js in module_form_user_register_form_alter function as
$form['#attached']['js'] = array(drupal_get_path('module', 'modulenamne').'/js/myjs.js',);

File is loaded but when clicked on submit the js is not working, instead the page is loading and drupal submit is executing.

3

If it is not crucial for you to have the additional fields in custom code, I would highly recommend you to make use of the fact that user is a fieldable entity and you can add additional fields easily to it via administration at "admin/config/people/accounts/fields" - this way you do not need to handle saving of the data and display of form elements.

When doing so, you can still make use of custom validation or submit handlers or fiddle with the form and fields easily using the same technique you are using - hook_form_alter and add your submit handler to do whatever logic you require.

Back to your approach — the reason why your submit function is not being called is that you are assigning a string to it instead of adding an element to the array. Try this:

$form['#submit'][] = 'module_form_submit'; (notice the empty brackets on the left side).

If you want to completely overwrite the submit functions array use this:

$form['#submit'] = array('module_form_submit'); - but this will cause that the user will not be created and data saved (unless you do that by hand in your custom submit handler), this also cancels whatever some contrib modules to with the registration form, so you might have unexpected issues.

Edit after comments:

If you do want to keep the default behaviour, but do your logic before everything, you can put your submit handler as first, as they are executed based on their order in the array like this:

array_unshift($form['#submit'], 'module_form_submit');

  • Thanks for the response, the reason why I removed the array was for overriding the default submit handler of user registration, I need my submit handler to execute first, check some conditions, and only then allow the drupal registration submit to execute.Any thoughts? – wilNev Mar 31 '15 at 8:22
  • I tried adding the extra fields through "admin/config/people/accounts/fields" ,this approach is okay for me, however I needed to replace the username field with new field 'ID' , is that possible? – wilNev Mar 31 '15 at 8:25
  • I updated my questions based on your comments. When it comes to the username field - no you cannot change that, it is a mandatory column in the users table, the User entity cannot be created without it. In a case that you need such custom behaviour you might need to consider using something else than user register form, maybe a content type or webform approach. – m1n0 Mar 31 '15 at 9:12
  • Thanks for your comments. I am planning to go for webform approach.But for this I need to manual insert in the users table. – wilNev Mar 31 '15 at 9:33
  • I have tried with this method to override the submit handler $form['#submit'] = array_unshift($form['#submit'], 'module_form_submit'); but its throwing a warning "Warning: Invalid argument supplied for foreach() in form_execute_handlers() (line 1501 of /var/www/html/account/includes/form.inc)" – wilNev Mar 31 '15 at 9:36

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.