Browse Source

Added configuration

main
ajstanley 10 months ago
parent
commit
c2cf5009a1
  1. 20
      config/install/doi_prefill.settings.yml
  2. 60
      config/schema/doi_prefill.schema.yml
  3. 3
      css/doi_prefill.css
  4. 8
      doi_prefill.routing.yml
  5. 2
      doi_prefill.services.yml
  6. 1
      src/CrossrefApiReader.php
  7. 2
      src/Form/DoiPrepopulateForm.php
  8. 56
      src/NodeBuilder.php

20
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'

60
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'

3
css/doi_prefill.css

@ -6,8 +6,7 @@
justify-content: space-evenly; justify-content: space-evenly;
} }
.form-inline .form-item { .form-inline .form-item {
flex: 1; flex: 1;
min-width: 250px; min-width: 250px;
} }

8
doi_prefill.routing.yml

@ -5,3 +5,11 @@ doi_prefill.doi_prepopulate:
_form: 'Drupal\doi_prefill\Form\DoiPrepopulateForm' _form: 'Drupal\doi_prefill\Form\DoiPrepopulateForm'
requirements: requirements:
_permission: 'create islandora_object content' _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'

2
doi_prefill.services.yml

@ -5,4 +5,4 @@ services:
doi_prefill.node_builder: doi_prefill.node_builder:
class: Drupal\doi_prefill\NodeBuilder 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']

1
src/CrossrefApiReader.php

@ -32,7 +32,6 @@ final class CrossrefApiReader {
$endpoint = 'https://api.crossref.org/works'; $endpoint = 'https://api.crossref.org/works';
$encoded_doi = urlencode($identifier); $encoded_doi = urlencode($identifier);
$url = "{$endpoint}/{$encoded_doi}"; $url = "{$endpoint}/{$encoded_doi}";
try { try {
$response = $this->httpClient->get($url, [ $response = $this->httpClient->get($url, [
'headers' => [ 'headers' => [

2
src/Form/DoiPrepopulateForm.php

@ -64,7 +64,7 @@ final class DoiPrepopulateForm extends FormBase {
// Load the service required to construct this class. // Load the service required to construct this class.
$container->get('doi_prefill.crossref_api_reader'), $container->get('doi_prefill.crossref_api_reader'),
$container->get('entity_type.manager'), $container->get('entity_type.manager'),
$container->get('doi_prefill.node_builder') $container->get('doi_prefill.node_builder'),
); );
} }

56
src/NodeBuilder.php

@ -7,30 +7,20 @@ namespace Drupal\doi_prefill;
use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Entity\EntityTypeManager;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Config\ConfigFactory;
/** /**
* Class to construct nodes from crossref DOI harvest. * Class to construct nodes from crossref DOI harvest.
*/ */
final class NodeBuilder { 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. * Constructs a CrossrefApiReader object.
*/ */
public function __construct( public function __construct(
private readonly EntityTypeManager $entityTypeManager, private readonly EntityTypeManager $entityTypeManager,
private readonly CrossrefApiReader $doiApi, private readonly CrossrefApiReader $doiApi,
private readonly ConfigFactory $config
) {} ) {}
/** /**
@ -46,6 +36,13 @@ final class NodeBuilder {
*/ */
public function buildNode($collection, $doi) { public function buildNode($collection, $doi) {
$contents = $this->doiApi->getWork($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. // Build typed relations.
$typed_relations = []; $typed_relations = [];
@ -61,42 +58,42 @@ final class NodeBuilder {
'rel_type' => 'relators:aut', 'rel_type' => 'relators:aut',
]; ];
} }
$type = $this->mapping[$contents['type']] ?? $contents['type']; $type = $term_mappings[$contents['type']] ?? $contents['type'];
$genre = $this->getOrCreateTerm($type, 'genre'); $genre = $this->getOrCreateTerm($type, 'genre');
// Build new node. // Build new node.
$new_node = Node::create([ $new_node = Node::create([
'title' => $contents['title'][0], $field_settings['title'] => $contents['title'][0],
'field_member_of' => $collection, 'field_member_of' => $collection,
'type' => 'islandora_object', 'type' => $config->get('content_type'),
'field_contributors' => $typed_relations, $field_settings['contributors'] => $typed_relations,
'field_publisher' => $contents['publisher'] ?? '', $field_settings['publisher'] => $contents['publisher'] ?? '',
'field_doi' => $doi, $field_settings['doi'] => $doi,
'field_genre' => $genre->id(), $field_settings['genre'] => $genre->id(),
'field_issue' => $contents['issue'] ?? '', $field_settings['issue'] => $contents['issue'] ?? '',
'field_volume' => $contents['volume'] ?? '', $field_settings['volume'] => $contents['volume'] ?? '',
'field_date_issued' => $contents['created']['date-parts'][0][0] ?? '', $field_settings['date_issued'] => $contents['created']['date-parts'][0][0] ?? '',
]); ]);
// Optional fields. // Optional fields.
if (isset($contents['abstract'])) { if (isset($contents['abstract'])) {
$new_node->set('field_abstract', [ $new_node->set($field_settings['abstract'], [
'value' => $contents['abstract'], 'value' => $contents['abstract'],
'format' => 'basic_html', 'format' => 'basic_html',
]); ]);
} }
if (isset($contents['container-title'])) { 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'])) { if (isset($contents['published-online'])) {
$field_date_online = []; $field_date_online = [];
foreach ($contents['published-online']['date-parts'] as $date_parts) { foreach ($contents['published-online']['date-parts'] as $date_parts) {
$field_date_online[] = ['value' => implode('-', $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'])) { if (isset($contents['page'])) {
$new_node->set('field_page_range', $contents['page']); $new_node->set($field_settings['page_range'], $contents['page']);
} }
// Multivalued fields. // Multivalued fields.
@ -104,7 +101,7 @@ final class NodeBuilder {
foreach (($contents['ISSN'] ?? []) as $issn) { foreach (($contents['ISSN'] ?? []) as $issn) {
$field_series_issn[] = ['value' => $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(); $new_node->save();
return $new_node->id(); return $new_node->id();
@ -126,20 +123,15 @@ final class NodeBuilder {
'name' => $term_name, 'name' => $term_name,
'vid' => $vocabulary, 'vid' => $vocabulary,
]); ]);
if ($terms) { if ($terms) {
// Return the first found term.
return reset($terms); return reset($terms);
} }
// If the term does not exist, create it.
$term = Term::create([ $term = Term::create([
'name' => $term_name, 'name' => $term_name,
'vid' => $vocabulary, 'vid' => $vocabulary,
]); ]);
$term->save(); $term->save();
return $term; return $term;
} }
} }

Loading…
Cancel
Save