diff --git a/web/modules/custom/asu_item_extras/src/Plugin/Block/UnpaywallBlock.php b/web/modules/custom/asu_item_extras/src/Plugin/Block/UnpaywallBlock.php new file mode 100644 index 000000000..648b36f5c --- /dev/null +++ b/web/modules/custom/asu_item_extras/src/Plugin/Block/UnpaywallBlock.php @@ -0,0 +1,183 @@ +renderer = $renderer; + $this->httpClient = $httpClient; + $this->entityTypeManager = $entityTypeManager; + } + + /** + * {@inheritdoc} + */ + public static function create( + ContainerInterface $container, + array $configuration, + $plugin_id, + $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('renderer'), + $container->get('http_client'), + $container->get('entity_type.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function build() { + $return_val = ''; + $block_config = BlockBase::getConfiguration(); + if (is_array($block_config) && array_key_exists('node', $block_config)) { + $node = $block_config['node']; + + $doi_val = ""; + $typed_idents = $node->field_typed_identifier; + foreach ($typed_idents as $typed_ident) { + if (!$doi_val) { + $typed_target_id = $typed_ident->get("target_id")->getCastedValue(); + $paragraph = $this->entityTypeManager->getStorage('paragraph')->load($typed_target_id); + $typed_ident_target_id = $paragraph->field_identifier_type->target_id; + if ($typed_ident_target_id) { + $typed_ident_type = $this->entityTypeManager->getStorage('taxonomy_term')->load($typed_ident_target_id)->get('field_identifier_predicate')->value; + if ($typed_ident_type == 'doi') { + $doi_val = $paragraph->get('field_identifier_value')->value; + } + } + } + } + if ($doi_val) { + $unpaywall_url = $this->callUnpayApi($doi_val); + if ($unpaywall_url) { + $return_val = render(Link::fromTextAndUrl($this->t('Open access version '), Url::fromUri($unpaywall_url))->toRenderable()); + } + } + + } + return [ + '#markup' => $return_val + ]; + } + + /** + * Gets OA article link if one exists. + * + * @param string $doi + * The doi. + * + * @return string + * The url. + */ + function callUnpayApi($doi) { + $query = "https://api.unpaywall.org/v2/" . $doi . "?email=digitalrepository@asu.edu"; + + $response = $this->httpClient->get($query); + $response_body = $response->getBody(); + $status_code = $response->getStatusCode(); + if ($status_code == 200) { + $resource = json_decode($response_body, TRUE); + return $resource['best_oa_location']['url']; + } + else { + return null; + } + } + + /** + * {@inheritdoc} + */ + public function getCacheTags() { + $block_config = BlockBase::getConfiguration(); + if (is_array($block_config) && array_key_exists('node', $block_config)) { + $nid = $block_config['node']; + } + if (isset($nid)) { + if (!is_string($nid)) { + $nid = $nid->id(); + } + return Cache::mergeTags(parent::getCacheTags(), ['node:' . $nid]); + } + else { + // Return default tags instead. + return parent::getCacheTags(); + } + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + return Cache::mergeContexts(parent::getCacheContexts(), ['route']); + } + +} diff --git a/web/themes/custom/asulib_barrio/templates/content/node--asu-repository-item--asu-audio.html.twig b/web/themes/custom/asulib_barrio/templates/content/node--asu-repository-item--asu-audio.html.twig index f1f226713..f5bfdee4d 100644 --- a/web/themes/custom/asulib_barrio/templates/content/node--asu-repository-item--asu-audio.html.twig +++ b/web/themes/custom/asulib_barrio/templates/content/node--asu-repository-item--asu-audio.html.twig @@ -132,6 +132,7 @@