You are here

public function PopulateForm::validateForm in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_import/src/Form/PopulateForm.php \Drupal\bibcite_import\Form\PopulateForm::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

modules/bibcite_import/src/Form/PopulateForm.php, line 107

Class

PopulateForm
Populate values to Reference form.

Namespace

Drupal\bibcite_import\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $format_id = $form_state
    ->getValue('format');
  $data = $form_state
    ->getValue('data');
  $format = $this->formatManager
    ->getDefinition($format_id);
  try {
    $decoded = $this->serializer
      ->decode($data, $format_id);
    $config = \Drupal::config('bibcite_import.settings');
    $denormalize_context = [
      'contributor_deduplication' => $config
        ->get('settings.contributor_deduplication'),
      'keyword_deduplication' => $config
        ->get('settings.keyword_deduplication'),
    ];
    $entity = $this->serializer
      ->denormalize(reset($decoded), Reference::class, $format_id, $denormalize_context);
    $form_state
      ->setValue('entity', $entity);
  } catch (\Exception $exception) {
    $err_string = $this
      ->t('@format entry is not valid. Please check pasted text.<br>%ex', [
      '@format' => $format['label'],
      '%ex' => $exception
        ->getMessage(),
    ]);
    $form_state
      ->setErrorByName('data', $err_string);
  }
}