Browse Source

Squashed commit of the following:

commit fe06f739545d0b9f3204f8ee3aee6a55e3520023
Author: Alexander O'Neill <aoneill@upei.ca>
Date:   Tue Oct 30 15:16:19 2018 -0300

    Add sorting options for references list.

    Support parenthetical citations.
mla_citations_3
Alexander O'Neill 6 years ago
parent
commit
459568efc1
  1. 120
      bibcite_footnotes.module
  2. 8
      js/plugins/reference_footnotes/dialogs/footnotes.js
  3. 22
      src/CitationTools.php
  4. 17
      src/Plugin/Filter/ReferenceFootnotesFilter.php
  5. 16
      templates/footnote-link.html.twig
  6. 2
      templates/footnote-list.html.twig

120
bibcite_footnotes.module

@ -9,8 +9,9 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\bibcite_footnotes\CitationTools;
/** /**_bibcite_foot
* Implements hook_help(). * Implements hook_help().
*/ */
function bibcite_footnotes_help($route_name, RouteMatchInterface $route_match) { 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'; $references['#attached']['library'][] = 'bibcite_footnotes/reference_footnote';
$dont_show_backlink_text = $config->get('filters.filter_reference_footnotes.settings.reference_dont_show_backlink_text');
$serializer = \Drupal::service('serializer'); $sort_references_by = $config->get('filters.filter_reference_footnotes.settings.reference_sort_by');
$citation_tools = new CitationTools();
foreach ($footnotes as $fn) { foreach ($footnotes as $fn) {
$item = [ $item = [
'#id' => $fn['fn_id'], '#id' => $fn['fn_id'],
@ -68,12 +69,14 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) {
], ],
]; ];
$build = []; $build = [];
$reference_entity_id = _bibcite_footnotes_get_reference_entity_id_from_bibcite_footnote($fn['text']);
if (!is_array($fn['ref_id'])) { if (!is_array($fn['ref_id'])) {
// Output normal footnote. // Output normal footnote.
$url = Url::fromUserInput('#' . $fn['ref_id'], ['attributes' => ['id' => $fn['fn_id'], 'class' => 'footnote-link']]); $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; $build[] = $link;
} }
else { 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)) { if (!empty($reference_entity_id)) {
$reference_storage = \Drupal::entityTypeManager() $citation_build = $citation_tools->getRenderableReference($reference_entity_id);
->getStorage('bibcite_reference') $build[] = $citation_build;
->load($reference_entity_id);
$data = $serializer->normalize($reference_storage, 'csl');
$build[] = ['#theme' => 'bibcite_citation', '#data' => $data];
$render = render($build); $render = render($build);
$item['#markup'] = $render; $item['#markup'] = $render;
$item['sort'] = trim(strip_tags(render($citation_build)));
$references['#items'][] = $item; $references['#items'][] = $item;
} }
@ -119,7 +118,41 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) {
$variables['notes'] = $notes; $variables['notes'] = $notes;
if ($sort_references_by == 'alphabetical') {
usort($references['#items'], '_bibcite_footnotes_reference_array_cmp');
}
$variables['references'] = $references; $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'] = '<h2>Hello!</h2>';
$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) { function bibcite_footnotes_theme_registry_alter(&$theme_registry) {
unset($theme_registry['footnote_list']['function']); 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']['path'] = drupal_get_path('module', 'bibcite_footnotes') . '/templates';
$theme_registry['footnote_list']['template'] = 'footnote-list'; $theme_registry['footnote_list']['template'] = 'footnote-list';
$theme_registry['footnote_list']['variables']['notes'] = []; $theme_registry['footnote_list']['variables']['notes'] = [];
$theme_registry['footnote_list']['variables']['references'] = []; $theme_registry['footnote_list']['variables']['references'] = [];
$theme_registry['footnote_list']['variables']['footnotes'] = []; $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 = '<ul class="footnotes">';
// Drupal 7 requires we use "render element" which just introduces a wrapper
// around the old array.
// $footnotes = $footnotes['footnotes'];
// loop through the footnotes.
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('bibcite_reference');
foreach ($footnotes as $fn) {
if (!is_array($fn['ref_id'])) {
// Output normal footnote.
$str .= '<li class="footnote" id="' . $fn['fn_id'] . '"><a class="footnote-label" href="#' . $fn['ref_id'] . '">' . $fn['value'] . '.</a> ';
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);
$build = $view_builder->view($reference_storage, 'citation');
$render = render($build);
$str .= $render . "</li>\n";
}
else {
$str .= $fn['text'] . "</li>\n";
}
}
else {
// Output footnote that has more than one reference to it in the body.
// The only difference is to insert backlinks to all references.
// Helper: we need to enumerate a, b, c...
$abc = str_split("abcdefghijklmnopqrstuvwxyz");
$i = 0;
$str .= '<li class="footnote" id="' . $fn['fn_id'] . '"><a href="#' . $fn['ref_id'][0] . '" class="footnote-label">' . $fn['value'] . '.</a> ';
foreach ($fn['ref_id'] as $ref) {
$str .= '<a class="footnote-multi" href="#' . $ref . '">' . $abc[$i] . '.</a> ';
$i++;
}
$str .= $fn['text'] . "</li>\n";
}
}
return $str;
unset($theme_registry['footnote_link']['function']);
$theme_registry['footnote_link']['path'] = drupal_get_path('module', 'bibcite_footnotes') . '/templates';
$theme_registry['footnote_link']['template'] = 'footnote-link';
} }

8
js/plugins/reference_footnotes/dialogs/footnotes.js

@ -26,8 +26,12 @@
type: 'textarea', type: 'textarea',
label: Drupal.t('Or add free-form footnote text :'), label: Drupal.t('Or add free-form footnote text :'),
setup: function (element) { setup: function (element) {
if (isEdit) if (isEdit) {
this.setValue(element.getText()); var text = element.getText();
if (!text.match(/\[bibcite_reference:(\d*)\]/)) {
this.setValue(text);
}
}
} }
}, },
{ {

22
src/CitationTools.php

@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: aoneill
* Date: 2018-10-30
* Time: 2:36 PM
*/
namespace Drupal\bibcite_footnotes;
class CitationTools {
public function getRenderableReference($reference_entity_id) {
$reference_storage = \Drupal::entityTypeManager()
->getStorage('bibcite_reference')
->load($reference_entity_id);
$serializer = \Drupal::service('serializer');
$data = $serializer->normalize($reference_storage, 'csl');
return ['#theme' => 'bibcite_citation', '#data' => $data];
}
}

17
src/Plugin/Filter/ReferenceFootnotesFilter.php

@ -78,7 +78,22 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
'#type' => 'textfield', '#type' => 'textfield',
'#title' => $this->t('References section label'), '#title' => $this->t('References section label'),
'#description' => $this->t('E.g., "Works Cited" for MLA style.'), '#description' => $this->t('E.g., "Works Cited" for MLA style.'),
'#default_value' => $this->settings['references_section_label'], '#default_value' => $this->settings['references_section_label'],
];
$settings['reference_dont_show_backlink_text'] = [
'#type' => 'checkbox',
'#title' => $this->t("Don't show note 'value' text in reference list."),
'#description' => $this->t("Suitable for MLA-style citations, like (Smith, 33-22)"),
'#default_value' => $this->settings['reference_dont_show_backlink_text'],
];
$settings['reference_sort_by'] = [
'#type' => 'select',
'#title' => $this->t("Sort references list by"),
'#options' => [
'order' => $this->t("Order in text"),
'alphabetical' => $this->t("Alphabetical"),
],
'#default_value' => $this->settings['reference_sort_by'],
]; ];
return $settings; return $settings;
} }

16
templates/footnote-link.html.twig

@ -0,0 +1,16 @@
{#
/**
* @file footnote-link.html.twig
* Default theme implementation to present an inline reference footnote link.
*
* Available variables:
* - fn: Components of the footnote
* - link: Rendered link.
* - attributes: HTML attributes for the container element.
*
* @see template_preprocess_footnote_link().
*
* @ingroup themeable
*/
#}
{{ fn }}

2
templates/footnote-list.html.twig

@ -17,5 +17,5 @@
<div{{ attributes.addClass('footnote-list') }}> <div{{ attributes.addClass('footnote-list') }}>
{{ notes }} {{ notes }}
{{ references }} {{ references }}
</div> </div>

Loading…
Cancel
Save