doiApi->getWork($doi); if (!$contents) { return []; } $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 = []; $vid = 'coar_resource_types'; foreach ($contents['author'] as $author) { $author_term = "{$author['family']}"; if (isset($author['given'])) { $author_term = "{$author_term}, {$author['given']}"; } $term = $this->getOrCreateTerm($author_term, $vid); $typed_relations[] = [ 'target_id' => $term->id(), 'rel_type' => 'relators:aut', ]; } $genre = $term_mappings[$contents['type']] ?? NULL; if ($genre) { $vocabulary = $this->get_allowed_vocabularies('node', $config->get('content_type'), $field_settings['genre']); $vid = reset($vocabulary); $terms = $this->entityTypeManager->getStorage('taxonomy_term') ->loadByProperties([ 'name' => $genre, 'vid' => $vid, ]); if ($terms) { $genre = reset($terms); } } // Build new node. $new_node = Node::create([ $field_settings['title'] => htmlspecialchars_decode($contents['title'][0]), 'field_member_of' => $collection, 'type' => $config->get('content_type'), $field_settings['contributors'] => $typed_relations, $field_settings['publisher'] => $contents['publisher'] ?? '', $field_settings['doi'] => $doi, $field_settings['issue'] => $contents['issue'] ?? '', $field_settings['volume'] => $contents['volume'] ?? '', $field_settings['date_issued'] => $contents['created']['date-parts'][0][0] ?? '', 'status' => 0, ]); // Optional fields. if ($genre) { $new_node->set($field_settings['genre'], $genre->id()); } if (isset($contents['abstract'])) { $new_node->set($field_settings['abstract'], [ 'value' => $contents['abstract'], 'format' => 'basic_html', ]); } if (isset($contents['container-title'])) { $new_node->set($field_settings['host_title'], htmlspecialchars_decode($contents['container-title'][0])); } if (isset($contents['published-online'])) { $field_date_online = []; foreach ($contents['published-online']['date-parts'] as $date_parts) { foreach ($date_parts as &$date_part) { $date_part = str_pad((string) $date_part, 2, "0", STR_PAD_LEFT); } $field_date_online[] = ['value' => implode('-', $date_parts)]; } $new_node->set($field_settings['date_online'], $field_date_online); } if (isset($contents['page'])) { $new_node->set($field_settings['page_range'], $contents['page']); } // Multivalued fields. $field_series_issn = []; foreach (($contents['ISSN'] ?? []) as $issn) { $field_series_issn[] = ['value' => $issn]; } $new_node->set($field_settings['series_issn'], $field_series_issn); $new_node->save(); return $new_node->id(); } /** * Check if a term exists in a vocabulary. If not, create it. * * @param string $term_name * The name of the term. * @param string $vocabulary * The machine name of the vocabulary. * * @return \Drupal\taxonomy\Entity\Term|null * The term entity if found or created, or NULL on failure. */ public function getOrCreateTerm($term_name, $vocabulary) { $terms = $this->entityTypeManager->getStorage('taxonomy_term') ->loadByProperties([ 'name' => $term_name, 'vid' => $vocabulary, ]); if ($terms) { return reset($terms); } $term = Term::create([ 'name' => $term_name, 'vid' => $vocabulary, ]); $term->save(); return $term; } /** * Get allowed vocabularies for a taxonomy term reference field. * * @param string $entity_type * The entity type (e.g., 'node'). * @param string $bundle * The bundle (content type machine name). * @param string $field_name * The machine name of the taxonomy term reference field. * * @return string[] * Array of allowed vocabulary IDs. */ function get_allowed_vocabularies($entity_type, $bundle, $field_name) { $config = $this->config->get('doi_prefill.settings'); $field_config = $this->entityTypeManager ->getStorage('field_config') ->load($entity_type . '.' . $bundle . '.' . $field_name); if ($field_config) { $settings = $field_config->getSettings(); if (isset($settings['handler_settings']['target_bundles'])) { return array_keys($settings['handler_settings']['target_bundles']); } } return []; } }