From 9c5701911be9510e71b1d45de3f7accc067c8d4a Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Tue, 9 Jan 2024 15:32:43 +0530 Subject: [PATCH 1/9] DGIR-123 : Date Ranges in Citations --- src/Normalizer/ExtendedDateTimeNormalizer.php | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Normalizer/ExtendedDateTimeNormalizer.php b/src/Normalizer/ExtendedDateTimeNormalizer.php index 3ebd768..eec4052 100644 --- a/src/Normalizer/ExtendedDateTimeNormalizer.php +++ b/src/Normalizer/ExtendedDateTimeNormalizer.php @@ -5,6 +5,7 @@ use Drupal\controlled_access_terms\Plugin\Field\FieldType\ExtendedDateTimeFormat; use EDTF\EdtfFactory; use EDTF\Model\ExtDate; +use EDTF\Model\Interval; /** * Converts EDTF fields to an array including computed values. @@ -26,15 +27,29 @@ public function normalize($object, $format = NULL, array $context = []) { if (!empty($dateValue['value'])) { $parser = EdtfFactory::newParser(); $parsed = $parser->parse($dateValue['value']); + if ($parsed->isValid()) { $edtf = $parsed->getEdtfValue(); - // XXX: Only support singular EDTF dates at this time, exit out if it's - // an interval or set. - if ($edtf instanceof ExtDate) { + + // Check if it's an interval + if ($edtf instanceof Interval) { + $start = $this->formatDateVariables(explode('-', $edtf->getStartDate()->iso8601())); + $end = $this->formatDateVariables(explode('-', $edtf->getEndDate()->iso8601())); + + $date_parts['date-parts'] = [ + 'start' => $start['date-parts'][0], + 'end' => $end['date-parts'][0], + ]; + + return $date_parts; + + } elseif ($edtf instanceof ExtDate) { + // Handle singular dates return $this->formatDateVariables(explode('-', $edtf->iso8601())); } } } + return []; } From 47e4068f040249d92a047a3549609f524a17db4f Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Tue, 9 Jan 2024 15:40:20 +0530 Subject: [PATCH 2/9] DGIR-123 : linting error fix --- src/Normalizer/ExtendedDateTimeNormalizer.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Normalizer/ExtendedDateTimeNormalizer.php b/src/Normalizer/ExtendedDateTimeNormalizer.php index eec4052..0230027 100644 --- a/src/Normalizer/ExtendedDateTimeNormalizer.php +++ b/src/Normalizer/ExtendedDateTimeNormalizer.php @@ -31,7 +31,7 @@ public function normalize($object, $format = NULL, array $context = []) { if ($parsed->isValid()) { $edtf = $parsed->getEdtfValue(); - // Check if it's an interval + // Check if it's an interval. if ($edtf instanceof Interval) { $start = $this->formatDateVariables(explode('-', $edtf->getStartDate()->iso8601())); $end = $this->formatDateVariables(explode('-', $edtf->getEndDate()->iso8601())); @@ -43,8 +43,9 @@ public function normalize($object, $format = NULL, array $context = []) { return $date_parts; - } elseif ($edtf instanceof ExtDate) { - // Handle singular dates + } + elseif ($edtf instanceof ExtDate) { + // Handle singular dates. return $this->formatDateVariables(explode('-', $edtf->iso8601())); } } From ea53329461ce5c52743e2c0068f0816a0a77ec71 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Thu, 11 Jan 2024 23:01:58 +0530 Subject: [PATCH 3/9] DGIR-123 : Better error handling and extra date formats --- src/Form/SelectCslForm.php | 9 ++++++--- src/Normalizer/ExtendedDateTimeNormalizer.php | 17 +++++++++++++++-- src/Plugin/Block/DisplayCitationsBlock.php | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 6552aeb..504b514 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -128,11 +128,14 @@ public function buildForm(array $form, FormStateInterface $form_state) { // Based on `csl` text output, we will do the error handling. // When HTML output is not as expected, add a form element which indicates // we received error. + $form['error_handling_element'] = [ + '#markup' => 0, + '#access' => FALSE, + ]; + if (!str_starts_with($csl, '
')) { // Add a custom markup element to the form. - $form['error_handling_element'] = [ - '#markup' => 'Form with error', - ]; + $form['error_handling_element']['#markup'] = 1; // Log error message. $this->logger->error($csl); diff --git a/src/Normalizer/ExtendedDateTimeNormalizer.php b/src/Normalizer/ExtendedDateTimeNormalizer.php index 0230027..7b56caf 100644 --- a/src/Normalizer/ExtendedDateTimeNormalizer.php +++ b/src/Normalizer/ExtendedDateTimeNormalizer.php @@ -33,8 +33,21 @@ public function normalize($object, $format = NULL, array $context = []) { // Check if it's an interval. if ($edtf instanceof Interval) { - $start = $this->formatDateVariables(explode('-', $edtf->getStartDate()->iso8601())); - $end = $this->formatDateVariables(explode('-', $edtf->getEndDate()->iso8601())); + // Check for open-ended date range + // Handle YYYY-MM-DD/.. format. + if (str_contains($dateValue['value'], '/..') !== false) { + $end = $this->formatDateVariables(explode('-', date('Y-m-d'))); // Current date + $start = $this->formatDateVariables(explode('-', $edtf->getStartDate()->iso8601())); + } + elseif (str_contains($dateValue['value'], '../') !== false) { + // Handle ../YYYY-MM-DD format. + $start = $this->formatDateVariables(explode('-', date('Y-m-d'))); // Current date + $end = $this->formatDateVariables(explode('-', $edtf->getEndDate()->iso8601())); + } + else { + $start = $this->formatDateVariables(explode('-', $edtf->getStartDate()->iso8601())); + $end = $this->formatDateVariables(explode('-', $edtf->getEndDate()->iso8601())); + } $date_parts['date-parts'] = [ 'start' => $start['date-parts'][0], diff --git a/src/Plugin/Block/DisplayCitationsBlock.php b/src/Plugin/Block/DisplayCitationsBlock.php index 78ebd69..8a2e2fb 100644 --- a/src/Plugin/Block/DisplayCitationsBlock.php +++ b/src/Plugin/Block/DisplayCitationsBlock.php @@ -85,7 +85,7 @@ public static function create(ContainerInterface $container, array $configuratio */ public function build() { $cite_this_form = $this->formBuilder->getForm('Drupal\islandora_citations\Form\SelectCslForm'); - if (!empty($cite_this_form['error_handling_element'])) { + if ($cite_this_form['error_handling_element']['#markup']) { // Hide the entire block. return NULL; } From 1e8a01b74fa15d71e9f49becd710a40c0751ed29 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Thu, 11 Jan 2024 23:11:30 +0530 Subject: [PATCH 4/9] DGIR-123 : Better error handling and extra date formats --- src/Normalizer/ExtendedDateTimeNormalizer.php | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Normalizer/ExtendedDateTimeNormalizer.php b/src/Normalizer/ExtendedDateTimeNormalizer.php index 7b56caf..2e9d202 100644 --- a/src/Normalizer/ExtendedDateTimeNormalizer.php +++ b/src/Normalizer/ExtendedDateTimeNormalizer.php @@ -35,18 +35,30 @@ public function normalize($object, $format = NULL, array $context = []) { if ($edtf instanceof Interval) { // Check for open-ended date range // Handle YYYY-MM-DD/.. format. - if (str_contains($dateValue['value'], '/..') !== false) { - $end = $this->formatDateVariables(explode('-', date('Y-m-d'))); // Current date - $start = $this->formatDateVariables(explode('-', $edtf->getStartDate()->iso8601())); + if (str_contains($dateValue['value'], '/..') !== FALSE) { + $end = $this->formatDateVariables( + explode('-', date('Y-m-d')) + ); + $start = $this->formatDateVariables( + explode('-', $edtf->getStartDate()->iso8601()) + ); } - elseif (str_contains($dateValue['value'], '../') !== false) { + elseif (str_contains($dateValue['value'], '../') !== FALSE) { // Handle ../YYYY-MM-DD format. - $start = $this->formatDateVariables(explode('-', date('Y-m-d'))); // Current date - $end = $this->formatDateVariables(explode('-', $edtf->getEndDate()->iso8601())); + $start = $this->formatDateVariables( + explode('-', date('Y-m-d')) + ); + $end = $this->formatDateVariables( + explode('-', $edtf->getEndDate()->iso8601()) + ); } else { - $start = $this->formatDateVariables(explode('-', $edtf->getStartDate()->iso8601())); - $end = $this->formatDateVariables(explode('-', $edtf->getEndDate()->iso8601())); + $start = $this->formatDateVariables( + explode('-', $edtf->getStartDate()->iso8601()) + ); + $end = $this->formatDateVariables( + explode('-', $edtf->getEndDate()->iso8601()) + ); } $date_parts['date-parts'] = [ From b293267da3bbeef8023916bc2181497553ec0b17 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Wed, 7 Feb 2024 15:04:09 +0530 Subject: [PATCH 5/9] DGIR-123: Update code to handle error and date format --- src/Form/SelectCslForm.php | 7 ++-- src/Plugin/Block/DisplayCitationsBlock.php | 48 +++++++++++++++++++--- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 504b514..1647c96 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -119,6 +119,9 @@ public function buildForm(array $form, FormStateInterface $form_state) { } $csl = !empty($default_csl) ? $this->getDefaultCitation($default_csl) : ''; + $form['#cache']['contexts'][] = 'url'; + $form['#theme'] = 'display_citations'; + // We receive error message as a string, and then we display same string // as output. // We expect output in a specific format when there is no error as below @@ -139,7 +142,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { // Log error message. $this->logger->error($csl); - return $form; } @@ -178,8 +180,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ]; - $form['#cache']['contexts'][] = 'url'; - $form['#theme'] = 'display_citations'; return $form; } @@ -245,6 +245,7 @@ public function getDefaultCitation($csl_name) { 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; diff --git a/src/Plugin/Block/DisplayCitationsBlock.php b/src/Plugin/Block/DisplayCitationsBlock.php index 8a2e2fb..0d2736f 100644 --- a/src/Plugin/Block/DisplayCitationsBlock.php +++ b/src/Plugin/Block/DisplayCitationsBlock.php @@ -3,10 +3,12 @@ namespace Drupal\islandora_citations\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; use Drupal\islandora_citations\IslandoraCitationsHelper; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -43,6 +45,13 @@ class DisplayCitationsBlock extends BlockBase implements ContainerFactoryPluginI */ protected $citationHelper; + /** + * The current route match service. + * + * @var \Drupal\Core\Routing\RouteMatchInterface + */ + protected $routeMatch; + /** * Construct a new DisplayCitationsBlock. * @@ -58,12 +67,15 @@ class DisplayCitationsBlock extends BlockBase implements ContainerFactoryPluginI * The form builder instance. * @param \Drupal\islandora_citations\IslandoraCitationsHelper $citationHelper * The CitationHelper instance. + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * The current route match service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, FormBuilderInterface $formBuilder, IslandoraCitationsHelper $citationHelper) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, FormBuilderInterface $formBuilder, IslandoraCitationsHelper $citationHelper, RouteMatchInterface $route_match) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entityTypeManager; $this->formBuilder = $formBuilder; $this->citationHelper = $citationHelper; + $this->routeMatch = $route_match; } /** @@ -76,7 +88,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $container->get('entity_type.manager'), $container->get('form_builder'), - $container->get('islandora_citations.helper') + $container->get('islandora_citations.helper'), + $container->get('current_route_match') ); } @@ -85,13 +98,14 @@ public static function create(ContainerInterface $container, array $configuratio */ public function build() { $cite_this_form = $this->formBuilder->getForm('Drupal\islandora_citations\Form\SelectCslForm'); - if ($cite_this_form['error_handling_element']['#markup']) { - // Hide the entire block. - return NULL; + $build['form'] = $cite_this_form; + + if ($cite_this_form['error_handling_element']['#markup'] == 1) { + // Hide entire block due to error. + return []; } if (!empty($this->citationHelper->getCitationEntityList())) { - $build['form'] = $cite_this_form; return $build; } } @@ -157,4 +171,26 @@ public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['default_csl_type'] = $values['field_csl_type']; } + /** + * {@inheritdoc} + */ + public function getCacheTags() { + // Retrieve the node ID. + $node = $this->routeMatch->getParameter('node'); + $node_id = $node ? $node->id() : NULL; + + // Return cache tags. + if ($node_id) { + return ['node:' . $node_id]; + } + return []; + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + return Cache::mergeContexts(parent::getCacheContexts(), ['route']); + } + } From 66fdfc3749e3317d37d542df8e6adc5543355e31 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Thu, 15 Feb 2024 13:34:41 +0530 Subject: [PATCH 6/9] DGIR-123 : Handle one more date format and update readme --- README.md | 9 +++++++++ src/Normalizer/ExtendedDateTimeNormalizer.php | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/README.md b/README.md index 14e5251..bccf597 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,15 @@ Map the relevant drupal fields to csl fields. Module is under active development and there are some known issues: - Normalisation of date range fields + - Latest under DGIR-123 we handled below date foemates + - 2023-12-24 + - 2023-12 + - 2023 + - 2023-10/2023-12 + - 2023-12-01/2023-12-24 + - 2005-05-05/.. [ It will take a current date as an end date ] + - ../2025 [ It will take current date as a start date ] + - /2023-10 OR 2023-10/ [ Will not map as format is not supported ] ## Maintainers/Sponsors diff --git a/src/Normalizer/ExtendedDateTimeNormalizer.php b/src/Normalizer/ExtendedDateTimeNormalizer.php index 2e9d202..006e300 100644 --- a/src/Normalizer/ExtendedDateTimeNormalizer.php +++ b/src/Normalizer/ExtendedDateTimeNormalizer.php @@ -52,6 +52,11 @@ public function normalize($object, $format = NULL, array $context = []) { explode('-', $edtf->getEndDate()->iso8601()) ); } + elseif (str_ends_with($dateValue['value'], '/') || str_starts_with($dateValue['value'], '/')) { + // Handle /YYYY-MM-DD or YYYY-MM-DD/ format. + // As the format is not supported, we will not map. + return []; + } else { $start = $this->formatDateVariables( explode('-', $edtf->getStartDate()->iso8601()) From 4ebd6392c6ca2ab78a510747c64de4e1b0559a0d Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Thu, 15 Feb 2024 22:48:02 +0530 Subject: [PATCH 7/9] DGIR-123 : Handle more date formats --- src/Normalizer/ExtendedDateTimeNormalizer.php | 21 ++++--------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/Normalizer/ExtendedDateTimeNormalizer.php b/src/Normalizer/ExtendedDateTimeNormalizer.php index 006e300..0682f4d 100644 --- a/src/Normalizer/ExtendedDateTimeNormalizer.php +++ b/src/Normalizer/ExtendedDateTimeNormalizer.php @@ -34,23 +34,10 @@ public function normalize($object, $format = NULL, array $context = []) { // Check if it's an interval. if ($edtf instanceof Interval) { // Check for open-ended date range - // Handle YYYY-MM-DD/.. format. - if (str_contains($dateValue['value'], '/..') !== FALSE) { - $end = $this->formatDateVariables( - explode('-', date('Y-m-d')) - ); - $start = $this->formatDateVariables( - explode('-', $edtf->getStartDate()->iso8601()) - ); - } - elseif (str_contains($dateValue['value'], '../') !== FALSE) { - // Handle ../YYYY-MM-DD format. - $start = $this->formatDateVariables( - explode('-', date('Y-m-d')) - ); - $end = $this->formatDateVariables( - explode('-', $edtf->getEndDate()->iso8601()) - ); + if (str_contains($dateValue['value'], '/..') !== FALSE || str_contains($dateValue['value'], '../') { + // Handle ../YYYY-MM-DD or YYYY-MM-DD/.. format. + // As the format is not supported, we will not map. + return []; } elseif (str_ends_with($dateValue['value'], '/') || str_starts_with($dateValue['value'], '/')) { // Handle /YYYY-MM-DD or YYYY-MM-DD/ format. From c2b83c6a941c80385488b127e273ce9c334d8eb2 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Thu, 15 Feb 2024 22:53:06 +0530 Subject: [PATCH 8/9] DGIR-123 : Handle more date formats --- src/Normalizer/ExtendedDateTimeNormalizer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Normalizer/ExtendedDateTimeNormalizer.php b/src/Normalizer/ExtendedDateTimeNormalizer.php index 0682f4d..3401ca2 100644 --- a/src/Normalizer/ExtendedDateTimeNormalizer.php +++ b/src/Normalizer/ExtendedDateTimeNormalizer.php @@ -33,8 +33,8 @@ public function normalize($object, $format = NULL, array $context = []) { // Check if it's an interval. if ($edtf instanceof Interval) { - // Check for open-ended date range - if (str_contains($dateValue['value'], '/..') !== FALSE || str_contains($dateValue['value'], '../') { + // Check for open-ended date range. + if (str_contains($dateValue['value'], '/..') !== FALSE || str_contains($dateValue['value'], '../')) { // Handle ../YYYY-MM-DD or YYYY-MM-DD/.. format. // As the format is not supported, we will not map. return []; From a020609afe9b4df44b00aba0b22c509d8640fe58 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Thu, 15 Feb 2024 22:56:59 +0530 Subject: [PATCH 9/9] DGIR-123 : Update readme --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index bccf597..19aaf7a 100644 --- a/README.md +++ b/README.md @@ -79,8 +79,7 @@ Module is under active development and there are some known issues: - 2023 - 2023-10/2023-12 - 2023-12-01/2023-12-24 - - 2005-05-05/.. [ It will take a current date as an end date ] - - ../2025 [ It will take current date as a start date ] + - 2005-05-05/.. OR ../2025 [ Will not map as format is not supported ] - /2023-10 OR 2023-10/ [ Will not map as format is not supported ] ## Maintainers/Sponsors