You are here

public function EntityCollector::validateForm in Relation 8.2

Same name and namespace in other branches
  1. 8 relation_entity_collector/src/Form/EntityCollector.php \Drupal\relation_entity_collector\Form\EntityCollector::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormBase::validateForm

File

relation_entity_collector/src/Form/EntityCollector.php, line 179
Contains \Drupal\relation_entity_collector\Form\EntityCollector.

Class

EntityCollector
Provides a entity collector form.

Namespace

Drupal\relation_entity_collector\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  switch ($form_state['triggering_element']['#value']) {
    case t('Pick'):

      // Require values.
      $relation_type = $form_state['values']['relation_type'];
      $entity_key = $form_state['values']['entity_key'];
      $errors = FALSE;
      if (empty($relation_type)) {
        form_set_error('relation_type', t('Please select a relation type.'));
        $errors = TRUE;
      }
      if (empty($entity_key)) {
        form_set_error('entity_key', t('Please select an entity.'));
        $errors = TRUE;
      }

      // If either of these are not selected we can not continue.
      if ($errors) {
        return;
      }

      // Get entity info from key ('{entity_type}:{entity_id}').
      list($entity_type, $entity_id) = explode(':', $entity_key);

      // Add the label for later display. #options is check_plain'd but we need
      // to do that ourselves.
      $entity_label = check_plain($form['entity_key']['#options'][$entity_key]);

      // Indexes are added in ascending order, starting from 0.
      $_SESSION += array(
        'relation_entity_keys' => array(),
      );
      $next_index = count($_SESSION['relation_entity_keys']);

      // If validation succeeds we will add this in the submit handler.
      $form_state['pick'] = array(
        'delta' => $next_index,
        'entity_key' => $entity_key,
        'entity_label' => $entity_label,
        'entity_type' => $entity_type,
        'entity_id' => $entity_id,
      );
      $endpoints = $_SESSION['relation_entity_keys'];
      $endpoints[] = $form_state['pick'];
      $relation = _relation_entity_collector_get_entity($form_state['values']['relation_type'], $endpoints);
      $relation->in_progress = TRUE;
      _relation_entity_collector_endpoints_validate($relation, $form, $form_state);
      field_attach_form_validate('relation', $relation, $form, $form_state);
      break;
    case t('Save relation'):
      _relation_entity_collector_endpoints_validate(_relation_entity_collector_get_entity(), $form, $form_state);
      break;
  }
}