Browse Source

Sort 'Works Cited' list by author, title.

8.x-1.x
Alexander O'Neill 6 years ago
parent
commit
43080534a7
  1. 49
      bibcite_footnotes.module
  2. 12
      src/CitationTools.php

49
bibcite_footnotes.module

@ -314,3 +314,52 @@ function bibcite_footnotes_get_ckeditor_select_item($entity): array {
$citation_key = $entity->id->first()->getValue()['value']; $citation_key = $entity->id->first()->getValue()['value'];
return [$citation_text, $citation_key]; return [$citation_text, $citation_key];
} }
/**
* Implementation of hook_preprocess_field().
* Sor the works cited list by rendered text, which results in author(s) then title.
*
* @param $variables
* @param $hook
*/
function bibcite_footnotes_preprocess_field(&$variables, $hook) {
$citation_tools = new CitationTools();
if ($variables['field_name'] == 'field_bibcite_fn_works_cited') {
foreach($variables['element'] as $index => $element) {
if (is_numeric($index)) {
$renderable = $citation_tools->getRenderableReference($element['#bibcite_reference']);
$items[] = [
'orig_index' => $index,
'orig_element' => $element,
'rendered' => trim(strip_tags(render($renderable))),
];
}
}
uasort($items, '_bibcite_footnotes_sort_rendered_references');
$i = 0;
foreach ($items as $item) {
$variables['element'][$i] = $item['orig_element'];
$variables['element']['#items'][$i] = $item['orig_element'];
$variables['items'][$i]['content'] = $item['orig_element'];
$i += 1;
}
}
}
/**
* Comparator function for theme field preprocess.
*
* @param $a
* The first renderable element.
* @param $b
* The second renderable element.
*
* @return int
*/
function _bibcite_footnotes_sort_rendered_references($a, $b) {
if ($a['rendered'] == $b['rendered']) {
return 0;
}
return strtolower($a['rendered']) < strtolower($b['rendered']) ? -1 : 1;
}

12
src/CitationTools.php

@ -11,12 +11,14 @@ namespace Drupal\bibcite_footnotes;
class CitationTools { class CitationTools {
public function getRenderableReference($reference_entity_id) { public function getRenderableReference($reference_entity) {
$reference_storage = \Drupal::entityTypeManager() if (is_numeric($reference_entity)) {
->getStorage('bibcite_reference') $reference_entity = \Drupal::entityTypeManager()
->load($reference_entity_id); ->getStorage('bibcite_reference')
->load($reference_entity);
}
$serializer = \Drupal::service('serializer'); $serializer = \Drupal::service('serializer');
$data = $serializer->normalize($reference_storage, 'csl'); $data = $serializer->normalize($reference_entity, 'csl');
return ['#theme' => 'bibcite_citation', '#data' => $data]; return ['#theme' => 'bibcite_citation', '#data' => $data];
} }
} }
Loading…
Cancel
Save