Displays an Altmetric badge on repository object pages using a DOI stored in a node field. Drupal 11 ready
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

124 lines
4.0 KiB

<?php
declare(strict_types=1);
namespace Drupal\islandora_altmetric\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
/**
* Configure the DOI source field and Altmetric badge style.
*
* Settings are shared by all instances of the Altmetric block.
* This is the form referenced by the module settings route.
*/
final class AltmetricSettingForm extends ConfigFormBase {
/**
* The entity field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* Constructs an AltmetricSettingForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
* The typed configuration manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
*/
public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($config_factory, $typed_config_manager);
$this->entityFieldManager = $entity_field_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('config.typed'),
$container->get('entity_field.manager'),
);
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'islandora_altmetric_altmetric_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return ['islandora_altmetric.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
// Inspect fields on the Islandora object content type.
$fields = $this->entityFieldManager->getFieldDefinitions('node', 'islandora_object');
$candidate_fields = [];
// Offer string fields whose machine names begin with "field".
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();
}
}
}
// Store the field machine name, rather than its display label.
$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') ?? 'field_doi',
'#required' => TRUE,
];
// Map Altmetric badge type values to human-readable labels.
$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') ?? '4',
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
// Persist site-wide settings for use by the block plugin.
$this->config('islandora_altmetric.settings')
->set('doi_field', $form_state->getValue('doi_field'))
->set('style', (string) $form_state->getValue('style'))
->save();
parent::submitForm($form, $form_state);
}
}