diff --git a/config/schema/islandora_altmetric.schema.yml b/config/schema/islandora_altmetric.schema.yml index 64d154e..707f978 100644 --- a/config/schema/islandora_altmetric.schema.yml +++ b/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' diff --git a/islandora_altmetric.links.menu.yml b/islandora_altmetric.links.menu.yml new file mode 100644 index 0000000..8e7c758 --- /dev/null +++ b/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 diff --git a/islandora_altmetric.routing.yml b/islandora_altmetric.routing.yml new file mode 100644 index 0000000..ed9fbd9 --- /dev/null +++ b/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' diff --git a/src/Form/AltimetricSettingsForm.php b/src/Form/AltimetricSettingsForm.php new file mode 100644 index 0000000..54707d2 --- /dev/null +++ b/src/Form/AltimetricSettingsForm.php @@ -0,0 +1,122 @@ +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); + } + +} diff --git a/src/Plugin/Block/IslandoraAltimetricBlock.php b/src/Plugin/Block/IslandoraAltimetricBlock.php index 1cb0979..13be8f3 100644 --- a/src/Plugin/Block/IslandoraAltimetricBlock.php +++ b/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; @@ -85,6 +99,9 @@ final class IslandoraAltimetricBlock extends BlockBase implements ContainerFacto ], ], ]; + $build['#cache'] = [ + 'max-age' => 0, + ]; return $build; }