diff --git a/README.md b/README.md index 14e5251..19aaf7a 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,14 @@ 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/.. 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 diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 7c45ce0..658c3a6 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -120,6 +120,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 @@ -129,15 +132,17 @@ 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(json_encode($csl)); - return $form; } @@ -176,8 +181,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ]; - $form['#cache']['contexts'][] = 'url'; - $form['#theme'] = 'display_citations'; return $form; } @@ -243,6 +246,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/Normalizer/ExtendedDateTimeNormalizer.php b/src/Normalizer/ExtendedDateTimeNormalizer.php index 3ebd768..3401ca2 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,47 @@ 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) { + // 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 []; + } + 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()) + ); + $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 []; } diff --git a/src/Plugin/Block/DisplayCitationsBlock.php b/src/Plugin/Block/DisplayCitationsBlock.php index 78ebd69..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 (!empty($cite_this_form['error_handling_element'])) { - // 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']); + } + }