commit e20e9e0586bea56d69fe6623686851947969a5c0 Author: astanley Date: Thu Nov 21 11:16:43 2024 -0400 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..970337e --- /dev/null +++ b/README.md @@ -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) diff --git a/citation_type_selector.info.yml b/citation_type_selector.info.yml new file mode 100644 index 0000000..88dc688 --- /dev/null +++ b/citation_type_selector.info.yml @@ -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 diff --git a/citation_type_selector.libraries.yml b/citation_type_selector.libraries.yml new file mode 100644 index 0000000..cb45dac --- /dev/null +++ b/citation_type_selector.libraries.yml @@ -0,0 +1,5 @@ +citation_type_selector_styles: + version: 1.x + css: + theme: + css/style.css: {} \ No newline at end of file diff --git a/citation_type_selector.links.menu.yml b/citation_type_selector.links.menu.yml new file mode 100644 index 0000000..90eb212 --- /dev/null +++ b/citation_type_selector.links.menu.yml @@ -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 diff --git a/citation_type_selector.module b/citation_type_selector.module new file mode 100644 index 0000000..5e7315f --- /dev/null +++ b/citation_type_selector.module @@ -0,0 +1,6 @@ +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' => '
', + '#suffix' => '
', + ]; + + 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.')); + } + +} diff --git a/src/Normalizer/FieldItemListNormalizer.php b/src/Normalizer/FieldItemListNormalizer.php new file mode 100644 index 0000000..4b4dfbe --- /dev/null +++ b/src/Normalizer/FieldItemListNormalizer.php @@ -0,0 +1,88 @@ +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; + } + +}