From 1e63de16beff283522ecbdedb0da060ad00c6e44 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Singh Date: Thu, 19 Oct 2023 10:22:05 -0400 Subject: [PATCH] Default Value for field_csl_type --- src/Form/SelectCslForm.php | 47 ++++++++++++++++------ src/IslandoraCitationsHelper.php | 14 +++---- src/Plugin/Block/DisplayCitationsBlock.php | 29 ++++++++++++- 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index e0e9f87..96f331b 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -20,6 +20,12 @@ class SelectCslForm extends FormBase { * @var Drupal\islandora_citations\IslandoraCitationsHelper */ protected $citationHelper; + /** + * CSL type value from block. + * + * @var string + */ + private $blockCSLType; /** * The route match. * @@ -65,6 +71,8 @@ public function getFormId() { */ public function buildForm(array $form, FormStateInterface $form_state) { $block_storage = $this->entityTypeManager->getStorage('block'); + // Check if the value is set in block, newly added field. + // Pass it in renderCitation. $blocks = $block_storage->loadMultiple(); $cslItems = $this->citationHelper->getCitationEntityList(); $default_csl = array_values($cslItems)[0]; @@ -73,6 +81,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { if (isset($settings['id'])) { if ($settings['id'] == 'islandora_citations_display_citations') { $default_csl = !empty($settings['default_csl']) ? $settings['default_csl'] : array_values($cslItems)[0]; + $this->blockCSLType = $settings['default_csl_type']; } } } @@ -87,7 +96,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#empty_option' => $this->t('- Select csl -'), '#default_value' => $default_csl, '#ajax' => [ - 'callback' => '::renderCitation', + 'callback' => '::renderAjaxCitation', 'wrapper' => 'formatted-citation', 'method' => 'html', 'event' => 'change', @@ -109,20 +118,15 @@ public function buildForm(array $form, FormStateInterface $form_state) { /** * Render CSL response on ajax call. */ - public function renderCitation(array $form, FormStateInterface $form_state) { + public function renderAjaxCitation(array $form, FormStateInterface $form_state) { $csl_name = $form_state->getValue('csl_list'); if ($csl_name == '') { return [ '#children' => '', ]; } - $entity = $this->routeMatch->getParameter('node'); - $citationItems[] = $this->citationHelper->encodeEntityForCiteproc($entity); - - $style = $this->citationHelper->loadStyle($csl_name); - - $rendered = $this->citationHelper->renderWithCiteproc($citationItems, $style); - + // Method call to render citation. + $rendered = $this->renderCitation($csl_name); $response = [ '#children' => $rendered['data'], ]; @@ -143,14 +147,12 @@ public function submitForm(array &$form, FormStateInterface $form_state) { * Block default csl name. */ public function getDefaultCitation($csl_name) { - $entity = $this->routeMatch->getParameter('node'); if (empty($csl_name)) { return $this->t('Select CSL'); } try { - $citationItems[] = $this->citationHelper->encodeEntityForCiteproc($entity); - $style = $this->citationHelper->loadStyle($csl_name); - $rendered = $this->citationHelper->renderWithCiteproc($citationItems, $style); + // Method call to render citation. + $rendered = $this->renderCitation($csl_name); return $rendered['data']; } catch (\Throwable $e) { @@ -158,4 +160,23 @@ public function getDefaultCitation($csl_name) { } } + /** + * Get rendered data. + * + * @param string $csl_name + * Block default csl name. + * + * @throws \Symfony\Component\Serializer\Exception\ExceptionInterface + */ + private function renderCitation($csl_name): ?array { + $entity = $this->routeMatch->getParameter('node'); + $citationItems[] = $this->citationHelper->encodeEntityForCiteproc($entity); + $blockCSLType = $this->blockCSLType; + if (!isset($citationItems[0]->type)) { + $citationItems[0]->type = $blockCSLType; + } + $style = $this->citationHelper->loadStyle($csl_name); + return $this->citationHelper->renderWithCiteproc($citationItems, $style); + } + } diff --git a/src/IslandoraCitationsHelper.php b/src/IslandoraCitationsHelper.php index 34183ba..61fa6fe 100644 --- a/src/IslandoraCitationsHelper.php +++ b/src/IslandoraCitationsHelper.php @@ -80,7 +80,7 @@ public function loadCslJsonSchema() { /** * Cet citations styles from config entity. */ - public function getCitationEntityList() { + public function getCitationEntityList(): array { $citationIds = $this->citationsStorage->getQuery()->execute(); $citationEntities = $this->citationsStorage->loadMultiple($citationIds); $citationList = []; @@ -94,7 +94,7 @@ public function getCitationEntityList() { /** * Load styles from citations style language. */ - public function getAllCslsFromCiteproc() { + public function getAllCslsFromCiteproc(): array { $drupalFinder = new DrupalFinder(); $drupalFinder->locateRoot(DRUPAL_ROOT); $vendorDir = $drupalFinder->getVendorDir(); @@ -110,6 +110,8 @@ public function getAllCslsFromCiteproc() { /** * Load style string from entity or file. + * + * @throws \Seboettg\CiteProc\Exception\CiteProcException */ public function loadStyle($styleName, $styleType = 'entity') { if ($styleType == 'entity') { @@ -125,8 +127,6 @@ public function loadStyle($styleName, $styleType = 'entity') { /** * Render entity with citeproc. - * - * @throws \Symfony\Component\Serializer\Exception\ExceptionInterface */ public function renderWithCiteproc(array $data, string $style, string $mode = 'bibliography') { try { @@ -146,12 +146,8 @@ public function renderWithCiteproc(array $data, string $style, string $mode = 'b * @throws \Symfony\Component\Serializer\Exception\ExceptionInterface * @throws \Exception */ - public function encodeEntityForCiteproc(EntityInterface $entity) { + public function encodeEntityForCiteproc(EntityInterface $entity): object { $cslEncodedData = $this->serializer->normalize($entity, 'csl-json'); - if (!isset($cslEncodedData['type'])) { - $this->logger->error('CSL encoding error. Type is a mandatory field.'); - throw new \Exception('CSL encoding error. Type is a mandatory field.'); - } return (object) $cslEncodedData; } diff --git a/src/Plugin/Block/DisplayCitationsBlock.php b/src/Plugin/Block/DisplayCitationsBlock.php index 7fa8a52..b2d7df4 100644 --- a/src/Plugin/Block/DisplayCitationsBlock.php +++ b/src/Plugin/Block/DisplayCitationsBlock.php @@ -3,6 +3,7 @@ namespace Drupal\islandora_citations\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -21,6 +22,13 @@ */ class DisplayCitationsBlock extends BlockBase implements ContainerFactoryPluginInterface { + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + /** * The form builder. * @@ -43,13 +51,16 @@ class DisplayCitationsBlock extends BlockBase implements ContainerFactoryPluginI * The plugin ID for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * The entity type manager service. * @param \Drupal\Core\Form\FormBuilderInterface $formBuilder * The form builder instance. * @param \Drupal\islandora_citations\IslandoraCitationsHelper $citationHelper * The CitationHelper instance. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $formBuilder, IslandoraCitationsHelper $citationHelper) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, FormBuilderInterface $formBuilder, IslandoraCitationsHelper $citationHelper) { parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->entityTypeManager = $entityTypeManager; $this->formBuilder = $formBuilder; $this->citationHelper = $citationHelper; } @@ -62,6 +73,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, + $container->get('entity_type.manager'), $container->get('form_builder'), $container->get('islandora_citations.helper') ); @@ -83,6 +95,7 @@ public function build() { public function defaultConfiguration() { return [ 'default_csl' => '', + 'default_csl_type' => 'Webpage', ]; } @@ -102,6 +115,7 @@ public function blockForm($form, FormStateInterface $form_state) { else { $config = $this->getConfiguration(); $defaultCSL = $config['default_csl']; + $defaultCSLType = $config['default_csl_type']; $form['csl_list'] = [ '#type' => 'select', '#title' => $this->t('Select default CSL'), @@ -110,6 +124,18 @@ public function blockForm($form, FormStateInterface $form_state) { '#attributes' => ['aria-label' => $this->t('Select CSL')], '#default_value' => $defaultCSL, ]; + $cslTypesVocab = $this->entityTypeManager->getStorage('taxonomy_term')->loadTree('csl_type'); + foreach ($cslTypesVocab as $vocab) { + $cslTypesNames[$vocab->name] = $vocab->name; + } + $form['field_csl_type'] = [ + '#type' => 'select', + '#title' => $this->t('Object Type (Citation)'), + '#options' => $cslTypesNames, + '#empty_option' => $this->t('- Select Citation -'), + '#attributes' => ['aria-label' => $this->t('Select Citation')], + '#default_value' => $defaultCSLType, + ]; } return $form; @@ -121,6 +147,7 @@ public function blockForm($form, FormStateInterface $form_state) { public function blockSubmit($form, FormStateInterface $form_state) { $values = $form_state->getValues(); $this->configuration['default_csl'] = $values['csl_list']; + $this->configuration['default_csl_type'] = $values['field_csl_type']; } }