commit
e20e9e0586
12 changed files with 419 additions and 0 deletions
@ -0,0 +1,34 @@
|
||||
# Citation Type Selector |
||||
|
||||
Allows users to specify citation type based on genre of Islandora object. |
||||
|
||||
## Introduction |
||||
|
||||
This module extends the Discovery Garden Islandora Citation module allowing for Citation types as well as styles. |
||||
|
||||
### Citation Type Field |
||||
This populates the requires 'field_csl_type' with a value mapped and pulled from a Genre field. |
||||
|
||||
## Installation |
||||
|
||||
Install as |
||||
[usual](https://www.drupal.org/docs/extending-drupal/installing-modules). |
||||
|
||||
## Configuration |
||||
|
||||
THe mapping form found at /admin/config/system/citation-select-settings allows admins to select a Content Classification Field, |
||||
the vocabulary that field draws from, the Citation Type Field, and the Vocabulary that field draws from. |
||||
These would normally be the Genre field using the Genre vocabulary, and the Object Type (Citation) field and the CSL Type vocabulary |
||||
|
||||
## Usage |
||||
|
||||
After installing and enabling module, configure at /admin/config/system/citation-select-settings. |
||||
|
||||
|
||||
## Maintainers |
||||
Current maintainers: |
||||
|
||||
* [Robertson Library](https://library.upei.ca/) |
||||
|
||||
## License |
||||
[GPLv3](http://www.gnu.org/licenses/gpl-3.0.txt) |
||||
@ -0,0 +1,8 @@
|
||||
name: 'Citation Type Selector' |
||||
type: module |
||||
description: 'Creates default citation type by genre' |
||||
package: Custom |
||||
core_version_requirement: ^10 || ^11 |
||||
dependencies: |
||||
- islandora:islandora |
||||
- discovery_garden:islandora_citations |
||||
@ -0,0 +1,5 @@
|
||||
citation_type_selector_styles: |
||||
version: 1.x |
||||
css: |
||||
theme: |
||||
css/style.css: {} |
||||
@ -0,0 +1,6 @@
|
||||
citation_type_selector.citation_select_settings: |
||||
title: Citation select settings |
||||
description: select default citation style |
||||
parent: system.admin_config_system |
||||
route_name: citation_type_selector.citation_select_settings |
||||
weight: 10 |
||||
@ -0,0 +1,6 @@
|
||||
<?php |
||||
|
||||
/** |
||||
* @file |
||||
* Primary module hooks for Citation Type Selector module. |
||||
*/ |
||||
@ -0,0 +1,7 @@
|
||||
citation_type_selector.citation_select_settings: |
||||
path: '/admin/config/system/citation-select-settings' |
||||
defaults: |
||||
_title: 'Citation select settings' |
||||
_form: 'Drupal\citation_type_selector\Form\CitationSelectSettingsForm' |
||||
requirements: |
||||
_permission: 'administer site configuration' |
||||
@ -0,0 +1,7 @@
|
||||
services: |
||||
serializer.citation_style_selector.csljson.field_item_list: |
||||
class: Drupal\citation_type_selector\Normalizer\FieldItemListNormalizer |
||||
arguments: ['@config.factory'] |
||||
tags: |
||||
- { name: normalizer, priority: 100 } |
||||
|
||||
@ -0,0 +1,5 @@
|
||||
genre_field: 'field_genre' |
||||
genre_vocabulary: 'genre' |
||||
csl_field: 'field_csl_type' |
||||
csl_vocab: 'csl_type' |
||||
term_settings: {} |
||||
@ -0,0 +1,31 @@
|
||||
# Schema for the configuration files of the Citation type selector module. |
||||
citation_type_selector.settings: |
||||
type: config_object |
||||
label: 'Citation Type Selector Settings' |
||||
mapping: |
||||
genre_field: |
||||
type: string |
||||
label: 'Content Classification Field' |
||||
description: 'The machine name of the field used for content classification.' |
||||
genre_vocabulary: |
||||
type: string |
||||
label: 'Classification Vocabulary' |
||||
description: 'The machine name of the taxonomy vocabulary used for content classification.' |
||||
csl_field: |
||||
type: string |
||||
label: 'Citation Type Field' |
||||
description: 'The machine name of the field used for citation type.' |
||||
csl_vocab: |
||||
type: string |
||||
label: 'Citation Type Vocabulary' |
||||
description: 'The machine name of the taxonomy vocabulary used for citation types.' |
||||
term_settings: |
||||
type: mapping |
||||
label: 'Term-to-CSL Mapping' |
||||
description: 'Mapping of taxonomy terms to their corresponding CSL styles.' |
||||
mapping: |
||||
'*': |
||||
type: string |
||||
label: 'CSL Style' |
||||
description: 'The CSL style associated with the term.' |
||||
|
||||
@ -0,0 +1,4 @@
|
||||
.side-by-side-container { |
||||
display: flex; |
||||
gap: 20px; |
||||
} |
||||
@ -0,0 +1,218 @@
|
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Drupal\citation_type_selector\Form; |
||||
|
||||
use Drupal\Core\Form\ConfigFormBase; |
||||
use Drupal\Core\Form\FormStateInterface; |
||||
use Drupal\Core\Entity\EntityTypeManagerInterface; |
||||
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||
|
||||
/** |
||||
* Configure Citation type selector settings for this site. |
||||
*/ |
||||
final class CitationSelectSettingsForm extends ConfigFormBase { |
||||
|
||||
/** |
||||
* The entity type manager service. |
||||
* |
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||||
*/ |
||||
protected $entityTypeManager; |
||||
|
||||
/** |
||||
* Constructs a new CitationSelectSettingsForm. |
||||
* |
||||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
||||
* The entity type manager service. |
||||
*/ |
||||
public function __construct(EntityTypeManagerInterface $entityTypeManager) { |
||||
$this->entityTypeManager = $entityTypeManager; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public static function create(ContainerInterface $container) { |
||||
return new static( |
||||
$container->get('entity_type.manager') |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getFormId(): string { |
||||
return 'citation_type_selector_settings'; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function getEditableConfigNames(): array { |
||||
return ['citation_type_selector.settings']; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function buildForm(array $form, FormStateInterface $form_state) { |
||||
// Load the stored configuration. |
||||
$config = $this->config('citation_type_selector.settings'); |
||||
|
||||
// Get all vocabularies. |
||||
$vocabularies = $this->entityTypeManager->getStorage('taxonomy_vocabulary')->loadMultiple(); |
||||
$vocabulary_options = []; |
||||
foreach ($vocabularies as $vocabulary) { |
||||
$vocabulary_options[$vocabulary->id()] = $vocabulary->label(); |
||||
} |
||||
|
||||
// Get CSL types. |
||||
$selected_csl_vocab = $form_state->getValue('csl_vocab', $config->get('csl_vocab')); |
||||
$csl_terms = $terms = $this->entityTypeManager |
||||
->getStorage('taxonomy_term') |
||||
->loadTree($selected_csl_vocab); |
||||
$csl_options = []; |
||||
foreach ($csl_terms as $cls_term) { |
||||
$csl_options[$cls_term->tid] = $cls_term->name; |
||||
} |
||||
// Get fields from bundle. |
||||
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'islandora_object'); |
||||
$entity_reference_fields = []; |
||||
foreach ($fields as $field_name => $field_definition) { |
||||
if ($field_definition->getType() === 'entity_reference') { |
||||
if (str_starts_with($field_name, 'field')) { |
||||
$entity_reference_fields[$field_name] = $field_definition->getLabel(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
$form['#attached']['library'][] = 'citation_type_selector/citation_type_selector_styles'; |
||||
$form['side_by_side'] = [ |
||||
'#type' => 'container', |
||||
'#attributes' => [ |
||||
'class' => ['side-by-side-container'], |
||||
], |
||||
]; |
||||
|
||||
// Islandora Object Field. |
||||
$form['side_by_side']['genre_field'] = [ |
||||
'#type' => 'select', |
||||
'#description' => $this->t("The content model's genre field."), |
||||
'#title' => $this->t('Content Classification Field'), |
||||
'#options' => $entity_reference_fields, |
||||
'#default_value' => $config->get('genre_field') ?? '', |
||||
]; |
||||
// Vocabulary selection field. |
||||
$form['side_by_side']['genre_vocabulary'] = [ |
||||
'#type' => 'select', |
||||
'#title' => $this->t('Classification Vocabulary'), |
||||
'#description' => $this->t("The object classification vocabulary."), |
||||
'#options' => $vocabulary_options, |
||||
'#default_value' => $config->get('genre_vocabulary') ?? '', |
||||
'#ajax' => [ |
||||
'callback' => '::updateTermsTable', |
||||
'wrapper' => 'terms-table-wrapper', |
||||
], |
||||
]; |
||||
|
||||
$form['side_by_side']['csl_field'] = [ |
||||
'#type' => 'select', |
||||
'#title' => $this->t('Citation Type Field'), |
||||
'#description' => $this->t("The content model's citation style field."), |
||||
'#options' => $entity_reference_fields, |
||||
'#default_value' => $config->get('csl_field') ?? '', |
||||
]; |
||||
// Field selection field. |
||||
$form['side_by_side']['csl_vocab'] = [ |
||||
'#type' => 'select', |
||||
'#title' => $this->t('Citation Type Vocabulary'), |
||||
'#description' => $this->t("The object CSL Style vocabulary."), |
||||
'#options' => $vocabulary_options, |
||||
'#default_value' => $config->get('csl_vocab') ?? '', |
||||
'#ajax' => [ |
||||
'callback' => '::updateTermsTable', |
||||
'wrapper' => 'terms-table-wrapper', |
||||
], |
||||
]; |
||||
|
||||
// Get the selected vocabulary. |
||||
$selected_vocabulary = $form_state->getValue('genre_vocabulary', $config->get('genre_vocabulary')); |
||||
|
||||
// Display terms in a table format. |
||||
$form['terms_table'] = [ |
||||
'#type' => 'table', |
||||
'#header' => [ |
||||
$this->t('Type'), |
||||
$this->t('CSL Style'), |
||||
], |
||||
'#prefix' => '<div id="terms-table-wrapper">', |
||||
'#suffix' => '</div>', |
||||
]; |
||||
|
||||
if ($selected_vocabulary) { |
||||
// Load terms for the selected vocabulary. |
||||
$terms = $this->entityTypeManager |
||||
->getStorage('taxonomy_term') |
||||
->loadTree($selected_vocabulary); |
||||
|
||||
foreach ($terms as $term) { |
||||
$form['terms_table'][$term->tid]['term_name'] = [ |
||||
'#plain_text' => $term->name, |
||||
]; |
||||
|
||||
// Add a dropdown for each term. |
||||
$form['terms_table'][$term->tid]['dropdown'] = [ |
||||
'#type' => 'select', |
||||
'#options' => $csl_options, |
||||
'#default_value' => $config->get('term_settings')[$term->tid] ?? '', |
||||
]; |
||||
} |
||||
} |
||||
|
||||
// Add a submit button. |
||||
$form['submit'] = [ |
||||
'#type' => 'submit', |
||||
'#value' => $this->t('Save'), |
||||
]; |
||||
|
||||
return parent::buildForm($form, $form_state); |
||||
} |
||||
|
||||
/** |
||||
* AJAX callback to update the terms table. |
||||
*/ |
||||
public function updateTermsTable(array &$form, FormStateInterface $form_state) { |
||||
return $form['terms_table']; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function submitForm(array &$form, FormStateInterface $form_state) { |
||||
// Save the selected vocabulary and term dropdown settings. |
||||
$selected_vocabulary = $form_state->getValue('genre_vocabulary'); |
||||
$selected_field = $form_state->getValue('csl_vocab'); |
||||
$genre_field = $form_state->getValue('genre_field'); |
||||
$csl_field = $form_state->getValue('csl_field'); |
||||
$term_settings = []; |
||||
|
||||
if (!empty($form_state->getValue('terms_table'))) { |
||||
foreach ($form_state->getValue('terms_table') as $tid => $term_data) { |
||||
$term_settings[$tid] = $term_data['dropdown']; |
||||
} |
||||
} |
||||
|
||||
$this->config('citation_type_selector.settings') |
||||
->set('genre_vocabulary', $selected_vocabulary) |
||||
->set('term_settings', $term_settings) |
||||
->set('csl_vocab', $selected_field) |
||||
->set('genre_field', $genre_field) |
||||
->set('csl_field', $csl_field) |
||||
->save(); |
||||
|
||||
$this->messenger()->addMessage($this->t('The settings have been saved.')); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,88 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\citation_type_selector\Normalizer; |
||||
|
||||
use Drupal\Core\Config\ConfigFactoryInterface; |
||||
use Drupal\Core\Field\FieldItemListInterface; |
||||
use Drupal\islandora_citations\Normalizer\NormalizerBase; |
||||
|
||||
/** |
||||
* Normalizes field item list into an array structure. |
||||
*/ |
||||
class FieldItemListNormalizer extends NormalizerBase { |
||||
|
||||
|
||||
/** |
||||
* The configuration factory. |
||||
* |
||||
* @var \Drupal\Core\Config\ConfigFactoryInterface |
||||
*/ |
||||
protected $configFactory; |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected $supportedInterfaceOrClass = FieldItemListInterface::class; |
||||
|
||||
/** |
||||
* Constructs a CustomNormalizer object. |
||||
* |
||||
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
||||
* The path alias manager service. |
||||
*/ |
||||
public function __construct(ConfigFactoryInterface $config_factory) { |
||||
$this->configFactory = $config_factory; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function normalize($field_item_list, $format = NULL, array $context = []) { |
||||
$field_item_values = []; |
||||
$config = $this->configFactory->getEditable('citation_type_selector.settings'); |
||||
$csl_field = $config->get('csl_field'); |
||||
if ($field_item_list->getName() == $csl_field) { |
||||
$node = $field_item_list->getEntity(); |
||||
$genre_field = $config->get('genre_field'); |
||||
if (!$node->get($genre_field)->isEmpty()) { |
||||
$genre_id = $node->get($genre_field)->first()->getValue()['target_id']; |
||||
} |
||||
$genre_id = $genre_id ?? NULL; |
||||
$term_settings = $config->get('term_settings'); |
||||
if (!$node->get($csl_field)->isEmpty()) { |
||||
$csl_id = $node->get($csl_field)->first()->getValue()['target_id']; |
||||
} |
||||
$csl_id = $csl_id ?? NULL; |
||||
$csl_val = $term_settings[$genre_id]; |
||||
if ($csl_val != $csl_id) { |
||||
$node->set($csl_field, $csl_val); |
||||
$node->save(); |
||||
} |
||||
} |
||||
$field_item_values = []; |
||||
|
||||
/** @var \Drupal\Core\Field\FieldItemListInterface $field_item_list */ |
||||
foreach ($field_item_list as $field_item) { |
||||
// If value is empty, do not process. |
||||
if ($field_item->isEmpty()) { |
||||
continue; |
||||
} |
||||
|
||||
$context['normalized-field-list'] = $field_item_values; |
||||
|
||||
// If there are multiple csl fields mapped, get array values for each one. |
||||
if ($context['csl-map']) { |
||||
foreach ($context['csl-map'] as $cslField) { |
||||
/** @var \Drupal\Core\Field\FieldItemInterface $field_item */ |
||||
$field_item_values[$cslField][] = $this->serializer->normalize($field_item, $format, $context); |
||||
} |
||||
} |
||||
else { |
||||
$field_item_values = $this->serializer->normalize($field_item, $format, $context); |
||||
} |
||||
} |
||||
|
||||
return $field_item_values; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue