diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 1647c96..658c3a6 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -2,13 +2,14 @@ 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; 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; @@ -141,7 +142,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['error_handling_element']['#markup'] = 1; // Log error message. - $this->logger->error($csl); + $this->logger->error(json_encode($csl)); return $form; } @@ -227,7 +228,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(); @@ -257,6 +258,17 @@ private function renderCitation($csl_name): ?array { $citationItems[0]->URL = Url::fromUserInput($node_url)->setAbsolute()->toString(); } + // Pass the current date to Accessed. + $current_date = new DrupalDateTime('now'); + + $date_parts = [ + $current_date->format('Y'), + $current_date->format('m'), + $current_date->format('d'), + ]; + + $citationItems[0]->accessed = (object) ['date-parts' => [$date_parts]]; + $style = $this->citationHelper->loadStyle($csl_name); return $this->citationHelper->renderWithCiteproc($citationItems, $style); } 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': diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index 42e83d3..687dca3 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -48,28 +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': - if (strpos($name, ',') !== FALSE) { - $names = explode(',', $name); - return is_array($names) ? - ['family' => $names[1], 'given' => $names[0]] : - ['family' => $name]; - } - else { - $names = explode(' ', $name); - return is_array($names) ? - ['given' => $names[1], 'family' => $names[0]] : - ['family' => $name]; - } + return match ($type) { + 'person' => $this->getNameParts($name), + 'institution' => ['family' => $name], + default => ['family' => $name], + }; - case 'institution': - return ['family' => $name]; + } + + /** + * Gets the first name and last name from name. + * + * @param string $name + * The person's name. + * + * @return array + * An array of name parts. + * + * @throws \Exception + */ + 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 { + $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'); + } + + return [ + 'given' => $firstName, + 'family' => $lastName, + ]; + } + catch (\Exception $e) { + return ['family' => $name]; } - return $name; } }