You are here

function redhen_contact_user_registration_form_state in RedHen CRM 8

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

Return form_state that includes the values from the visible form elements.

Parameters

array $form: The RedHen contact form.

\Drupal\Core\Form\FormStateInterface $form_state: The RedHen contact form state.

string $contact_type: The RedHen Contact Type.

Return value

\Drupal\Core\Form\FormStateInterface The modified form_state.

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

File

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

Code

function redhen_contact_user_registration_form_state(array $form, FormStateInterface $form_state, $contact_type) {

  // Start with form_state as passed.
  $limited_state = clone $form_state;

  // Reset values element since we'll be rebuilding it below.
  $field_parent_name = 'form_display_' . $form_state
    ->get('redhen_contact')
    ->getType();
  $limited_state
    ->setValues([
    $field_parent_name => [],
  ]);

  // Get Redhen Contact field definitions to determine default values.
  $contact_fields = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions('redhen_contact', $contact_type);

  // Get all Redhen contact field values.
  $field_values = $form_state
    ->getValue($field_parent_name);
  $limited_field_values = [];

  // Loop through all Redhen contact field values.
  foreach ($field_values as $key => $value) {

    // Skip un-accessible children.
    if (isset($form[$key]['#access']) && !$form[$key]['#access']) {
      continue;
    }

    // Skip any empty fields.
    if (!isset($contact_fields[$key]) || !redhen_contact_form_field_has_value($contact_fields[$key], $value)) {
      continue;
    }

    // Add fields with new values to limited field values.
    $limited_field_values[$key] = $value;
  }

  // Rebuild form state with limited field values.
  $limited_state
    ->setValue($field_parent_name, $limited_field_values);
  return $limited_state;
}