You are here

function _redhen_contact_user_submission_validate in RedHen CRM 8

Helper function for validating Contact Form values submitted via User forms.

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

\Drupal\redhen_contact\Entity\Contact $contact:

2 calls to _redhen_contact_user_submission_validate()
redhen_contact_user_registration_validate in modules/redhen_contact/redhen_contact.module
User registration form RedHen Contact validation handler.
redhen_contact_user_update_validate in modules/redhen_contact/redhen_contact.module
User edit form RedHen Contact validation handler.

File

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

Code

function _redhen_contact_user_submission_validate($form, &$form_state, Contact $contact) {

  // Get submitted field values.

  /** @var $form_display \Drupal\core\Entity\Entity\EntityFormDisplay */
  $form_display = $form_state
    ->get('form_display_' . $contact
    ->getType());

  // Update Contact entity with values submitted on the form.
  // Do NOT overwrite fields with values with empty field values if the
  // submitted form is user_register_form. Do overwrite fields with values with
  // empty field values if the submitted form is the user_edit form.
  // This allows the user registration form to remain simple (i.e. not all
  // Contact fields shown) without clobbering existing field data for a Contact
  // while allowing the User edit form to function as expected (i.e. you can
  // delete field values).
  $form_id = $form_state
    ->getBuildInfo()['form_id'];
  if ($form_id == 'user_register_form') {
    $contact = _redhen_contact_user_submission_apply($form, $form_state, $form_display, $contact, TRUE);
  }
  elseif ($form_id == 'user_form') {
    $contact = _redhen_contact_user_submission_apply($form, $form_state, $form_display, $contact);
  }

  // Entity was validated in entityFormValidate(). This will prevent validation
  // exception from being thrown.
  $form_state
    ->getFormObject()
    ->getEntity()
    ->validate();

  // Update Contact stored in form_state to be our chosen Contact (i.e.
  // pre-existing Contact with matching email address or new Contact) with its
  // field values updated from the values supplied in the form.
  $form_state
    ->set('redhen_contact', $contact);
}