From 3922cb4410ff9936fe7e8a046df6b542b642354b Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Tue, 2 Jan 2024 16:43:19 +0530 Subject: [PATCH 01/19] 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 02/19] 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 03/19] 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 04/19] 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 05/19] 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 06/19] 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 07/19] 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 08/19] 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 09/19] 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; From 4ac3a79da784eeb0c8c85faf721a229c28a7c1ec Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Fri, 19 Jan 2024 11:48:06 +0530 Subject: [PATCH 10/19] FDR-399: Fixed warnings related to Person taxonomy last name missing --- src/Normalizer/NormalizerBase.php | 46 ++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index 42e83d3..3248590 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -54,14 +54,20 @@ protected function formatNameVariables($name, $type): array { case 'person': if (strpos($name, ',') !== FALSE) { $names = explode(',', $name); - return is_array($names) ? - ['family' => $names[1], 'given' => $names[0]] : + // Get the first name and last name. + $name_details = is_array($names) ? $this->getFirstNameAndLastName($names) : []; + + return !empty($name_details) ? + ['family' => $name_details['last_name'], 'given' => $name_details['first_name']] : ['family' => $name]; } else { $names = explode(' ', $name); - return is_array($names) ? - ['given' => $names[1], 'family' => $names[0]] : + // Get the first name and last name. + $name_details = is_array($names) ? $this->getFirstNameAndLastName($names) : []; + + return !empty($name_details) ? + ['given' => $name_details['last_name'], 'family' => $name_details['first_name']] : ['family' => $name]; } @@ -72,4 +78,36 @@ protected function formatNameVariables($name, $type): array { return $name; } + /** + * Get the first name and last name from name array. + * + * @param array $names + * An array of name indexes. + */ + protected function getFirstNameAndLastName(array $names) { + if (empty($names)) { + return []; + } + + $name_index_count = count($names); + // Last index in the names array would be considered as the last name. + $last_name = $name_index_count > 1 ? $names[$name_index_count - 1] : ''; + + // Rest all make up the first name. + $first_name = []; + if ($name_index_count > 1) { + for ($i = 0; $i < $name_index_count - 1; $i++) { + $first_name[] = $names[$i]; + } + } + else { + $first_name = $names[0]; + } + + return [ + 'first_name' => is_array($first_name) ? implode(' ', $first_name) : $first_name, + 'last_name' => $last_name, + ]; + } + } From 9e2f125cdb17cb006338cf3d16c9b489b239e7a8 Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Fri, 19 Jan 2024 11:51:57 +0530 Subject: [PATCH 11/19] FDR-399: Codesniffer fixes --- src/Normalizer/NormalizerBase.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index 3248590..2e539ab 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -58,7 +58,10 @@ protected function formatNameVariables($name, $type): array { $name_details = is_array($names) ? $this->getFirstNameAndLastName($names) : []; return !empty($name_details) ? - ['family' => $name_details['last_name'], 'given' => $name_details['first_name']] : + [ + 'family' => $name_details['last_name'], + 'given' => $name_details['first_name'], + ] : ['family' => $name]; } else { @@ -67,7 +70,10 @@ protected function formatNameVariables($name, $type): array { $name_details = is_array($names) ? $this->getFirstNameAndLastName($names) : []; return !empty($name_details) ? - ['given' => $name_details['last_name'], 'family' => $name_details['first_name']] : + [ + 'given' => $name_details['last_name'], + 'family' => $name_details['first_name'], + ] : ['family' => $name]; } From 7331940f866a1cccd28f922836a48a35f018b3b9 Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Fri, 19 Jan 2024 19:14:04 +0530 Subject: [PATCH 12/19] FDR-511: Fixed citations publisher error --- src/Normalizer/ContentEntityNormalizer.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Normalizer/ContentEntityNormalizer.php b/src/Normalizer/ContentEntityNormalizer.php index fce5de9..3837bab 100644 --- a/src/Normalizer/ContentEntityNormalizer.php +++ b/src/Normalizer/ContentEntityNormalizer.php @@ -77,11 +77,20 @@ private function normalizeCslFieldsForCiteProc(&$normalized_field_items) { foreach ($normalized_field_items as $cslKey => $cslValueArray) { $fieldType = $this->getCslVariableType($cslKey); - // If the variable type if string, comma separate the values. + // If the variable type is string, comma separate the values. switch ($fieldType) { case 'string': case 'number': - $normalized_field_items[$cslKey] = is_array($cslValueArray) ? implode(', ', $cslValueArray) : $cslValueArray; + $values = []; + if (is_array($cslValueArray)) { + foreach ($cslValueArray as $key => $arrayValue) { + $values[] = is_array($arrayValue) ? $arrayValue[array_key_first($arrayValue)] : $arrayValue; + } + } + else { + $values = $cslValueArray; + } + $normalized_field_items[$cslKey] = is_array($values) ? implode(', ', $values) : $values; break; case 'array': From eb383223060bf1253b0ad91487c453c9638b04b9 Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Tue, 23 Jan 2024 14:06:03 +0530 Subject: [PATCH 13/19] FDR-399: Addressed PR feedback --- src/Normalizer/NormalizerBase.php | 89 +++++++++++++++---------------- 1 file changed, 42 insertions(+), 47 deletions(-) diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index 2e539ab..49fdc23 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -52,30 +52,11 @@ protected function formatDateVariables($date): array { protected function formatNameVariables($name, $type): array { switch ($type) { case 'person': - if (strpos($name, ',') !== FALSE) { - $names = explode(',', $name); - // Get the first name and last name. - $name_details = is_array($names) ? $this->getFirstNameAndLastName($names) : []; - - return !empty($name_details) ? - [ - 'family' => $name_details['last_name'], - 'given' => $name_details['first_name'], - ] : - ['family' => $name]; - } - else { - $names = explode(' ', $name); - // Get the first name and last name. - $name_details = is_array($names) ? $this->getFirstNameAndLastName($names) : []; - - return !empty($name_details) ? - [ - 'given' => $name_details['last_name'], - 'family' => $name_details['first_name'], - ] : - ['family' => $name]; - } + $name_values = strpos($name, ',') !== FALSE ? + $this->getFirstNameAndLastName($name, ',') : + $this->getFirstNameAndLastName($name); + + return !empty($name_values) ? $name_values : ['family' => $name]; case 'institution': return ['family' => $name]; @@ -85,35 +66,49 @@ protected function formatNameVariables($name, $type): array { } /** - * Get the first name and last name from name array. + * Get the first name and last name from name. * - * @param array $names - * An array of name indexes. + * @param string $name + * The Person name. + * @param string $separator + * The separator used to explode name. */ - protected function getFirstNameAndLastName(array $names) { - if (empty($names)) { - return []; - } - + protected function getFirstNameAndLastName($name, $separator = ' ') { + $names = explode($separator, $name); $name_index_count = count($names); - // Last index in the names array would be considered as the last name. - $last_name = $name_index_count > 1 ? $names[$name_index_count - 1] : ''; - - // Rest all make up the first name. - $first_name = []; - if ($name_index_count > 1) { - for ($i = 0; $i < $name_index_count - 1; $i++) { - $first_name[] = $names[$i]; - } + + // If the separator is a comma, the last name is the first index. + if ($separator === ',') { + return [ + 'family' => trim($names[1]), + 'given' => trim($names[0]), + ]; } else { - $first_name = $names[0]; - } + $first_name = []; + $last_name = ''; + + if ($name_index_count > 1) { + // The last index in the names array would be considered as the last + // name. + $last_name = $names[$name_index_count - 1]; - return [ - 'first_name' => is_array($first_name) ? implode(' ', $first_name) : $first_name, - 'last_name' => $last_name, - ]; + // The rest all make up the first name. + for ($i = 0; $i < $name_index_count - 1; $i++) { + $first_name[] = $names[$i]; + } + } + else { + // Handling the case where only a single word name is provided. + $first_name = $names['0']; + } + + $first_name = is_array($first_name) ? implode(' ', $first_name) : $first_name; + return [ + 'given' => trim($last_name), + 'family' => trim($first_name), + ]; + } } } From d458898ece5babe2a26a3904fb01e55b559c6d5a Mon Sep 17 00:00:00 2001 From: Noel Chiasson Date: Tue, 23 Jan 2024 13:03:52 -0400 Subject: [PATCH 14/19] Reversing order of given/family name references --- src/Normalizer/NormalizerBase.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index 49fdc23..321c13f 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -80,8 +80,9 @@ protected function getFirstNameAndLastName($name, $separator = ' ') { // If the separator is a comma, the last name is the first index. if ($separator === ',') { return [ - 'family' => trim($names[1]), - 'given' => trim($names[0]), + 'given' => trim($names[1]), + 'family' => trim($names[0]), + ]; } else { @@ -105,8 +106,8 @@ protected function getFirstNameAndLastName($name, $separator = ' ') { $first_name = is_array($first_name) ? implode(' ', $first_name) : $first_name; return [ - 'given' => trim($last_name), - 'family' => trim($first_name), + 'family' => trim($last_name), + 'given' => trim($first_name), ]; } } From 0bd4c4be2e75f15b3dce7d1bb92dff860fbdce37 Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Wed, 24 Jan 2024 14:14:13 +0530 Subject: [PATCH 15/19] FDR-399: Handling the case where only a single word name is provided --- src/Normalizer/NormalizerBase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index 321c13f..8f48627 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -101,7 +101,7 @@ protected function getFirstNameAndLastName($name, $separator = ' ') { } else { // Handling the case where only a single word name is provided. - $first_name = $names['0']; + return []; } $first_name = is_array($first_name) ? implode(' ', $first_name) : $first_name; From b8a121415d83418424ae5f408295fadb9ef6c72a Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Wed, 24 Jan 2024 14:15:22 +0530 Subject: [PATCH 16/19] FDR-399: Fixed extra space in array --- src/Normalizer/NormalizerBase.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index 8f48627..32f1710 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -82,7 +82,6 @@ protected function getFirstNameAndLastName($name, $separator = ' ') { return [ 'given' => trim($names[1]), 'family' => trim($names[0]), - ]; } else { From e8f4af9bad895270ea8d8e65853d161655eff8bd Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Thu, 25 Jan 2024 15:06:34 +0530 Subject: [PATCH 17/19] FDR-399: Handling the case when the name starts with comma --- src/Normalizer/NormalizerBase.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index 32f1710..3586ae4 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -79,6 +79,13 @@ protected function getFirstNameAndLastName($name, $separator = ' ') { // If the separator is a comma, the last name is the first index. if ($separator === ',') { + // Handling the case when the name starts with comma. Eg: ,First. + if (!$names[0]) { + return [ + 'family' => trim($names[1]), + ]; + } + return [ 'given' => trim($names[1]), 'family' => trim($names[0]), From f9d36478f6f7f84826e1b3879661156e9a9f3e4f Mon Sep 17 00:00:00 2001 From: Akanksha Date: Thu, 1 Feb 2024 17:19:51 +0530 Subject: [PATCH 18/19] FDR-399: Switch to regex --- src/Form/SelectCslForm.php | 2 +- src/Normalizer/NormalizerBase.php | 90 +++++++++++++------------------ 2 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 869e60c..c895662 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -8,8 +8,8 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; -use Drupal\path_alias\AliasManagerInterface; use Drupal\islandora_citations\IslandoraCitationsHelper; +use Drupal\path_alias\AliasManagerInterface; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index 3586ae4..b55f068 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -48,74 +48,60 @@ protected function formatDateVariables($date): array { /** * Formats name variables as per csl json. + * + * @throws \Exception */ protected function formatNameVariables($name, $type): array { - switch ($type) { - case 'person': - $name_values = strpos($name, ',') !== FALSE ? - $this->getFirstNameAndLastName($name, ',') : - $this->getFirstNameAndLastName($name); - - return !empty($name_values) ? $name_values : ['family' => $name]; + return match ($type) { + 'person' => $this->getNameParts($name), + 'institution' => ['family' => $name], + default => ['family' => $name], + }; - case 'institution': - return ['family' => $name]; - } - - return $name; } /** - * Get the first name and last name from name. + * Gets the first name and last name from name. * * @param string $name - * The Person name. - * @param string $separator - * The separator used to explode name. + * The person's name. + * + * @return array + * An array of name parts. + * + * @throws \Exception */ - protected function getFirstNameAndLastName($name, $separator = ' ') { - $names = explode($separator, $name); - $name_index_count = count($names); - - // If the separator is a comma, the last name is the first index. - if ($separator === ',') { - // Handling the case when the name starts with comma. Eg: ,First. - if (!$names[0]) { - return [ - 'family' => trim($names[1]), - ]; - } - - return [ - 'given' => trim($names[1]), - 'family' => trim($names[0]), - ]; - } - else { - $first_name = []; - $last_name = ''; - - if ($name_index_count > 1) { - // The last index in the names array would be considered as the last - // name. - $last_name = $names[$name_index_count - 1]; - - // The rest all make up the first name. - for ($i = 0; $i < $name_index_count - 1; $i++) { - $first_name[] = $names[$i]; - } + protected function getNameParts(string $name): array { + try { + // If name has a comma, we assume that it's + // formatted as Lastname,Firstname + // This is kind of dicey, names might just contain commas. + // @todo but that's an edge case which can be handled when + // encountered. + if (str_contains($name, ',')) { + $nameParts = explode(',', $name); + $firstName = $nameParts[1] ?? ''; + $lastName = $nameParts[0] ?? ''; } else { - // Handling the case where only a single word name is provided. - return []; + $name = trim($name); + $lastName = (!str_contains($name, ' ')) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name); + $firstName = trim(preg_replace('#' . preg_quote($lastName, '#') . '#', '', $name)); + } + + if (empty($firstName) && empty($lastName)) { + throw new \Exception('Name is not formatted properly'); } - $first_name = is_array($first_name) ? implode(' ', $first_name) : $first_name; return [ - 'family' => trim($last_name), - 'given' => trim($first_name), + 'given' => $firstName, + 'family' => $lastName, ]; } + catch (\Exception $e) { + return ['family' => $name]; + } + } } From 1f7da328305952ab52c1b2fcae4c7ef0fd19d4b4 Mon Sep 17 00:00:00 2001 From: Akanksha Date: Thu, 1 Feb 2024 17:32:31 +0530 Subject: [PATCH 19/19] FDR-399: Switch to regex --- src/Form/SelectCslForm.php | 4 ++-- src/Normalizer/NormalizerBase.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index c895662..7c45ce0 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -136,7 +136,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ]; // Log error message. - $this->logger->error($csl); + $this->logger->error(json_encode($csl)); return $form; } @@ -225,7 +225,7 @@ public function getDefaultCitation($csl_name) { try { // Method call to render citation. $rendered = $this->renderCitation($csl_name); - return $rendered['data']; + return $rendered['data'] ?? NULL; } catch (\Throwable $e) { return $e->getMessage(); diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index b55f068..687dca3 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -89,7 +89,7 @@ protected function getNameParts(string $name): array { $firstName = trim(preg_replace('#' . preg_quote($lastName, '#') . '#', '', $name)); } - if (empty($firstName) && empty($lastName)) { + if (empty($firstName) || empty($lastName)) { throw new \Exception('Name is not formatted properly'); }