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(); } } }