You are here

public function EntityQueryMapImportForm::validateForm in GraphQL 8.3

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

src/Form/EntityQueryMapImportForm.php, line 42

Class

EntityQueryMapImportForm
Form controller for GraphQL query map forms.

Namespace

Drupal\graphql\Form

Code

public function validateForm(array &$form, FormStateInterface $formState) {
  $files = file_save_upload('query_map_json', [
    'file_validate_extensions' => [
      'json',
    ],
  ]);

  /** @var \Drupal\file\FileInterface $file */
  if (empty($files) || !($file = reset($files)) || !$file instanceof FileInterface) {
    $formState
      ->setError($form['query_map_json'], $this
      ->t('No file was uploaded.'));
  }
  else {

    // Save the file for use in the submit handler.
    $formState
      ->set('file', $file);
    $map = file_get_contents($file
      ->getFileUri());
    $version = sha1($map);
    if (QueryMap::exists($version)) {
      $formState
        ->setError($form['query_map_json'], $this
        ->t('A query map with the same version @version already exists.', [
        '@version' => $version,
      ]));
    }
  }
}