From c2cf5009a11805fb63b162c9504e3c6494aa4c46 Mon Sep 17 00:00:00 2001 From: ajstanley Date: Fri, 7 Feb 2025 18:53:04 +0000 Subject: [PATCH] Added configuration --- config/install/doi_prefill.settings.yml | 20 +++++++++ config/schema/doi_prefill.schema.yml | 60 +++++++++++++++++++++++++ css/doi_prefill.css | 3 +- doi_prefill.routing.yml | 8 ++++ doi_prefill.services.yml | 2 +- src/CrossrefApiReader.php | 1 - src/Form/DoiPrepopulateForm.php | 2 +- src/NodeBuilder.php | 56 ++++++++++------------- 8 files changed, 115 insertions(+), 37 deletions(-) create mode 100644 config/install/doi_prefill.settings.yml create mode 100644 config/schema/doi_prefill.schema.yml diff --git a/config/install/doi_prefill.settings.yml b/config/install/doi_prefill.settings.yml new file mode 100644 index 0000000..c4851b2 --- /dev/null +++ b/config/install/doi_prefill.settings.yml @@ -0,0 +1,20 @@ +field_settings: + title: 'title' + contributors: 'field_contributors' + publisher: 'field_publisher' + doi: 'field_doi' + genre: 'field_genre' + issue: 'field_issue' + volume: 'field_volume' + date_issued: 'field_edtf_date_issued' + abstract: 'field_abstract' + host_title: 'field_host_title' + date_online: 'field_date_online' + page_range: 'field_page_range' + series_issn: 'field_series_issn' +doi_term_islandora_term_pairs: + - key: 'article-journal' + value: 'Journal Article' + - key: 'monograph' + value: 'Book' + diff --git a/config/schema/doi_prefill.schema.yml b/config/schema/doi_prefill.schema.yml new file mode 100644 index 0000000..ffe6ea8 --- /dev/null +++ b/config/schema/doi_prefill.schema.yml @@ -0,0 +1,60 @@ +doi_prefill.settings: + type: config_object + label: 'DOI Prefill Settings' + mapping: + field_settings: + type: mapping + label: 'Field Mappings' + mapping: + title: + type: string + label: 'Title' + contributors: + type: string + label: 'Contributors' + publisher: + type: string + label: 'Publisher' + doi: + type: string + label: 'DOI' + genre: + type: string + label: 'Genre' + issue: + type: string + label: 'Issue' + volume: + type: string + label: 'Volume' + date_issued: + type: string + label: 'Date Issued' + abstract: + type: string + label: 'Abstract' + host_title: + type: string + label: 'Host Title' + date_online: + type: string + label: 'Date Online' + page_range: + type: string + label: 'Page Range' + series_issn: + type: string + label: 'Series ISSN' + doi_term_islandora_term_pairs: + type: sequence + label: 'DOI Term - Islandora Term Pairs' + sequence: + type: mapping + mapping: + key: + type: string + label: 'DOI Term' + value: + type: string + label: 'Islandora Term' + diff --git a/css/doi_prefill.css b/css/doi_prefill.css index 8874d43..8caeed0 100644 --- a/css/doi_prefill.css +++ b/css/doi_prefill.css @@ -6,8 +6,7 @@ justify-content: space-evenly; } - .form-inline .form-item { flex: 1; min-width: 250px; -} +} \ No newline at end of file diff --git a/doi_prefill.routing.yml b/doi_prefill.routing.yml index 5a91dd6..d3b8e68 100644 --- a/doi_prefill.routing.yml +++ b/doi_prefill.routing.yml @@ -5,3 +5,11 @@ doi_prefill.doi_prepopulate: _form: 'Drupal\doi_prefill\Form\DoiPrepopulateForm' requirements: _permission: 'create islandora_object content' + +doi_prefill.doi_settings: + path: '/admin/config/system/doi_field_settings' + defaults: + _title: 'DOI Prefill Settings' + _form: 'Drupal\doi_prefill\Form\DOIFieldSettingsForm' + requirements: + _permission: 'administer site configuration' diff --git a/doi_prefill.services.yml b/doi_prefill.services.yml index dacd1b3..2da0821 100644 --- a/doi_prefill.services.yml +++ b/doi_prefill.services.yml @@ -5,4 +5,4 @@ services: doi_prefill.node_builder: class: Drupal\doi_prefill\NodeBuilder - arguments: ['@entity_type.manager', '@doi_prefill.crossref_api_reader'] + arguments: ['@entity_type.manager', '@doi_prefill.crossref_api_reader', '@config.factory'] diff --git a/src/CrossrefApiReader.php b/src/CrossrefApiReader.php index 910c398..f929d33 100644 --- a/src/CrossrefApiReader.php +++ b/src/CrossrefApiReader.php @@ -32,7 +32,6 @@ final class CrossrefApiReader { $endpoint = 'https://api.crossref.org/works'; $encoded_doi = urlencode($identifier); $url = "{$endpoint}/{$encoded_doi}"; - try { $response = $this->httpClient->get($url, [ 'headers' => [ diff --git a/src/Form/DoiPrepopulateForm.php b/src/Form/DoiPrepopulateForm.php index 4c9c6eb..cd9b80f 100644 --- a/src/Form/DoiPrepopulateForm.php +++ b/src/Form/DoiPrepopulateForm.php @@ -64,7 +64,7 @@ final class DoiPrepopulateForm extends FormBase { // 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') + $container->get('doi_prefill.node_builder'), ); } diff --git a/src/NodeBuilder.php b/src/NodeBuilder.php index c2e8241..ba8647a 100644 --- a/src/NodeBuilder.php +++ b/src/NodeBuilder.php @@ -7,30 +7,20 @@ namespace Drupal\doi_prefill; use Drupal\Core\Entity\EntityTypeManager; use Drupal\node\Entity\Node; use Drupal\taxonomy\Entity\Term; +use Drupal\Core\Config\ConfigFactory; /** * Class to construct nodes from crossref DOI harvest. */ final class NodeBuilder { - /** - * Mapping of content types to their respective classes. - * - * @var arraystringstring - */ - protected $mapping = [ - 'journal-article' => 'Journal Article', - 'book-chapter' => 'Book, Section', - 'monograph' => 'Book', - 'proceedings-article' => 'Conference Proceedings', - ]; - /** * Constructs a CrossrefApiReader object. */ public function __construct( private readonly EntityTypeManager $entityTypeManager, private readonly CrossrefApiReader $doiApi, + private readonly ConfigFactory $config ) {} /** @@ -46,6 +36,13 @@ final class NodeBuilder { */ public function buildNode($collection, $doi) { $contents = $this->doiApi->getWork($doi); + $config = $this->config->get('doi_prefill.settings'); + $field_settings = $config->get('field_settings'); + $mapping = $config->get('doi_term_islandora_term_pairs'); + $term_mappings = []; + foreach ($mapping as $mapping => $values) { + $term_mappings[$values['key']] = $values['value']; + } // Build typed relations. $typed_relations = []; @@ -61,42 +58,42 @@ final class NodeBuilder { 'rel_type' => 'relators:aut', ]; } - $type = $this->mapping[$contents['type']] ?? $contents['type']; + $type = $term_mappings[$contents['type']] ?? $contents['type']; $genre = $this->getOrCreateTerm($type, 'genre'); // Build new node. $new_node = Node::create([ - 'title' => $contents['title'][0], + $field_settings['title'] => $contents['title'][0], 'field_member_of' => $collection, - 'type' => 'islandora_object', - 'field_contributors' => $typed_relations, - 'field_publisher' => $contents['publisher'] ?? '', - 'field_doi' => $doi, - 'field_genre' => $genre->id(), - 'field_issue' => $contents['issue'] ?? '', - 'field_volume' => $contents['volume'] ?? '', - 'field_date_issued' => $contents['created']['date-parts'][0][0] ?? '', + 'type' => $config->get('content_type'), + $field_settings['contributors'] => $typed_relations, + $field_settings['publisher'] => $contents['publisher'] ?? '', + $field_settings['doi'] => $doi, + $field_settings['genre'] => $genre->id(), + $field_settings['issue'] => $contents['issue'] ?? '', + $field_settings['volume'] => $contents['volume'] ?? '', + $field_settings['date_issued'] => $contents['created']['date-parts'][0][0] ?? '', ]); // Optional fields. if (isset($contents['abstract'])) { - $new_node->set('field_abstract', [ + $new_node->set($field_settings['abstract'], [ 'value' => $contents['abstract'], 'format' => 'basic_html', ]); } if (isset($contents['container-title'])) { - $new_node->set('field_host_title', $contents['container-title'][0]); + $new_node->set($field_settings['host_title'], $contents['container-title'][0]); } if (isset($contents['published-online'])) { $field_date_online = []; foreach ($contents['published-online']['date-parts'] as $date_parts) { $field_date_online[] = ['value' => implode('-', $date_parts)]; } - $new_node->set('field_date_online', $field_date_online); + $new_node->set($field_settings['date_online'], $field_date_online); } if (isset($contents['page'])) { - $new_node->set('field_page_range', $contents['page']); + $new_node->set($field_settings['page_range'], $contents['page']); } // Multivalued fields. @@ -104,7 +101,7 @@ final class NodeBuilder { foreach (($contents['ISSN'] ?? []) as $issn) { $field_series_issn[] = ['value' => $issn]; } - $new_node->set('field_series_issn', $field_series_issn); + $new_node->set($field_settings['series_issn'], $field_series_issn); $new_node->save(); return $new_node->id(); @@ -126,20 +123,15 @@ final class NodeBuilder { 'name' => $term_name, 'vid' => $vocabulary, ]); - if ($terms) { - // Return the first found term. return reset($terms); } - - // If the term does not exist, create it. $term = Term::create([ 'name' => $term_name, 'vid' => $vocabulary, ]); $term->save(); return $term; - } }