You are here

public static function EntityconnectFormUtils::validateForm in Entity connect 8.2

Form API #validate callback for a form with entity_reference fields.

Removes the entityconnect button values from form_state to prevent exceptions.

Parameters

array $form: The form to validate.

\Drupal\Core\Form\FormStateInterface $form_state: The state of the form.

File

src/EntityconnectFormUtils.php, line 90

Class

EntityconnectFormUtils
Contains form alter, callbacks and utility methods for entityconnect.

Namespace

Drupal\entityconnect

Code

public static function validateForm(array &$form, FormStateInterface $form_state) {
  $ref_fields = static::getReferenceFields($form, $form_state);
  foreach ($ref_fields as $field) {

    // Extract the values for this field from $form_state->getValues().
    $path = array_merge($form['#parents'], [
      $field,
    ]);
    $key_exists = NULL;
    $ref_values = NestedArray::getValue($form_state
      ->getValues(), $path, $key_exists);
    if ($key_exists) {
      foreach ($ref_values as $key => $value) {
        if (strpos($key, '_entityconnect') !== FALSE) {
          $form_state
            ->unsetValue(array_merge($path, [
            $key,
          ]));
        }
      }
    }
  }
}