You are here

function redhen_contact_user_registration_submit in RedHen CRM 8

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

User registration form RedHen Contact submit handler.

Links a Contact to a Drupal user on user registration. Contact will a pre-existing Contact if Redhen Contact admin setting to link user to existing Contact is TRUE and we were able to find a Contact with an email that matches the user being created. If either of these conditions is false, the Contact will be created new.

Parameters

array $form: Form array.

array $form_state: Form state array.

1 string reference to 'redhen_contact_user_registration_submit'
redhen_contact_form_user_register_form_alter in modules/redhen_contact/redhen_contact.module
Implements hook_form_FORM_ID_alter() on the user_register_form.

File

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

Code

function redhen_contact_user_registration_submit($form, &$form_state) {

  // Load Contact
  $contact = $form_state
    ->get('redhen_contact');

  // Connect Drupal user to Redhen Contact.
  // We know this should happen on submit without checking anything because
  // we only embed redhen_contact fields on the user_registration form if
  // redhen_contact.settings.connect_users is TRUE.
  // See redhen_contact_form_user_register_form_alter().
  $contact
    ->setUserId($form_state
    ->getFormObject()
    ->getEntity()
    ->id());

  // Set Contact's email address to that of the new User.
  $contact
    ->setEmail($form_state
    ->getValue('mail'));

  // Save Contact associated with the user being created.
  $contact
    ->save();

  // Add message that new User was linked to a Contact.
  $message = t('User has been linked to the contact %name.', [
    '%name' => $contact
      ->label(),
  ]);

  // Update form_state Contact for later processing.
  $form_state
    ->set('redhen_contact', $contact);

  // Only display this message to CRM admins to avoid confusion.
  $user = Drupal::currentUser();
  if ($user
    ->hasPermission('administer contact entities')) {
    \Drupal::messenger()
      ->addMessage($message);
  }
}