You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
3.8 KiB
147 lines
3.8 KiB
<?php |
|
|
|
declare(strict_types=1); |
|
|
|
namespace Drupal\doi_prefill\Form; |
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
use Drupal\Core\Form\FormBase; |
|
use Drupal\Core\Form\FormStateInterface; |
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
use Drupal\doi_prefill\CrossrefApiReader; |
|
use Drupal\doi_prefill\NodeBuilder; |
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
|
|
/** |
|
* Provides a DOI Prefill form. |
|
*/ |
|
final class DoiPrepopulateForm extends FormBase { |
|
|
|
/** |
|
* The DOI API reader. |
|
* |
|
* @var \Drupal\doi_prefill\CrossrefApiReader |
|
*/ |
|
protected $doiApi; |
|
|
|
/** |
|
* The Node builder. |
|
* |
|
* @var Drupal\doi_prefill\NodeBuilder |
|
*/ |
|
protected $nodeBuilder; |
|
|
|
/** |
|
* The entity type manager service. |
|
* |
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
|
*/ |
|
protected $entityTypeManager; |
|
|
|
/** |
|
* The constructor. |
|
* |
|
* @param \Drupal\doi_prefill\CrossrefApiReader $doiApi |
|
* The Api reader. |
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
|
* The EntityTypeManager. |
|
*/ |
|
public function __construct(CrossrefApiReader $doiApi, EntityTypeManagerInterface $entityTypeManager, NodeBuilder $nodeBuilder) { |
|
$this->doiApi = $doiApi; |
|
$this->entityTypeManager = $entityTypeManager; |
|
$this->nodeBuilder = $nodeBuilder; |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public static function create(ContainerInterface $container) { |
|
// Instantiates this form class. |
|
return new static( |
|
// Load the service required to construct this class. |
|
$container->get('doi_prefill.crossref_api_reader'), |
|
$container->get('entity_type.manager'), |
|
$container->get('doi_prefill.node_builder') |
|
); |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function getFormId(): string { |
|
return 'doi_prefill_doi_prepopulate'; |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function buildForm(array $form, FormStateInterface $form_state): array { |
|
$terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadByProperties([ |
|
'name' => 'Collection', |
|
'vid' => 'islandora_models', |
|
]); |
|
$term = reset($terms); |
|
$collection_ids = \Drupal::entityQuery('node') |
|
->accessCheck(FALSE) |
|
->condition('field_model', $term->id()) |
|
->execute(); |
|
$collections = $this->entityTypeManager->getStorage('node')->loadMultiple($collection_ids); |
|
$options = []; |
|
foreach ($collections as $id => $collection) { |
|
$options[$id] = $collection->label(); |
|
} |
|
|
|
$form['container'] = [ |
|
'#type' => 'container', |
|
'#attributes' => ['class' => ['form-inline']], |
|
]; |
|
$form['container']['doi'] = [ |
|
'#type' => 'textfield', |
|
'#title' => $this->t('Enter DOI'), |
|
'#required' => TRUE, |
|
]; |
|
$form['container']['collection'] = [ |
|
'#title' => $this->t('Collection'), |
|
'#type' => 'select', |
|
'#options' => $options, |
|
'#required' => TRUE, |
|
|
|
]; |
|
$form['container']['redirect'] = [ |
|
'#type' => 'select', |
|
'#title' => $this->t("After submission?"), |
|
'#options' => [ |
|
'edit' => $this->t('Edit after submission'), |
|
'resume' => $this->t('Return to this form'), |
|
], |
|
'#default_value' => 'edit', |
|
]; |
|
$form['#attached']['library'][] = 'doi_prefill/styles'; |
|
|
|
$form['actions'] = [ |
|
'#type' => 'actions', |
|
'submit' => [ |
|
'#type' => 'submit', |
|
'#value' => $this->t('Send'), |
|
], |
|
]; |
|
|
|
return $form; |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function submitForm(array &$form, FormStateInterface $form_state): void { |
|
$doi = trim($form_state->getValue('doi')); |
|
$collection = $form_state->getValue('collection'); |
|
$nid = $this->nodeBuilder->buildNode($collection, $doi); |
|
|
|
if ($form_state->getValue('redirect') == 'edit') { |
|
$destination = "/node/{$nid}/edit"; |
|
$response = new RedirectResponse($destination); |
|
$response->send(); |
|
} |
|
} |
|
|
|
}
|
|
|