You are here

function _redhen_contact_user_submission_apply in RedHen CRM 8

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

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

This happens when a RedHen Contact form is embedded in a User form via a form alter hook, then the from is submitted. Note that this function updates the $contact, but does not save the changes.

Parameters

array $form: Form array.

array $form_state: Form state array.

EntityFormDisplay $form_display: EntityFormDisplay to extract field values from.

\Drupal\redhen_contact\Entity\Contact $contact: RedhenContact to update.

bool $limit_values: Whether to limit updated values to non-null fields.

Return value

string Status message.

1 call to _redhen_contact_user_submission_apply()
_redhen_contact_user_submission_validate in modules/redhen_contact/redhen_contact.module
Helper function for validating Contact Form values submitted via User forms.

File

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

Code

function _redhen_contact_user_submission_apply($form, $form_state, $form_display, Contact $contact, $limit_values = FALSE) {

  // Limit field values updated on the Contact to only the fields with non-empty
  // values from the submitted form. Use this option if you don't want to
  // clobber pre-existing field values with empty field values from the form.
  if ($limit_values) {
    $value_state = redhen_contact_user_registration_form_state($form, $form_state, $contact
      ->getType());
  }
  else {
    $value_state = $form_state;
  }

  // We always update fields if the User and Contact are already linked. So, if
  // the submission came from the user_form, we apply the submitted field
  // values.
  // We only update fields on user registration if settings permit or the
  // Contact is new. To determine this, we check whether the submission came
  // from the user_register_form and if it did, we check the setting that
  // determines whether we update fields on User registration.
  $form_id = $form_state
    ->getBuildInfo()['form_id'];
  $registration_update = \Drupal::config('redhen_contact.settings')
    ->get('registration_update');
  if ($form_id == 'user_form' || $registration_update && $form_id == 'user_register_form' || $contact
    ->isNew()) {
    $values = $value_state
      ->getValues()['form_display_' . $contact
      ->getType()];
    foreach ($values as $field => $value) {
      if ($contact
        ->hasField($field)) {
        $widget = $form_display
          ->getRenderer($field);
        if ($widget) {

          // Let the widget massage the submitted values.
          $value = $widget
            ->massageFormValues($value, $form, $form_state);
        }
        $contact
          ->set($field, $value);
      }
    }
  }
  return $contact;
}