Skip to content

Commit

Permalink
Merge branch 'main' into dgir-123
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant-bd authored Feb 7, 2024
2 parents b293267 + a60ae16 commit 8910d41
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 22 deletions.
18 changes: 15 additions & 3 deletions src/Form/SelectCslForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 11 additions & 2 deletions src/Normalizer/ContentEntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
66 changes: 49 additions & 17 deletions src/Normalizer/NormalizerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit 8910d41

Please sign in to comment.