realname_registration.module 24.6 KB
Newer Older
1 2 3 4 5 6
<?php
/**
 * @file
 * For using real names during registration/
 *
 * The realname_registration allows usernames to be generated based upon
Steve Gerbino's avatar
Steve Gerbino committed
7
 * required first name and last name fields on the registration form.
8 9
 */

10 11 12 13 14

/*
 * Implementation of hook_enable().
 */
function realname_registration_enable() {
15
  $success  = t('Realname registration has been enabled. ');
16
  $success .= t('Please visit the') . ' <a href="config/people/realname_registration">' . t('configuration page') . '</a>.';
17 18 19
  drupal_set_message($success, 'status');
}

20 21 22 23
/**
 * Implement hook_help().
 */
function realname_registration_help($path, $arg) {
Steve Gerbino's avatar
Steve Gerbino committed
24 25 26 27
  switch ($path) {
    case 'admin/help#realname_registration':
      $output = '<h3>' . t('Realname registration') . '</h3>';
      $output .= '<h4>' . t('Summary') . '</h4>';
28
      $output .= '<p>' . t('Usernames are made of a combination of the first name, middle name, and last name field. The values will be stored in your provided user fields.') . '</p>';
Steve Gerbino's avatar
Steve Gerbino committed
29 30
      $output .= '<h4>' . t('Settings') . '</h4>';
      $output .= '<dl>';
31
      $output .= '<dt><strong>' . t('Using the core Profile module') . '</strong></dt>';
32 33 34
      $output .= '<dd>' . t('(If you already have these fields, take note of the names and skip this step)') . '</dd>'; 
      $output .= '<dd>' . t('Navigate to <a href="../config/people/accounts/fields">admin/config/people/accounts/fields</a> and create a text field that will hold the user\'s first name.') . ' '; 
      $output .= t('While editing the field, ensure that the Required field option is checked, also to Display on the registration form.') . ' '; 
35 36 37 38 39 40 41 42 43 44
      $output .= t('Repeat this step for the last name field. The middle name field is optional, and does not need to be required. Be sure to take note of what you have named the new fields.') . '</dd>';
      $output .= '<dd><a href="../config/people/accounts/fields">' . t('Manage user fields') . ' &rarr;</a></dd>';
      if (module_exists('profile2')) {
        $output .= '<dt><strong>' . t('Using the Profile2 module') . '</strong></dt>';
        $output .= '<dd>' . t('If you have Profile2 installed, you will have additional fieldsets on the configuration page.') . ' ';
        $output .= t('Create your field names so that the first and last names are required. The middle name field is always optional.') . ' ';
        $output .= t('Ensure that your Profile2 type is used during registration.') . ' ';
        $output .= t('To use the fields in Realname registration, check the box saying use this module for storage and supply the machine name of the corresponding Profile2 type.') . '</dd>';
        $output .= '<dd><a href="../structure/profiles">' . t('Manage Profile2 types') . ' &rarr;</a></dd>';
      }
Steve Gerbino's avatar
Steve Gerbino committed
45 46 47
      $output .= '</dl>';
      $output .= '<dl>';
      $output .= '<dt><strong>' . t('Configure the Realname registration module') . '</strong></dt>';
48
      $output .= '<dd>' . t('Now that you have first name, middle name, and last name fields associated with your users, you can map these fields in Realname registration.') . ' ';
49
      $output .= t('Type the field names that you have created in the appropriate textfields, and select your other options and click Submit.') . '</dd>';
50
      $output .= '<dd><a href="../config/people/realname_registration">' . t('Configure Realname registration') . ' &rarr;</a></dd>';
Steve Gerbino's avatar
Steve Gerbino committed
51
      $output .= '</dl>';
52 53 54 55 56 57 58
      $output .= '<p><strong>' . t('Project resources') . '</strong></p>';
      $output .= '<ul>';
      $output .= '<li><a href="http://drupal.org/project/realname_registration">' . t('Project home') . '</a></li>';
      $output .= '<li><a href="http://docs.yunices.com/project/realname-registration/installation-configuration">' . t('Documentation') . '</a></li>';
      $output .= '<li><a href="http://drupal.org/project/issues/realname_registration?status=All&categories=All">' . t('Issue tracker') . '</a></li>';
      $output .= '</ul>';
      
Steve Gerbino's avatar
Steve Gerbino committed
59
      return $output;
60 61 62
  }
}

63 64 65 66 67 68 69
/**
 * Implementation of hook_form_alter().
 */
function realname_registration_form_alter(&$form, $form_state, $form_id) {
  if (!($form_id == 'user_register_form')) {
    return;
  }
Steve Gerbino's avatar
Steve Gerbino committed
70

71 72 73 74 75 76 77 78
  if (isset($form['account']) && is_array($form['account'])) {
    $form['account']['name']['#type'] = 'hidden';
    $form['account']['name']['#value'] = 'unset_username';
  }
  else {
    $form['name']['#type'] = 'hidden';
    $form['name']['#value'] = 'unset_username';
  }
Steve Gerbino's avatar
Steve Gerbino committed
79

80
  $form['#validate'][] = 'realname_registration_mandatory_validate';
81 82
  if (variable_get('realname_registration_use_validation')) {
    $form['#validate'][] = 'realname_registration_validate';
83 84 85 86 87 88 89
  }
}

/**
 * Implementation of hook_user_presave().
 */
function realname_registration_user_presave(&$edit, $account, $category) {
90
  if (isset($edit['name'])) {
91
    if ($edit['name'] == 'unset_username') {
Steve Gerbino's avatar
Steve Gerbino committed
92
      $firstname_field = variable_get('realname_registration_firstname_field');
93 94
      $middlename_field = variable_get('realname_registration_middlename_field');
      $lastname_field = variable_get('realname_registration_lastname_field');
95 96 97 98 99 100 101 102 103 104 105

      $firstname_type = variable_get('realname_registration_profile2_firstname_field_profile_name');
      $middlename_type = variable_get('realname_registration_profile2_middlename_field_profile_name');
      $lastname_type = variable_get('realname_registration_profile2_lastname_field_profile_name');

      if (variable_get('realname_registration_ucfirst')) {
        if (variable_get('realname_registration_use_profile2_firstname_field')) {
          $edit['profile_' . $firstname_type][$firstname_field]['und'][0]['value'] = drupal_ucfirst($edit['profile_' . $firstname_type][$firstname_field]['und'][0]['value']);
        }
        else {
          $edit[$firstname_field]['und'][0]['value'] = drupal_ucfirst($edit[$firstname_field]['und'][0]['value']);
106 107
        }

108 109 110 111
        if (variable_get('realname_registration_use_profile2_middlename_field')) {
          if (!empty($edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'])) {
            $edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'] = drupal_ucfirst($edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value']);
          }
112 113
        }
        else {
114 115 116
          if (!empty($edit[$middlename_field]['und'][0]['value'])) {
            $edit[$middlename_field]['und'][0]['value'] = drupal_ucfirst($edit[$middlename_field]['und'][0]['value']);
          }
117
        }
118 119 120

        if (variable_get('realname_registration_use_profile2_lastname_field')) {
          $edit['profile_' . $lastname_type][$lastname_field]['und'][0]['value'] = drupal_ucfirst($edit['profile_' . $lastname_type][$lastname_field]['und'][0]['value']);
121
        }
122 123
        else {
          $edit[$lastname_field]['und']['0']['value'] = drupal_ucfirst($edit[$lastname_field]['und'][0]['value']);
124
        }
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
      }

      // Generate the username based on first name and last name fields.
      switch (variable_get('realname_registration_format')) {
        case 0:
          // Create username based on first name and last name (e.g., John Smith).
          if (variable_get('realname_registration_use_profile2_firstname_field')) {
            $username = $edit['profile_' . $firstname_type][$firstname_field]['und'][0]['value'];
          }
          else {
            $username = $edit[$firstname_field]['und'][0]['value'];
          }
          $username .= ' ';
          if (variable_get('realname_registration_use_profile2_lastname_field')) {
            $username .= $edit['profile_' . $lastname_type][$lastname_field]['und'][0]['value'];
          }
          else {
            $username .= $edit[$lastname_field]['und'][0]['value'];
          }
          break;

        case 1;
          // Create username based on first initial and last name (e.g., JSmith).
          if (variable_get('realname_registration_use_profile2_firstname_field')) {
            $username = drupal_substr($edit['profile_' . $firstname_type][$firstname_field]['und'][0]['value'], 0, 1);
          }
          else {
            $username = drupal_substr($edit[$firstname_field]['und'][0]['value'], 0, 1);
          }
          if (variable_get('realname_registration_use_profile2_lastname_field')) {
            $username .= $edit['profile_' . $lastname_type][$lastname_field]['und'][0]['value'];
          }
          else {
            $username .= $edit[$lastname_field]['und'][0]['value'];
          };
          break;

        case 2:
          // Create username based on first name, middle name, and last name (e.g., John Jacob Smith).
          if (variable_get('realname_registration_use_profile2_firstname_field')) {
            $username = $edit['profile_' . $firstname_type][$firstname_field]['und'][0]['value'];
          }
          else {
            $username = $edit[$firstname_field]['und'][0]['value'];
          }
          $username .= ' ';
          if (variable_get('realname_registration_use_profile2_middlename_field')) {
            if (!empty($edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'])) {
              $username .= $edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'];
              $username .= ' ';
            }
          }
          else {
            if (!empty($edit[$middlename_field]['und'][0]['value'])) {
              $username .= $edit[$middlename_field]['und'][0]['value'];
              $username .= ' ';
            }
          }
          if (variable_get('realname_registration_use_profile2_lastname_field')) {
            $username .= $edit['profile_' . $lastname_type][$lastname_field]['und'][0]['value'];
          }
          else {
            $username .= $edit[$lastname_field]['und'][0]['value'];
          }
          break;

        case 3:
          // Create username based on first name, middle initial, and last name (e.g., John J Smith).
          if (variable_get('realname_registration_use_profile2_firstname_field')) {
            $username = $edit['profile_' . $firstname_type][$firstname_field]['und'][0]['value'];
          }
          else {
            $username = $edit[$firstname_field]['und'][0]['value'];
          }
          $username .= ' ';
          if (variable_get('realname_registration_use_profile2_middlename_field')) {
            if (!empty($edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'])) {
              $username .= drupal_substr($edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'], 0, 1);
              $username .= ' ';
            }
          }
          else {
            if (!empty($edit[$middlename_field]['und'][0]['value'])) {
              $username .= drupal_substr($edit[$middlename_field]['und'][0]['value'], 0, 1);
              $username .= ' ';
            }
          }
          if (variable_get('realname_registration_use_profile2_lastname_field')) {
            $username .= $edit['profile_' . $lastname_type][$lastname_field]['und'][0]['value'];
          }
          else {
            $username .= $edit[$lastname_field]['und'][0]['value'];
          }
          break;

        case 4:
          // Create username based on first initial, middle initial, and last name (e.g., JJSmith).
          if (variable_get('realname_registration_use_profile2_firstname_field')) {
            $username = drupal_substr($edit['profile_' . $firstname_type][$firstname_field]['und'][0]['value'], 0, 1);
          }
          else {
            $username = drupal_substr($edit[$firstname_field]['und'][0]['value'], 0, 1);
          }
          if (variable_get('realname_registration_use_profile2_middlename_field')) {
            if (!empty($edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'])) {
              $username .= drupal_substr($edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'], 0, 1);
            }
          }
          else {
            if (!empty($edit[$middlename_field]['und'][0]['value'])) {
              $username .= drupal_substr($edit[$middlename_field]['und'][0]['value'], 0, 1);
            }
          }
          if (variable_get('realname_registration_use_profile2_lastname_field')) {
            $username .= $edit['profile_' . $lastname_type][$lastname_field]['und'][0]['value'];
          }
          else {
            $username .= $edit[$lastname_field]['und'][0]['value'];
          }
          break;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
        case 5:
          // Create username based on first name and last name without spaces (e.g., JohnSmith).
          if (variable_get('realname_registration_use_profile2_firstname_field')) {
            $username = $edit['profile_' . $firstname_type][$firstname_field]['und'][0]['value'];
          }
          else {
            $username = $edit[$firstname_field]['und'][0]['value'];
          }
          if (variable_get('realname_registration_use_profile2_lastname_field')) {
            $username .= $edit['profile_' . $lastname_type][$lastname_field]['und'][0]['value'];
          }
          else {
            $username .= $edit[$lastname_field]['und'][0]['value'];
          }
          break;

        case 6:
          // Create username based on first name, middle name, and last name without spaces (e.g., JohnJacobSmith).
          if (variable_get('realname_registration_use_profile2_firstname_field')) {
            $username = $edit['profile_' . $firstname_type][$firstname_field]['und'][0]['value'];
          }
          else {
            $username = $edit[$firstname_field]['und'][0]['value'];
          }
          if (variable_get('realname_registration_use_profile2_middlename_field')) {
            if (!empty($edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'])) {
              $username .= $edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'];
            }
          }
          else {
            if (!empty($edit[$middlename_field]['und'][0]['value'])) {
              $username .= $edit[$middlename_field]['und'][0]['value'];
            }
          }
          if (variable_get('realname_registration_use_profile2_lastname_field')) {
            $username .= $edit['profile_' . $lastname_type][$lastname_field]['und'][0]['value'];
          }
          else {
            $username .= $edit[$lastname_field]['und'][0]['value'];
          }
          break;

        case 7:
          // Create username based on first name, middle initial, and last name without spaces (e.g., JohnJSmith).
          if (variable_get('realname_registration_use_profile2_firstname_field')) {
            $username = $edit['profile_' . $firstname_type][$firstname_field]['und'][0]['value'];
          }
          else {
            $username = $edit[$firstname_field]['und'][0]['value'];
          }
          if (variable_get('realname_registration_use_profile2_middlename_field')) {
            if (!empty($edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'])) {
              $username .= drupal_substr($edit['profile_' . $middlename_type][$middlename_field]['und'][0]['value'], 0, 1);
            }
          }
          else {
            if (!empty($edit[$middlename_field]['und'][0]['value'])) {
              $username .= drupal_substr($edit[$middlename_field]['und'][0]['value'], 0, 1);
            }
          }
          if (variable_get('realname_registration_use_profile2_lastname_field')) {
            $username .= $edit['profile_' . $lastname_type][$lastname_field]['und'][0]['value'];
          }
          else {
            $username .= $edit[$lastname_field]['und'][0]['value'];
          }
          break;
312
      }
313

314
      if (variable_get('realname_registration_tolower')) {
315 316
        $username = strtolower($username);
      }
317

318 319
      // Check if the username already exists in the database.
      $result = db_query("SELECT name FROM {users} WHERE name = :username", array(':username' => $username));
Steve Gerbino's avatar
Steve Gerbino committed
320

321
      // While the username is taken, append a number and increment it until our username is unique.
322 323 324
      $i = 0;
      while ($result->rowCount()) {
        $result = db_query("SELECT name FROM {users} WHERE name = :username", array(':username' => $username . ++$i));
325
        if (!($result->rowCount())) {
326 327
          $username = $username . $i;
        }
328
      }
329
      $edit['name'] = $username;
330 331 332 333 334 335 336 337 338 339 340
    }
  }
}

/**
 * Form validation handler for user_register_form().
 *
 * @see user_register_form()
 * @see user_register_submit()
 */
function realname_registration_validate($form, &$form_state) {
341
  // A first name may not contain symbols or numbers.
342 343
  $firstname_field = variable_get('realname_registration_firstname_field');
  if (variable_get('realname_registration_use_profile2_firstname_field')) {
344 345 346
    if (preg_match("/[\!\#\$\%\^\*\(\)\[\]\{\}\\\|\/\?\.\>\<\\\"\+,:;=&@0-9]/", $form_state['input']['profile_'
      . variable_get('realname_registration_profile2_firstname_field_profile_name') ][$firstname_field]['und'][0]['value'])) {
      form_set_error($firstname_field, t('First name may not contain symbols or numbers.'));
347 348 349
    }
  }
  else {
350 351
    if (preg_match("/[\!\#\$\%\^\*\(\)\[\]\{\}\\\|\/\?\.\>\<\\\"\+,:;=&@0-9]/", $form_state['values'][$firstname_field]['und'][0]['value'])) {
      form_set_error($firstname_field, t('First name may not contain symbols or numbers.'));
352
    }
353
  }
354

355
  // A middle name may not contain symbols or numbers.
356
  $middlename_field = variable_get('realname_registration_middlename_field');
357 358 359 360 361
  if (variable_get('realname_registration_use_profile2_middlename_field')) {
    if (!empty($form_state['input']['profile_' . variable_get('realname_registration_profile2_middlename_field_profile_name') ][$middlename_field]['und'][0]['value'])) {
      if (preg_match("/[\!\#\$\%\^\*\(\)\[\]\{\}\\\|\/\?\.\>\<\\\"\+,:;=&@0-9]/", $form_state['input']['profile_'
        . variable_get('realname_registration_profile2_middlename_field_profile_name') ][$middlename_field]['und'][0]['value'])) {
        form_set_error($middlename_field, t('Middle name may not contain symbols or numbers.'));
362 363
      }
    }
364 365 366 367 368
  }
  else {
    if (!empty($form_state['values'][$middlename_field]['und'][0]['value'])) {
      if (preg_match("/[\!\#\$\%\^\*\(\)\[\]\{\}\\\|\/\?\.\>\<\\\"\+,:;=&@0-9]/", $form_state['values'][$middlename_field]['und'][0]['value'])) {
        form_set_error($middlename_field, t('Middle name may not contain symbols or numbers.'));
369 370
      }
    }
371 372
  }

373
  // A last name may not contain symbols or numbers.
374 375
  $lastname_field = variable_get('realname_registration_lastname_field');
  if (variable_get('realname_registration_use_profile2_lastname_field')) {
376 377 378
    if (preg_match("/[\!\#\$\%\^\*\(\)\[\]\{\}\\\|\/\?\.\>\<\\\"\+,:;=&@0-9]/", $form_state['input']['profile_'
      . variable_get('realname_registration_profile2_lastname_field_profile_name') ][$lastname_field]['und'][0]['value'])) {
      form_set_error($lastname_field, t('Last name may not contain symbols or numbers.'));
379 380 381
    }
  }
  else {
382 383
    if (preg_match("/[\!\#\$\%\^\*\(\)\[\]\{\}\\\|\/\?\.\>\<\\\"\+,:;=&@0-9]/", $form_state['values'][$lastname_field]['und'][0]['value'])) {
      form_set_error($lastname_field, t('Last name may not contain symbols or numbers.'));
384
    }
385 386
  }
}
387

388 389 390 391 392 393 394 395 396 397 398 399
/**
 * Form validation handler for user_register_form().
 *
 * @see user_register_form()
 * @see user_register_submit()
 */
function realname_registration_mandatory_validate($form, &$form_state) {
  // A first name may not begin or end with a space.
  $firstname_field = variable_get('realname_registration_firstname_field');
  if (variable_get('realname_registration_use_profile2_firstname_field')) {
    if (preg_match("/^\\s+(.*)$/", $form_state['input']['profile_'
      . variable_get('realname_registration_profile2_firstname_field_profile_name') ][$firstname_field]['und'][0]['value'])) {
400
      form_set_error($firstname_field, t('First name cannot begin with a space.'));
401 402 403
    }
    if (preg_match("/^(.*)\\s+$/", $form_state['input']['profile_'
      . variable_get('realname_registration_profile2_firstname_field_profile_name') ][$firstname_field]['und'][0]['value'])) {
404 405 406 407 408
      form_set_error($firstname_field, t('First name cannot end with a space.'));
    }
    if (preg_match("/\\s\\s/", $form_state['input']['profile_'
      . variable_get('realname_registration_profile2_firstname_field_profile_name') ][$firstname_field]['und'][0]['value'])) {
      form_set_error($firstname_field, t('First name cannot contain two spaces in a row.'));
409 410 411 412
    }
  }
  else {
    if (preg_match("/^\\s+(.*)$/", $form_state['values'][$firstname_field]['und'][0]['value'])) {
413
      form_set_error($firstname_field, t('First name cannot begin with a space.'));
414 415
    }
    if (preg_match("/^(.*)\\s+$/", $form_state['values'][$firstname_field]['und'][0]['value'])) {
416 417 418 419
      form_set_error($firstname_field, t('First name cannot end with a space.'));
    }
    if (preg_match("/\\s\\s/", $form_state['values'][$firstname_field]['und'][0]['value'])) {
      form_set_error($firstname_field, t('First name cannot contain two spaces in a row.'));
420 421 422 423 424 425 426 427 428
    }
  }

  // A middle name may not begin or end with a space.
  $middlename_field = variable_get('realname_registration_middlename_field');
  if (variable_get('realname_registration_use_profile2_middlename_field')) {
    if (!empty($form_state['input']['profile_' . variable_get('realname_registration_profile2_middlename_field_profile_name') ][$middlename_field]['und'][0]['value'])) {
      if (preg_match("/^\\s+(.*)$/", $form_state['input']['profile_'
        . variable_get('realname_registration_profile2_middlename_field_profile_name') ][$middlename_field]['und'][0]['value'])) {
429
        form_set_error($middlename_field, t('Middle name cannot begin with a space.'));
430 431 432
      }
      if (preg_match("/^(.*)\\s+$/", $form_state['input']['profile_'
        . variable_get('realname_registration_profile2_middlename_field_profile_name') ][$middlename_field]['und'][0]['value'])) {
433 434 435 436 437
        form_set_error($middlename_field, t('Middle name cannot end with a space.'));
      }
      if (preg_match("/\\s\\s/", $form_state['input']['profile_'
        . variable_get('realname_registration_profile2_middlename_field_profile_name') ][$middlename_field]['und'][0]['value'])) {
        form_set_error($middlename_field, t('Middle name cannot contain two spaces in a row.'));
438 439 440 441 442 443
      }
    }
  }
  else {
    if (!empty($form_state['values'][$middlename_field]['und'][0]['value'])) {
      if (preg_match("/^\\s+(.*)$/", $form_state['values'][$middlename_field]['und'][0]['value'])) {
444
        form_set_error($middlename_field, t('Middle name cannot begin with a space.'));
445 446
      }
      if (preg_match("/^(.*)\\s+$/", $form_state['values'][$middlename_field]['und'][0]['value'])) {
447 448 449 450
        form_set_error($middlename_field, t('Middle name cannot end with a space.'));
      }
      if (preg_match("/\\s\\s/", $form_state['values'][$middlename_field]['und'][0]['value'])) {
        form_set_error($middlename_field, t('Middle name cannot contain two spaces in a row.'));
451 452 453 454 455 456 457 458 459
      }
    }
  }

  // A last name may not begin or end with a space.
  $lastname_field = variable_get('realname_registration_lastname_field');
  if (variable_get('realname_registration_use_profile2_lastname_field')) {
    if (preg_match("/^\\s+(.*)$/", $form_state['input']['profile_'
      . variable_get('realname_registration_profile2_lastname_field_profile_name') ][$lastname_field]['und'][0]['value'])) {
460
      form_set_error($lastname_field, t('Last name cannot begin with a space.'));
461 462 463
    }
    if (preg_match("/^(.*)\\s+$/", $form_state['input']['profile_'
      . variable_get('realname_registration_profile2_lastname_field_profile_name') ][$lastname_field]['und'][0]['value'])) {
464 465 466 467 468
      form_set_error($lastname_field, t('Last name cannot end with a space.'));
    }
    if (preg_match("/\\s\\s/", $form_state['input']['profile_'
      . variable_get('realname_registration_profile2_lastname_field_profile_name') ][$lastname_field]['und'][0]['value'])) {
      form_set_error($lastname_field, t('Last name cannot contain two spaces in a row.'));
469 470 471 472
    }
  }
  else {
    if (preg_match("/^\\s+(.*)$/", $form_state['values'][$lastname_field]['und'][0]['value'])) {
473
      form_set_error($lastname_field, t('Last name cannot begin with a space.'));
474 475
    }
    if (preg_match("/^(.*)\\s+$/", $form_state['values'][$lastname_field]['und'][0]['value'])) {
476 477 478 479
      form_set_error($lastname_field, t('Last name cannot end with a space.'));
    }
    if (preg_match("/\\s\\s/", $form_state['values'][$lastname_field]['und'][0]['value'])) {
      form_set_error($lastname_field, t('Last name cannot contain two spaces in a row.'));
480 481 482 483
    }
  }
}

484 485 486 487 488 489 490
/**
 * Implementation of hook_menu().
 */
function realname_registration_menu() {
  $items = array();

  $items['admin/config/people/realname_registration'] = array(
491 492
    'title' => 'Realname registration',
    'description' => 'Administer settings for Realname registration.',
493 494 495 496 497 498 499
    'page callback' => 'drupal_get_form',
    'page arguments' => array('realname_registration_settings_form'),
    'access arguments' => array('access administration pages'),
    'file' => 'realname_registration.admin.inc',
   );

  return $items;
500
}