Browse Source

added config

main
astanley 2 years ago
parent
commit
4c9f9cbb73
  1. 9
      config/schema/islandora_altmetric.schema.yml
  2. 6
      islandora_altmetric.links.menu.yml
  3. 7
      islandora_altmetric.routing.yml
  4. 122
      src/Form/AltimetricSettingsForm.php
  5. 25
      src/Plugin/Block/IslandoraAltimetricBlock.php

9
config/schema/islandora_altmetric.schema.yml

@ -5,3 +5,12 @@ block.settings.islandora_altmetric_islandora_altmetric:
example:
type: string
label: Example
# Schema for the configuration files of the Islandora Altmetric module.
islandora_altmetric.settings:
type: config_object
label: 'Islandora Altmetric settings'
mapping:
example:
type: string
label: 'Example'

6
islandora_altmetric.links.menu.yml

@ -0,0 +1,6 @@
islandora_altmetric.altimetric_settings:
title: Altimetric settings
description: Setting for Altimetric Badge
parent: entity.islandora_citations.config_overview
route_name: islandora_altmetric.altimetric_settings
weight: 10

7
islandora_altmetric.routing.yml

@ -0,0 +1,7 @@
islandora_altmetric.altimetric_settings:
path: '/admin/config/system/altimetric-settings'
defaults:
_title: 'Altimetric settings'
_form: 'Drupal\islandora_altmetric\Form\AltimetricSettingsForm'
requirements:
_permission: 'administer site configuration'

122
src/Form/AltimetricSettingsForm.php

@ -0,0 +1,122 @@
<?php
declare(strict_types=1);
namespace Drupal\islandora_altmetric\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
/**
* Configure Islandora Altmetric settings for this site.
*/
final class AltimetricSettingsForm extends ConfigFormBase {
/**
* The entity field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* Constructs a AltimetricSettingsForm object.
*
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
*/
public function __construct(EntityFieldManagerInterface $entity_field_manager) {
$this->entityFieldManager = $entity_field_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_field.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'islandora_altmetric_altimetric_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return ['islandora_altmetric.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$fields = $this->entityFieldManager->getFieldDefinitions('node', 'islandora_object');
$candidate_fields = [];
foreach ($fields as $field_name => $field_definition) {
if ($field_definition->getType() === 'string') {
if (str_starts_with($field_name, 'field')) {
$candidate_fields[$field_name] = $field_definition->getLabel();
}
}
}
$form['doi_field'] = [
'#type' => 'select',
'#title' => $this->t('DOI Field'),
'#options' => $candidate_fields,
'#description' => $this->t("Field from content model with DOI"),
'#default_value' => $this->config('islandora_altmetric.settings')->get('doi_field'),
];
$styles = [
'4' => $this->t('Default'),
'donut' => $this->t('Donut'),
'medium-donut' => $this->t('Medium Donut'),
'large-donut' => $this->t('Large Donut'),
];
$form['style'] = [
'#type' => 'select',
'#title' => $this->t('Badge Style'),
'#options' => $styles,
'#description' => $this->t("Display Style"),
'#default_value' => $this->config('islandora_altmetric.settings')->get('style'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
// @todo Validate the form here.
// Example:
// @code
// if ($form_state->getValue('example') === 'wrong') {
// $form_state->setErrorByName(
// 'message',
// $this->t('The value is not correct.'),
// );
// }
// @endcode
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config('islandora_altmetric.settings')
->set('doi_field', $form_state->getValue('doi_field'))
->set('style', $form_state->getValue('style'))
->save();
parent::submitForm($form, $form_state);
}
}

25
src/Plugin/Block/IslandoraAltimetricBlock.php

@ -6,8 +6,9 @@ namespace Drupal\islandora_altmetric\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@ -34,6 +35,13 @@ final class IslandoraAltimetricBlock extends BlockBase implements ContainerFacto
*/
protected $routeMatch;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs the plugin instance.
*/
@ -42,10 +50,12 @@ final class IslandoraAltimetricBlock extends BlockBase implements ContainerFacto
$plugin_id,
$plugin_definition,
EntityTypeManagerInterface $entity_typemanager,
RouteMatchInterface $route_match
RouteMatchInterface $route_match,
ConfigFactoryInterface $configFactory
) {
$this->routeMatch = $route_match;
$this->entityTypeManager = $entity_typemanager;
$this->configFactory = $configFactory;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
@ -59,6 +69,7 @@ final class IslandoraAltimetricBlock extends BlockBase implements ContainerFacto
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('current_route_match'),
$container->get('config.factory'),
);
}
@ -66,10 +77,13 @@ final class IslandoraAltimetricBlock extends BlockBase implements ContainerFacto
* {@inheritdoc}
*/
public function build(): array {
$config = $this->configFactory->get('islandora_altmetric.settings');
$node = $this->routeMatch->getParameter('node');
$doi = $node->field_doi->value;
$doi_field = $config->get('doi_field') ?? 'field_doi';
$doi = $node->$doi_field->value;
$style = $config->get('style');
$markup = <<<EOD
<div class="altmetric-embed" data-badge-popover="right" data-doi="$doi"></div>
<div class="altmetric-embed" data-badge-type='$style' data-badge-popover="right" data-doi="$doi"></div>
<script async src="//badge.altmetric.com/embeds.js"></script>
EOD;
@ -85,6 +99,9 @@ final class IslandoraAltimetricBlock extends BlockBase implements ContainerFacto
],
],
];
$build['#cache'] = [
'max-age' => 0,
];
return $build;
}

Loading…
Cancel
Save