You are here

function redhen_contact_form_user_register_form_alter in RedHen CRM 8

Same name and namespace in other branches
  1. 7 modules/redhen_contact/redhen_contact.module \redhen_contact_form_user_register_form_alter()

Implements hook_form_FORM_ID_alter() on the user_register_form.

File

modules/redhen_contact/redhen_contact.module, line 191
Contains redhen_contact.module..

Code

function redhen_contact_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Get Redhen Contact settings.
  $config = \Drupal::config('redhen_contact.settings');

  // Check whether we should create a Contact on User registration.
  if ($config
    ->get('connect_users')) {

    // Get menu item to check for overridden Contact Type parameter, but only
    // when a user is registering from user/register.
    $url_exploded = array_slice(explode('/', \Drupal::request()
      ->getRequestUri()), 1);
    if ($url_exploded[0] == 'user' && $url_exploded[1] == 'register' && isset($url_exploded[2])) {
      $contact_type = $url_exploded[2];
    }
    else {

      // If a parameter was not passed, use the default contact type.
      $contact_type = $config
        ->get('registration_type');
    }

    // If a valid contact type was found, embed fields from the Contact Type on
    // the user registration form.
    $types = redhen_contact_type_options_list();
    if (array_key_exists($contact_type, $types)) {
      $contact_object = Contact::create([
        'type' => $contact_type,
      ]);
      _redhen_contact_user_embed_contact_form($form, $form_state, $contact_object, $config
        ->get('registration_form'));

      // Add a validation handler for validating the Contact form data.
      $form['#validate'][] = 'redhen_contact_user_registration_validate';

      // Add a submit handler for handling the Contact form data.
      $form['actions']['submit']['#submit'][] = 'redhen_contact_user_registration_submit';

      // Hide the Contact email field, we will use the user mail field.
      $form['redhen_contact_' . $contact_type]['email']['#access'] = FALSE;
    }
    else {
      \Drupal::messenger()
        ->addMessage(t('Invalid RedHen contact type parameter.'));
    }
  }
}