Browse Source

added validator

main
ajstanley 11 months ago
parent
commit
daa166ad5e
  1. 21
      src/Form/DoiPrepopulateForm.php

21
src/Form/DoiPrepopulateForm.php

@ -72,6 +72,23 @@ final class DoiPrepopulateForm extends FormBase {
return 'doi_prefill_doi_prepopulate';
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$doi = trim($form_state->getValue('doi'));
if (!empty($doi)) {
$existing_nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
'field_doi' => $doi,
]);
if (!empty($existing_nodes)) {
$form_state->setErrorByName('doi', $this->t('A node with this DOI already exists.'));
}
}
}
/**
* {@inheritdoc}
*/
@ -134,6 +151,10 @@ final class DoiPrepopulateForm extends FormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$doi = trim($form_state->getValue('doi'));
$existing_nodes = $this->entityTypeManager->getStorage('node')->loadByProperties([
'field_doi' => $doi,
]);
$collection = $form_state->getValue('collection');
$nid = $this->nodeBuilder->buildNode($collection, $doi);

Loading…
Cancel
Save