2

On the registration page, I want to restrict the domain of the e-mail adress only between a few choices (to limit the use of the site to internal company use only) through a list of choices. The user, upon registration, would therefore only have to write the characters before the @ sign ; and then choose the correct domain name he uses (for instance yahoo.fr). The final value of the email registered by Drupal (and thus registered in the database) should be like this one: [User_entry]+[@]+[chosen_domain_name]. I found some functions to verify the e-mail spelling, but I really want users only to have the choice between shown domain names.

I've read some answers talking about hook_form_user_login_alter(), but I still don't get how I could make it...

Thks for your replies !

3

I'm not sure if you have any experience in PHP or custom module development but I don't have time to give you a working example. However I will point you in the right direction.

• Create a custom module

• Use hook_form_alter to alter the form with the form id user_registration

• In your hook_form_alter add a custom validation function and ensure it is the first validation function that runs

• In your hook_form_alter unset the mail element and add two elements for the two parts of your form (the text input and the dropdown list of domains)

• In your custom validation function, use the submitted values of your form elements to create an email address and assign it to the element you unset before, mail

• Make sure that in the validation function, you don't allow users to submit blank values

Edit for a fuller answer to be added

Ok, apologies for taking my time. This code is untested but should hopefully work for what you want to do. First of all, try to avoid doing things like this in the theme layer. The theme layer should be for themes only, so you want to create a custom module.

To create your module you need to create a .info file. Alter the following code and paste it into your text editor and save it as example.info. Remember that you can change the names of the files etc to whatever suits you, just replace the word "example" with whatever name you want to give it. Please note that Drupal deliberately omits the closing ?> PHP tag.

name = "Example module"
description = "Gives an example of a module."
core = 6.x
package = Other

Next, you want to save a file called example.module. Inside this file, you will want to copy the following code. This code follows the instructions that I gave you in my previous answer. I have tried to comment this code as well as possible in order to try to show you what is being done and why. This should do what you want it to do.

<?php

/**
 * Implementation of hook_form_alter()
 */
function example_form_alter(&$form, &$form_state, $form_id) {
    // If the form is the user_register form
    if ($form_id == 'user_register') {
        // Attach our custom validation function as the first validation function
        array_unshift($form['#validate'], '_example_user_validate');
        // Hide the email address field
        $form['mail']['#type'] = 'hidden';
        // Give the email address field a random default value (in this case a random number and a timestamp)
        // as we cannot unset it due to Drupal's validation rules
        $form['mail']['#default_value'] = rand(1111, 9999) . '@' .time() . '.com';
        // Add our new text entry field
        $form['mail_first'] = array(
      '#type' => 'textfield',
      '#title' => t('Email address'),
      '#default_value' => '',
      '#size' => 60,
      '#maxlength' => 64,
      '#required' => TRUE,
    );
        // Add our select list of possible email domain values
        $form['mail_last'] = array(
      '#type' => 'select',
      '#prefix' => '@',
      '#options' => array(
              // These are the possible email address domains
              'yahoo.fr' => 'yahoo.fr',
              'yahoo.com' => 'yahoo.com',
              'example.com' => 'example.com',
            ),
      '#required' => TRUE,
    );
    }
}

/**
 * Function to piece together our email address
 */
function _example_user_validate(&$form, &$form_state) {
    // If our two form elements have values
    if (!empty($form_state['values']['mail_first']) && !empty($form_state['values']['mail_last'])) {
        // Set the email address to the correct value
        $form_state['values']['mail'] = $form_state['values']['mail_first'] . '@' . $form_state['values']['mail_last'];
    }
}
| improve this answer | |
  • Thks a lot for your help ! I'll try to write this (I have a little experience in PHP), and post-it there either it works or not...! :) – nyoz May 23 '11 at 11:01
  • If you come up with code and it doesn't work, post it back here and we can take a look at it. – Jamie Hollern May 23 '11 at 11:43
  • Well, as I thought what I tried didn't worked : I'm a very beginner at PHP coding and Drupal architecture frightens me a bit ! I post my function in answer to this question, so it's not limited. – nyoz May 25 '11 at 18:54
  • BTW : tatheme is the name of my time, I've put that into my template.php – nyoz May 25 '11 at 19:00
  • (I meant my theme, not my time ^^) – nyoz May 26 '11 at 10:10
2

Here is what I wrote, I think it's full of errors ! O_O ^^

<?php 
    function tatheme_user_registration_form_alter (&$variables) {
        if (!empty($variables['user']['mail'])) {
            unset $variables['user']['mail'];
        }
        print '<input id="mailpart1" value="" class="'.$classes.'" type="text" /> @ <select id="mailpart2" name="mailpart2" class="'.$classes.'"><option value="dede.net">dede.net</option><option value="dede.com">dede.com</option>';

        $syntax='#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#'; 

        $mail = $_POST['mailpart1']+"@"+$_POST['mailpart2'];

        if (isset($_POST['mailpart1']) OR !$_POST['mailpart1']) {
            return false;
        }
        else {  
            if (preg_match($syntax,$mail)) {
                return true; 
            }
            else {
                return false;
            }
        }

        $variables['user']['mail'] = $mail;
    }
    ?>

CODE UPDATE AT 05/28/11 :

<?php

/**
 * Implementation of hook_form_alter()
 */
function ta_module_form_alter(&$form, &$form_state, $form_id) {
    // If the form is the user_register form
    if ($form_id == 'user_register_form') {
        // Attach our custom validation function as the first validation function
        array_unshift($form['#validate'], '_ta_module_user_validate');
        // Hide the email address field
        $form['account']['mail']['#type'] = 'hidden';
        // Give the email address field a random default value (in this case a random number and a timestamp)
        // as we cannot unset it due to Drupal's validation rules
        $form['account']['mail']['#default_value'] = rand(1111, 9999) . '@' .time() . '.com';
        // Add our new text entry field
        $form['mail_first'] = array(
      '#type' => 'textfield',
      '#title' => t('Email address'),
      '#default_value' => '',
      '#size' => 40,
      '#maxlength' => 64,
      '#required' => TRUE,
    );
        // Add our select list of possible email domain values
        $form['mail_last'] = array(
      '#type' => 'select',
      '#prefix' => '@',
      '#options' => array(
              // These are the possible email address domains
              'blabla.net' => 'blabla.net',
              'blabla.com' => 'blabla.com',
            ),
      '#default_value' => 'blabla.net',
      '#required' => TRUE,
    );
    }
}

/**
 * Function to piece together our email address
 */
function _ta_module_user_validate(&$form, &$form_state) {
    // If our two form elements have values
    if (!empty($form_state['values']['mail_first']) && !empty($form_state['values']['mail_last'])) {
        // Set the email address to the correct value
        $form_state['values']['mail']['edit-mail'] = $form_state['values']['mail_first'] . '@' . $form_state['values']['mail_last'];
    }
}
| improve this answer | |
  • 1
    Kudos for trying to code it mate, even if you didn't get it right you still had a shot. I'll post some code a bit later that should set you right. Well done. – Jamie Hollern May 25 '11 at 19:54
  • Ok! Thks for your attention! – nyoz May 26 '11 at 10:10

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.