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

ISSUE-236: Citation Formatter check for null #238

Merged
merged 11 commits into from
Nov 7, 2022
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"league/html-to-markdown":"^5.0.0",
"erusev/parsedown": "^1.7",
"drupal/webform": "^5.23 || ^6.0",
"ext-json": "*"
"ext-json": "*",
"seboettg/citeproc-php": "dev-master"
}
}
68 changes: 38 additions & 30 deletions src/CiteProc/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,39 +89,47 @@ public function bibliography(string $locale, array $styleNames, array $jsonData)
else {
$citeProc = new CiteProc($style);
}
$bibliography = $citeProc->render($jsonData, "bibliography");
$cssStyles = $citeProc->renderCssStyles();
$parsedStyle = $this->parseCSS($cssStyles);
$processed_bibliography = $bibliography;
foreach ($parsedStyle as $css_prop => $css_statements) {
$css_selector = ltrim($css_prop, '.');
$css_selector_len = strlen($css_selector);
$pos = strpos($processed_bibliography,$css_selector);
if ($pos !== false) {
$inline_rule = ' style="';
foreach($css_statements as $css_property => $css_value) {
$inline_rule .= $css_property . ':' . $css_value . ';';
}
$inline_rule .= '"';
$start = 0;
while (($inline_pos = strpos(($processed_bibliography),$css_selector,$start)) !== false) {
$processed_bibliography = substr_replace($processed_bibliography, $inline_rule, $inline_pos + $css_selector_len + 1, 0);
$start = $inline_pos + 1;
$citeProc->init();
$citeProcBibliography = $citeProc->getContext()->getBibliography();
if($citeProcBibliography !== null) {
$bibliography = $citeProc->render($jsonData, "bibliography");
$cssStyles = $citeProc->renderCssStyles();
$parsedStyle = $this->parseCSS($cssStyles);
$processed_bibliography = $bibliography;
foreach ($parsedStyle as $css_prop => $css_statements) {
$css_selector = ltrim($css_prop, '.');
$css_selector_len = strlen($css_selector);
$pos = strpos($processed_bibliography,$css_selector);
if ($pos !== false) {
$inline_rule = ' style="';
foreach($css_statements as $css_property => $css_value) {
$inline_rule .= $css_property . ':' . $css_value . ';';
}
$inline_rule .= '"';
$start = 0;
while (($inline_pos = strpos(($processed_bibliography),$css_selector,$start)) !== false) {
$processed_bibliography = substr_replace($processed_bibliography, $inline_rule, $inline_pos + $css_selector_len + 1, 0);
$start = $inline_pos + 1;
}
}
}
if ($style_iterator > 0) {
$processed_bibliography = '<div class="hidden csl-bib-body-container '. $selected_style . '">' . $processed_bibliography . '</div>';
}
else {
$processed_bibliography = '<div class="csl-bib-body-container '. $selected_style . '">' . $processed_bibliography . '</div>';
}
$rendered_bibliography .= $processed_bibliography;
$style_file = $citation_style_directory . '/' . $selected_style . '.csl';
$style_xml = simplexml_load_file($style_file);
$style_title = $style_xml->info->title->__toString();
$select .= '<option value="' . $selected_style . '">' . $style_title . '</option>';
++$style_iterator;
}
else {
$message = 'The ' . $selected_style . ' CSL style wasn\'t rendered because it doesn\'t contain a bibliography node.' ;
\Drupal::logger('format_strawberryfield')->info($message);
}
if ($style_iterator > 0) {
$processed_bibliography = '<div class="hidden csl-bib-body-container '. $selected_style . '">' . $processed_bibliography . '</div>';
}
else {
$processed_bibliography = '<div class="csl-bib-body-container '. $selected_style . '">' . $processed_bibliography . '</div>';
}
$rendered_bibliography .= $processed_bibliography;
$style_file = $citation_style_directory . '/' . $selected_style . '.csl';
$style_xml = simplexml_load_file($style_file);
$style_title = $style_xml->info->title->__toString();
$select .= '<option value="' . $selected_style . '">' . $style_title . '</option>';
++$style_iterator;
}
} catch (Exception $e) {
echo $e->getMessage();
Expand Down
24 changes: 19 additions & 5 deletions src/Element/EntityAutocompleteUUID.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
// static::getEntityLabels().
$element['#default_value'] = [$element['#default_value']];
}

// Allow an empty array in case of multiple as default value.
if ($element['#default_value']) {
if (!(reset($element['#default_value']) instanceof EntityInterface)) {
throw new \InvalidArgumentException('The #default_value property has to be an entity object or an array of entity objects.');
Expand Down Expand Up @@ -300,19 +300,33 @@ public static function validateEntityAutocomplete(array &$element, FormStateInte
$value = isset($last_value['target_id']) ? $last_value['target_id'] : $last_value;
}
}
if (empty($value)) {
return;
}
// Now we need to turn the IDs into UUIDs.
if (is_array($value)) {
//$entities = \Drupal::entityTypeManager()->getStorage($element['#target_type'])->loadMultiple($entity_ids);
// Check if target ID is being passed
$values = $value;
$value = [];
foreach ($values as $value_entry ) {
if (isset($value_entry['target_id'])) {
$value[] = $value_entry['target_id'];
}
}
$entities = \Drupal::entityTypeManager()->getStorage($element['#target_type'])->loadMultiple($value);
if ($entities) {
foreach($entities as $entity) {
$value[] = $entity->uuid();
}
}
}
else {
elseif ($value) {
$entities = \Drupal::entityTypeManager()->getStorage($element['#target_type'])->load($value);
if ($entities) {
$value = $entities->uuid();
}
}



$form_state->setValueForElement($element, $value);
}

Expand Down
92 changes: 48 additions & 44 deletions src/Plugin/Field/FieldFormatter/StrawberryCitationFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,16 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
$citation_style_directory = $this->cslStylesPath;
// Get the list of style files.
$style_list = $this->fileSystem->scanDirectory($citation_style_directory, '/\.(csl)$/i', ['recurse' => FALSE, 'key' => 'name']);
# Generate a list of select options and push in the styles.
// Generate a list of select options and push in the styles.
foreach($style_list as $style) {
$style_name = $style->name;
$style_xml = simplexml_load_file($style->uri);
$style_title = $style_xml->info->title->__toString();
$style_options[$style_name] = $this->t($style_title);
// Check if the bibliography node exists and only add to the list if it does.
$style_bibliography_exists = isset($style_xml->bibliography);
if($style_bibliography_exists) {
$style_title = $style_xml->info->title->__toString();
$style_options[$style_name] = $this->t($style_title);
}
}
// Alphabetize them.
asort($style_options);
Expand Down Expand Up @@ -320,6 +324,47 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
// In case someone decided to wipe the original context?
// We bring it back!
$context = $context + $original_context;
// Render data from metadata template into JSON string.
$rendered_json_string = $metadatadisplayentity->renderNative($context);

// Get styles selected from formatter settings.
$selected_styles = $this->settings['citationstyle'];
// Get language key from settings.
$selected_locale_key = false;
if ($this->getSetting('localekey')) {
$selected_locale_key = $this->settings['localekey'];
}
$selected_locale_value = array_key_exists($selected_locale_key, $jsondata) ? $jsondata[$selected_locale_key] : false;
if ($selected_locale_value) {
$available_locale = trim($selected_locale_value);
}
elseif ($langcode) {
$available_locale = trim($langcode);
}

$data = json_decode($rendered_json_string);
$json_error = json_last_error();
if ($json_error != JSON_ERROR_NONE) {
$message = $this->t('There was an issue decoding your metadata as JSON for node @id, field @field',
[
'@id' => $nodeid,
'@field' => $items->getName(),
]);
return $elements[$delta] = ['#markup' => $message];
}
$render = new Render();
$bibliography = $render->bibliography($available_locale, $selected_styles, $data);
$elements[$delta] = [
'#type' => 'container',
'#attributes' => [
'id' => 'bibliography' . $uniqueid,
'class' => ['bibliography'],
]
];
$elements[$delta]['#attached']['library'][] = 'format_strawberryfield/citations_strawberry';
$elements[$delta]['bibliography'] = [
'#markup' => \Drupal\Core\Render\Markup::create($bibliography),
];
}
catch (\Exception $e) {
// Render each element as markup.
Expand All @@ -331,47 +376,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
];
}
}
// Render data from metadata template into JSON string.
$rendered_json_string = $metadatadisplayentity->renderNative($context);

// Get styles selected from formatter settings.
$selected_styles = $this->settings['citationstyle'];
// Get language key from settings.
$selected_locale_key = false;
if ($this->getSetting('localekey')) {
$selected_locale_key = $this->settings['localekey'];
}
$selected_locale_value = array_key_exists($selected_locale_key, $jsondata) ? $jsondata[$selected_locale_key] : false;
if ($selected_locale_value) {
$available_locale = trim($selected_locale_value);
}
elseif ($langcode) {
$available_locale = trim($langcode);
}

$data = json_decode($rendered_json_string);
$json_error = json_last_error();
if ($json_error != JSON_ERROR_NONE) {
$message = $this->t('There was an issue decoding your metadata as JSON for node @id, field @field',
[
'@id' => $nodeid,
'@field' => $items->getName(),
]);
return $elements[$delta] = ['#markup' => $message];
}
$render = new Render();
$bibliography = $render->bibliography($available_locale, $selected_styles, $data);
$elements[$delta] = [
'#type' => 'container',
'#attributes' => [
'id' => 'bibliography' . $uniqueid,
'class' => ['bibliography'],
]
];
$elements[$delta]['#attached']['library'][] = 'format_strawberryfield/citations_strawberry';
$elements[$delta]['bibliography'] = [
'#markup' => \Drupal\Core\Render\Markup::create($bibliography),
];
return $elements;
}

Expand Down