Browse Source

initial commit

main
astanley 2 years ago
commit
54cfe7eea7
  1. 7
      config/schema/islandora_altmetric.schema.yml
  2. 7
      islandora_altmetric.info.yml
  3. 4
      islandora_altmetric.libraries.yml
  4. 6
      islandora_altmetric.module
  5. 1
      js/doi_embed.js
  6. 91
      src/Plugin/Block/IslandoraAltimetricBlock.php

7
config/schema/islandora_altmetric.schema.yml

@ -0,0 +1,7 @@
block.settings.islandora_altmetric_islandora_altmetric:
type: block_settings
label: 'Islandora Altmetric block'
mapping:
example:
type: string
label: Example

7
islandora_altmetric.info.yml

@ -0,0 +1,7 @@
name: 'Islandora Altmetric'
type: module
description: 'Displays Altmetric Badge in a block'
package: Custom
core_version_requirement: ^10 || ^11
dependencies:
- islandora:islandora

4
islandora_altmetric.libraries.yml

@ -0,0 +1,4 @@
islandora_altmetric:
js:
js/doi_embed.js: {}

6
islandora_altmetric.module

@ -0,0 +1,6 @@
<?php
/**
* @file
* Primary module hooks for Islandora Altmetric module.
*/

1
js/doi_embed.js

@ -0,0 +1 @@
!function(e,t){var d="createElement",c="getElementsByTagName",n="setAttribute",a=document.getElementById(e);a&&a.parentNode&&a.parentNode.removeChild(a),(a=(a=document[d+"NS"]&&document.documentElement.namespaceURI)?document[d+"NS"](a,"script"):document[d]("script"))[n]("id",e),a[n]("src",t),(document[c]("head")[0]||document[c]("body")[0]).appendChild(a)}("altmetric-embed-js","https://d1bxh8uas1mnw7.cloudfront.net/assets/altmetric_badges-e8c6816abd50a76f6c51292a940cd903f1d7767d11fcfee92c0e65948faf4a1b.js");

91
src/Plugin/Block/IslandoraAltimetricBlock.php

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace Drupal\islandora_altmetric\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides an islandora altimetric block.
*
* @Block(
* id = "islandora_altmetric_islandora_altimetric",
* admin_label = @Translation("Islandora Altimetric"),
* category = @Translation("Custom"),
* )
*/
final class IslandoraAltimetricBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The route match service.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructs the plugin instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
EntityTypeManagerInterface $entity_typemanager,
RouteMatchInterface $route_match
) {
$this->routeMatch = $route_match;
$this->entityTypeManager = $entity_typemanager;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('current_route_match'),
);
}
/**
* {@inheritdoc}
*/
public function build(): array {
$node = $this->routeMatch->getParameter('node');
$doi = $node->field_doi->value;
$markup = <<<EOD
<div class="altmetric-embed" data-badge-popover="right" data-doi="$doi"></div>
<script async src="//badge.altmetric.com/embeds.js"></script>
EOD;
$build['content'] = [
'#markup' => $markup,
];
$build['content'] = [
'#markup' => $markup,
'#attached' => [
'library' => [
'islandora_altmetric/islandora_altmetric',
],
],
];
return $build;
}
}
Loading…
Cancel
Save