From 3922cb4410ff9936fe7e8a046df6b542b642354b Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Tue, 2 Jan 2024 16:43:19 +0530 Subject: [PATCH 1/9] DGIR-139 : Current Date in Citations --- src/Form/SelectCslForm.php | 16 ++++++++++++++++ src/Plugin/Block/DisplayCitationsBlock.php | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 6552aeb..97e414c 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -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; @@ -31,6 +32,13 @@ class SelectCslForm extends FormBase { */ private $blockCSLType; + /** + * CSL type value from block. + * + * @var string + */ + private $blockCSLAccessedDateFormate; + /** * The route match. * @@ -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->blockCSLAccessedDateFormate = $settings['csl_accessed_date_format'] ?? ''; } } } @@ -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->blockCSLAccessedDateFormate)) { + $current_date = new DrupalDateTime('now'); + $citationItems[0]->URL = $citationItems[0]->URL . ' ' . + $current_date->format($this->blockCSLAccessedDateFormate); + } + $style = $this->citationHelper->loadStyle($csl_name); return $this->citationHelper->renderWithCiteproc($citationItems, $style); } diff --git a/src/Plugin/Block/DisplayCitationsBlock.php b/src/Plugin/Block/DisplayCitationsBlock.php index 78ebd69..937c90a 100644 --- a/src/Plugin/Block/DisplayCitationsBlock.php +++ b/src/Plugin/Block/DisplayCitationsBlock.php @@ -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'), @@ -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; @@ -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']; } } From 93613dffc559ac7b96e30166ef069d45e442a022 Mon Sep 17 00:00:00 2001 From: Prashant-bd <149510496+Prashant-bd@users.noreply.github.com> Date: Thu, 4 Jan 2024 00:18:28 +0530 Subject: [PATCH 2/9] Update src/Form/SelectCslForm.php Co-authored-by: Chris MacDonald <31731869+chrismacdonaldw@users.noreply.github.com> --- src/Form/SelectCslForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 97e414c..477fdad 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -37,7 +37,7 @@ class SelectCslForm extends FormBase { * * @var string */ - private $blockCSLAccessedDateFormate; + private $blockCSLAccessedDateFormat; /** * The route match. From 5b4a9dfb6a2186b862c8dc94a025b74db1926dad Mon Sep 17 00:00:00 2001 From: Prashant-bd <149510496+Prashant-bd@users.noreply.github.com> Date: Thu, 4 Jan 2024 00:19:24 +0530 Subject: [PATCH 3/9] Update SelectCslForm.php --- src/Form/SelectCslForm.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 477fdad..d3f1113 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -118,7 +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->blockCSLAccessedDateFormate = $settings['csl_accessed_date_format'] ?? ''; + $this->blockCSLAccessedDateFormat = $settings['csl_accessed_date_format'] ?? ''; } } } @@ -263,10 +263,10 @@ private function renderCitation($csl_name): ?array { } // If Accessed is configured, add current date. - if (!empty($this->blockCSLAccessedDateFormate)) { + if (!empty($this->blockCSLAccessedDateFormat)) { $current_date = new DrupalDateTime('now'); $citationItems[0]->URL = $citationItems[0]->URL . ' ' . - $current_date->format($this->blockCSLAccessedDateFormate); + $current_date->format($this->blockCSLAccessedDateFormat); } $style = $this->citationHelper->loadStyle($csl_name); From bbc1ff1f5b9943e757677b044723ff66bd9a9ffa Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Thu, 4 Jan 2024 17:43:22 +0530 Subject: [PATCH 4/9] DGIR-139 : Update date format for citation block --- src/Form/SelectCslForm.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index d3f1113..459c8e9 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -262,11 +262,20 @@ 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)) { + // If Accessed is configured, add the current date. + // TODO : Check for configuration requirement. + if (empty($citationItems[0]->accessed) && empty($this->blockCSLAccessedDateFormate)) { $current_date = new DrupalDateTime('now'); - $citationItems[0]->URL = $citationItems[0]->URL . ' ' . - $current_date->format($this->blockCSLAccessedDateFormat); + + // TODO : User CSL JSON date formatting. + $date_parts = [ + $current_date->format('Y'), + $current_date->format('m'), + $current_date->format('d'), + ]; + + // Assign the inner object to the outer array + $citationItems[0]->accessed = (object)['date-parts' => [$date_parts]]; } $style = $this->citationHelper->loadStyle($csl_name); From 701d7cf9a0f0654f10be2737e384144d3d47ea8e Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Thu, 4 Jan 2024 17:51:15 +0530 Subject: [PATCH 5/9] DGIR-139 : Linting error fixes --- src/Form/SelectCslForm.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 459c8e9..0f610b9 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -263,18 +263,18 @@ private function renderCitation($csl_name): ?array { } // If Accessed is configured, add the current date. - // TODO : Check for configuration requirement. + // @todo Check for configuration requirement. if (empty($citationItems[0]->accessed) && empty($this->blockCSLAccessedDateFormate)) { $current_date = new DrupalDateTime('now'); - // TODO : User CSL JSON date formatting. + // @todo User CSL JSON date formatting. $date_parts = [ $current_date->format('Y'), $current_date->format('m'), $current_date->format('d'), ]; - // Assign the inner object to the outer array + // Assign the inner object to the outer array. $citationItems[0]->accessed = (object)['date-parts' => [$date_parts]]; } From 44cc89f64005c0f39ef05de0604632a081e1edae Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Thu, 4 Jan 2024 17:52:12 +0530 Subject: [PATCH 6/9] DGIR-139 : Linting error fixes --- src/Form/SelectCslForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 0f610b9..b9bc7e2 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -275,7 +275,7 @@ private function renderCitation($csl_name): ?array { ]; // Assign the inner object to the outer array. - $citationItems[0]->accessed = (object)['date-parts' => [$date_parts]]; + $citationItems[0]->accessed = (object) ['date-parts' => [$date_parts]]; } $style = $this->citationHelper->loadStyle($csl_name); From 1bd9c4c08831f63e2fb6ae6096395a1b7d21ddf9 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Fri, 5 Jan 2024 20:10:59 +0530 Subject: [PATCH 7/9] DGIR-139: Remove configuration and condition for accessed object --- src/Form/SelectCslForm.php | 22 ++++++++-------------- src/Plugin/Block/DisplayCitationsBlock.php | 9 --------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index b9bc7e2..45aa36a 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -118,7 +118,6 @@ 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'] ?? ''; } } } @@ -262,21 +261,16 @@ private function renderCitation($csl_name): ?array { $citationItems[0]->URL = Url::fromUserInput($node_url)->setAbsolute()->toString(); } - // If Accessed is configured, add the current date. - // @todo Check for configuration requirement. - if (empty($citationItems[0]->accessed) && empty($this->blockCSLAccessedDateFormate)) { - $current_date = new DrupalDateTime('now'); + // Pass the current date to Accessed. + $current_date = new DrupalDateTime('now'); - // @todo User CSL JSON date formatting. - $date_parts = [ - $current_date->format('Y'), - $current_date->format('m'), - $current_date->format('d'), - ]; + $date_parts = [ + $current_date->format('Y'), + $current_date->format('m'), + $current_date->format('d'), + ]; - // Assign the inner object to the outer array. - $citationItems[0]->accessed = (object) ['date-parts' => [$date_parts]]; - } + $citationItems[0]->accessed = (object) ['date-parts' => [$date_parts]]; $style = $this->citationHelper->loadStyle($csl_name); return $this->citationHelper->renderWithCiteproc($citationItems, $style); diff --git a/src/Plugin/Block/DisplayCitationsBlock.php b/src/Plugin/Block/DisplayCitationsBlock.php index 937c90a..bca658a 100644 --- a/src/Plugin/Block/DisplayCitationsBlock.php +++ b/src/Plugin/Block/DisplayCitationsBlock.php @@ -123,7 +123,6 @@ 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'), @@ -145,13 +144,6 @@ public function blockForm($form, FormStateInterface $form_state) { '#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; @@ -164,7 +156,6 @@ 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']; } } From 30f9c9c3539769fc1ca97145c22ba61439eb9c55 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Fri, 5 Jan 2024 20:18:30 +0530 Subject: [PATCH 8/9] DGIR-139: Remove unwanted code --- src/Form/SelectCslForm.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 45aa36a..869e60c 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -32,13 +32,6 @@ class SelectCslForm extends FormBase { */ private $blockCSLType; - /** - * CSL type value from block. - * - * @var string - */ - private $blockCSLAccessedDateFormat; - /** * The route match. * From 364577380d4b636b3f477ada0e4d2eddd7093384 Mon Sep 17 00:00:00 2001 From: Prashant-bd <149510496+Prashant-bd@users.noreply.github.com> Date: Wed, 10 Jan 2024 11:49:11 +0530 Subject: [PATCH 9/9] Update DisplayCitationsBlock.php --- src/Plugin/Block/DisplayCitationsBlock.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Plugin/Block/DisplayCitationsBlock.php b/src/Plugin/Block/DisplayCitationsBlock.php index bca658a..78ebd69 100644 --- a/src/Plugin/Block/DisplayCitationsBlock.php +++ b/src/Plugin/Block/DisplayCitationsBlock.php @@ -143,7 +143,6 @@ public function blockForm($form, FormStateInterface $form_state) { '#attributes' => ['aria-label' => $this->t('Select Citation')], '#default_value' => $defaultCSLType, ]; - } return $form;