You are here

public static function Registrants::validateExisting in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 src/Element/Registrants.php \Drupal\rng\Element\Registrants::validateExisting()
  2. 8 src/Element/Registrants.php \Drupal\rng\Element\Registrants::validateExisting()

Validate adding existing entity sub-form.

Parameters

array $form: The complete form.

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

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Element/Registrants.php, line 828

Class

Registrants
Provides a form element for a registrant and person association.

Namespace

Drupal\rng\Element

Code

public static function validateExisting(array &$form, FormStateInterface $form_state) {
  $element = RegistrantsElement::findElement($form, $form_state);
  $utility = new RegistrantsElement($element, $form_state);
  $utility
    ->buildRegistrant(TRUE);
  $autocomplete_tree = array_merge($element['#parents'], [
    'entities',
    'person',
    'existing',
    'existing_autocomplete',
  ]);
  $element_existing = NestedArray::getValue($element, [
    'entities',
    'person',
    'existing',
    'existing_autocomplete',
  ]);
  $existing_entity_type = $element_existing['#target_type'];
  $existing_value = NestedArray::getValue($form_state
    ->getTemporaryValue('_registrants_values'), $autocomplete_tree);
  if (!empty($existing_value)) {
    $identity = \Drupal::entityTypeManager()
      ->getStorage($existing_entity_type)
      ->load($existing_value);
    if ($utility
      ->identityExists($identity)) {
      $form_state
        ->setError(NestedArray::getValue($form, $autocomplete_tree), t('Person is already on this registration.'));
    }
  }
  else {
    $form_state
      ->setError(NestedArray::getValue($form, $autocomplete_tree), t('Choose a person.'));
  }
}