diff --git a/bibcite_footnotes.module b/bibcite_footnotes.module index 1bad342..66b18b0 100644 --- a/bibcite_footnotes.module +++ b/bibcite_footnotes.module @@ -9,8 +9,9 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; use Drupal\Core\Link; +use Drupal\bibcite_footnotes\CitationTools; -/** +/**_bibcite_foot * Implements hook_help(). */ function bibcite_footnotes_help($route_name, RouteMatchInterface $route_match) { @@ -57,9 +58,9 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) { ]; $references['#attached']['library'][] = 'bibcite_footnotes/reference_footnote'; - - $serializer = \Drupal::service('serializer'); - + $dont_show_backlink_text = $config->get('filters.filter_reference_footnotes.settings.reference_dont_show_backlink_text'); + $sort_references_by = $config->get('filters.filter_reference_footnotes.settings.reference_sort_by'); + $citation_tools = new CitationTools(); foreach ($footnotes as $fn) { $item = [ '#id' => $fn['fn_id'], @@ -68,12 +69,14 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) { ], ]; $build = []; + $reference_entity_id = _bibcite_footnotes_get_reference_entity_id_from_bibcite_footnote($fn['text']); + if (!is_array($fn['ref_id'])) { // Output normal footnote. $url = Url::fromUserInput('#' . $fn['ref_id'], ['attributes' => ['id' => $fn['fn_id'], 'class' => 'footnote-link']]); - $link = Link::fromTextAndUrl($fn['value'], $url)->toRenderable(); + $link = Link::fromTextAndUrl(($dont_show_backlink_text && $reference_entity_id ? '^' : $fn['value']), $url)->toRenderable(); $build[] = $link; } else { @@ -95,16 +98,12 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) { } - preg_match('/\[bibcite_reference:(\d*)\]/', $fn['text'], $matches); - $reference_entity_id = $matches[1]; if (!empty($reference_entity_id)) { - $reference_storage = \Drupal::entityTypeManager() - ->getStorage('bibcite_reference') - ->load($reference_entity_id); - $data = $serializer->normalize($reference_storage, 'csl'); - $build[] = ['#theme' => 'bibcite_citation', '#data' => $data]; + $citation_build = $citation_tools->getRenderableReference($reference_entity_id); + $build[] = $citation_build; $render = render($build); $item['#markup'] = $render; + $item['sort'] = trim(strip_tags(render($citation_build))); $references['#items'][] = $item; } @@ -119,7 +118,41 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) { $variables['notes'] = $notes; + if ($sort_references_by == 'alphabetical') { + usort($references['#items'], '_bibcite_footnotes_reference_array_cmp'); + } $variables['references'] = $references; +} + +/** + * @param $text The text of the bibcite reference placeholder. + * @return Entity id of the Reference. + */ +function _bibcite_footnotes_get_reference_entity_id_from_bibcite_footnote($text){ + preg_match('/\[bibcite_reference:(\d*)\]/', $text, $matches); + return $matches[1]; +} + +function _bibcite_footnotes_reference_array_cmp($a, $b) { + $a1 = (!empty($a['sort']) ? strtolower($a['sort']) : ''); + $b1 = (!empty($b['sort']) ? strtolower($b['sort']) : ''); + return strcmp($a1, $b1); +} + +function bibcite_footnotes_preprocess_footnote_link(&$variables) { +// $variables['fn']['fn']['#type'] = 'markeup'; +// $variables['fn']['fn']['#markup'] = '

Hello!

'; + $fn = $variables['fn']['fn']; + // TODO: Make a more formal way to denote inline citations. + $citation_is_inline = ($fn['value'][0] == '(' && $fn['value'][strlen($fn['value']) - 1] == ')') ? '-inline' : ''; + $class = 'see-footnote' . $citation_is_inline; + // Generate the hover text + $citation_tools = new CitationTools(); + $citation_entity_id = _bibcite_footnotes_get_reference_entity_id_from_bibcite_footnote($fn['text']); + $title = trim(strip_tags(render($citation_tools->getRenderableReference($citation_entity_id)))); + $url = Url::fromUserInput('#' . $fn['fn_id'], ['attributes' => ['id' => $fn['ref_id'], 'class' => $class, 'title' => $title]]); + $link = Link::fromTextAndUrl($fn['value'], $url)->toRenderable(); + $variables['fn']['fn'][] = $link; } @@ -136,69 +169,14 @@ function bibcite_footnotes_theme() { function bibcite_footnotes_theme_registry_alter(&$theme_registry) { unset($theme_registry['footnote_list']['function']); -// $theme_registry['footnote_list']['function'] = 'bibcite_footnotes_theme_footnote_list'; $theme_registry['footnote_list']['path'] = drupal_get_path('module', 'bibcite_footnotes') . '/templates'; $theme_registry['footnote_list']['template'] = 'footnote-list'; $theme_registry['footnote_list']['variables']['notes'] = []; $theme_registry['footnote_list']['variables']['references'] = []; $theme_registry['footnote_list']['variables']['footnotes'] = []; -} - -function bibcite_footnotes_theme_footnote_list($footnotes) { - // @todo: change this ugly array for arguments in the function. - $footnotes = $footnotes['footnotes']['#footnotes']; - - $notes = [ - '#theme' => 'item_list', - '#list_type' => 'ul', - '#title' => 'Notes', - '#attributes' => ['class' => ''], - '#wrapper_attributes' => ['class' => 'container'], - ]; - -// return drupal_render($content); - $str = '