Skip to content

Commit

Permalink
Merge pull request #63 from Prashant-bd/dgir-123
Browse files Browse the repository at this point in the history
DGIR-123 : Date Ranges in Citations
  • Loading branch information
nchiasson-dgi authored Feb 20, 2024
2 parents a60ae16 + a020609 commit f5cffd0
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 15 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 10 additions & 6 deletions src/Form/SelectCslForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, '<div class="csl-bib-body">')) {
// 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;
}

Expand Down Expand Up @@ -176,8 +181,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
],
];

$form['#cache']['contexts'][] = 'url';
$form['#theme'] = 'display_citations';
return $form;
}

Expand Down Expand Up @@ -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;
Expand Down
39 changes: 36 additions & 3 deletions src/Normalizer/ExtendedDateTimeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 [];
}

Expand Down
48 changes: 42 additions & 6 deletions src/Plugin/Block/DisplayCitationsBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand All @@ -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;
}

/**
Expand All @@ -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')
);
}

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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']);
}

}

0 comments on commit f5cffd0

Please sign in to comment.