Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DGIR-139 : Current Date in Citations #62

Merged
merged 10 commits into from
Jan 10, 2024
16 changes: 16 additions & 0 deletions src/Form/SelectCslForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\islandora_citations\Form;

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
Expand Down Expand Up @@ -31,6 +32,13 @@ class SelectCslForm extends FormBase {
*/
private $blockCSLType;

/**
* CSL type value from block.
*
* @var string
*/
private $blockCSLAccessedDateFormat;

/**
* The route match.
*
Expand Down Expand Up @@ -110,6 +118,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
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'];
$this->blockCSLAccessedDateFormat = $settings['csl_accessed_date_format'] ?? '';
}
}
}
Expand Down Expand Up @@ -253,6 +262,13 @@ private function renderCitation($csl_name): ?array {
$citationItems[0]->URL = Url::fromUserInput($node_url)->setAbsolute()->toString();
}

// If Accessed is configured, add current date.
if (!empty($this->blockCSLAccessedDateFormat)) {
Prashant-bd marked this conversation as resolved.
Show resolved Hide resolved
$current_date = new DrupalDateTime('now');
$citationItems[0]->URL = $citationItems[0]->URL . ' ' .
chrismacdonaldw marked this conversation as resolved.
Show resolved Hide resolved
$current_date->format($this->blockCSLAccessedDateFormat);
}

$style = $this->citationHelper->loadStyle($csl_name);
return $this->citationHelper->renderWithCiteproc($citationItems, $style);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Plugin/Block/DisplayCitationsBlock.php
chrismacdonaldw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public function blockForm($form, FormStateInterface $form_state) {
$config = $this->getConfiguration();
$defaultCSL = $config['default_csl'];
$defaultCSLType = $config['default_csl_type'];
$accessedDateFormat = $config['csl_accessed_date_format'];
$form['csl_list'] = [
'#type' => 'select',
'#title' => $this->t('Select default CSL'),
Expand All @@ -143,6 +144,14 @@ public function blockForm($form, FormStateInterface $form_state) {
'#attributes' => ['aria-label' => $this->t('Select Citation')],
'#default_value' => $defaultCSLType,
];

$form['field_accessed'] = [
'#type' => 'textfield',
'#title' => $this->t('Accessed'),
'#description' => $this->t('This will show current date if configured. Date format ex. F d, Y.'),
'#attributes' => ['aria-label' => $this->t('Accessed')],
'#default_value' => $accessedDateFormat,
];
}

return $form;
Expand All @@ -155,6 +164,7 @@ 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'];
$this->configuration['csl_accessed_date_format'] = $values['field_accessed'];
}

}