Browse Source

Allow endnotes to contain references.

ibidem
Alexander O'Neill 6 years ago
parent
commit
5ae1a217f6
  1. 45
      bibcite_footnotes.module
  2. 17
      js/plugins/reference_footnotes/dialogs/footnotes.js
  3. 9
      js/plugins/reference_footnotes/plugin.js
  4. 2
      src/Plugin/CKEditorPlugin/ReferenceFootnotes.php
  5. 8
      src/Plugin/Filter/ReferenceFootnotesFilter.php

45
bibcite_footnotes.module

@ -69,16 +69,13 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) {
], ],
]; ];
$build = []; $build = [];
$reference_entity_id = _bibcite_footnotes_get_reference_entity_id_from_bibcite_footnote($fn['text']); $reference_entity_id = _bibcite_footnotes_get_reference_entity_id_from_bibcite_footnote($fn['reference']);
$footnote_link_text = $dont_show_backlink_text && $reference_entity_id ? '^' : $fn['value']; $footnote_link_text = ($dont_show_backlink_text && $reference_entity_id) ? '^' : $fn['value'];
if (!is_array($fn['ref_id'])) { if (!is_array($fn['ref_id'])) {
// 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(($footnote_link_text), $url)->toRenderable(); $link = Link::fromTextAndUrl(($footnote_link_text), $url)->toRenderable();
$build[] = $link; $build['footnote-link'] = $link;
} }
else { else {
// Output footnote that has more than one reference to it in the body. // Output footnote that has more than one reference to it in the body.
@ -88,7 +85,7 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) {
$i = 0; $i = 0;
$url = Url::fromUserInput('#' . $fn['ref_id'][0], ['attributes' => ['id' => $fn['fn_id'], 'class' => 'footnote-link']]); $url = Url::fromUserInput('#' . $fn['ref_id'][0], ['attributes' => ['id' => $fn['fn_id'], 'class' => 'footnote-link']]);
$build[] = Link::fromTextAndUrl($footnote_link_text, $url)->toRenderable(); $build['footnote-link'] = Link::fromTextAndUrl($footnote_link_text, $url)->toRenderable();
foreach ($fn['ref_id'] as $ref) { foreach ($fn['ref_id'] as $ref) {
$url = Url::fromUserInput( '#' . $ref, ['attributes' => ['id' => $fn['fn_id'], 'class' => 'footnote-multi']]); $url = Url::fromUserInput( '#' . $ref, ['attributes' => ['id' => $fn['fn_id'], 'class' => 'footnote-multi']]);
@ -96,11 +93,28 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) {
$i++; $i++;
} }
}
if (!empty($fn['text'])) {
// Handle the case where an endnote contains text and a reference.
// It will appear in both lists. But the link text for the endnote should
// always be the title of the note. So make a copy of the biuld arrray
// and change the link text.
$note_build = $build + ['#type' => 'markup', '#markup' => ' <span class="endnote-text">' . $fn['text'] . '</span>'];
$note_build['footnote-link'] = Link::fromTextAndUrl($fn['value'], $url)->toRenderable();
if (!empty($reference_entity_id)) {
$citation_build = $citation_tools->getRenderableReference($reference_entity_id);
$note_build[] = $citation_build;
}
$note_item = [] + $item;
$note_item['#markup'] = render($note_build);
$notes['#items'][] = $note_item;
} }
if (!empty($reference_entity_id)) { if (!empty($reference_entity_id)) {
$citation_build = $citation_tools->getRenderableReference($reference_entity_id); $citation_build = $citation_tools->getRenderableReference($reference_entity_id);
$build[] = $citation_build; $build[] = $citation_build;
$render = render($build); $render = render($build);
$item['#markup'] = $render; $item['#markup'] = $render;
@ -108,13 +122,7 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) {
$references['#items'][] = $item; $references['#items'][] = $item;
} }
else {
$build[] = ['#type' => 'markup', '#markup' => ' ' . $fn['text']];
$render = render($build);
$item['#markup'] = $render;
$notes['#items'][] = $item;
}
} }
$variables['notes'] = $notes; $variables['notes'] = $notes;
@ -125,15 +133,6 @@ function bibcite_footnotes_preprocess_footnote_list(&$variables) {
$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) { function _bibcite_footnotes_reference_array_cmp($a, $b) {
$a1 = (!empty($a['sort']) ? strtolower($a['sort']) : ''); $a1 = (!empty($a['sort']) ? strtolower($a['sort']) : '');
$b1 = (!empty($b['sort']) ? strtolower($b['sort']) : ''); $b1 = (!empty($b['sort']) ? strtolower($b['sort']) : '');
@ -149,7 +148,7 @@ function bibcite_footnotes_preprocess_footnote_link(&$variables) {
$class = 'see-footnote'; $class = 'see-footnote';
// Generate the hover text // Generate the hover text
$citation_tools = new CitationTools(); $citation_tools = new CitationTools();
$citation_entity_id = _bibcite_footnotes_get_reference_entity_id_from_bibcite_footnote($fn['text']); $citation_entity_id = $fn['reference'];
$citation_data = $citation_tools->getRenderableReference($citation_entity_id); $citation_data = $citation_tools->getRenderableReference($citation_entity_id);
// Citation contains a page reference, so construct parenthetical footnote. // Citation contains a page reference, so construct parenthetical footnote.

17
js/plugins/reference_footnotes/dialogs/footnotes.js

@ -12,13 +12,13 @@
elements: elements:
[ [
{ {
id: 'reference_footnote', id: 'reference',
type: 'select', type: 'select',
items: editor.config.referenceFootnotes_list, items: editor.config.referenceFootnotes_list,
label: Drupal.t('Reference Footnote item:'), label: Drupal.t('Reference Footnote item:'),
setup: function (element) { setup: function (element) {
if (isEdit) if (isEdit)
this.setValue(element.getText()); this.setValue(element.getAttribute('reference'));
} }
}, },
{ {
@ -27,10 +27,10 @@
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) {
var text = element.getText(); var text = element.getText();
if (!text.match(/\[bibcite_reference:(\d*)\]/)) {
this.setValue(text); this.setValue(text);
}
} }
} }
}, },
@ -68,11 +68,10 @@
this.setupContent( this.realObj ); this.setupContent( this.realObj );
}, },
onOk : function() { onOk : function() {
var referenceNote = this.getValueOf('info', 'reference_footnote'); var referenceNote = this.getValueOf('info', 'reference');
var textNote = this.getValueOf('info', 'footnote'); var textNote = this.getValueOf('info', 'footnote');
var value = textNote ? textNote : referenceNote;
var page = this.getValueOf('info', 'page'); var page = this.getValueOf('info', 'page');
CKEDITOR.plugins.reference_footnotes.createFootnote( editor, this.realObj, value, this.getValueOf('info', 'value'), page); CKEDITOR.plugins.reference_footnotes.createFootnote( editor, this.realObj, textNote, this.getValueOf('info', 'value'), referenceNote, page);
delete this.fakeObj; delete this.fakeObj;
delete this.realObj; delete this.realObj;
} }

9
js/plugins/reference_footnotes/plugin.js

@ -29,10 +29,10 @@
init: function( editor ) init: function( editor )
{ {
editor.addCommand('createreferencefootnotes', new CKEDITOR.dialogCommand('createreferencefootnotes', { editor.addCommand('createreferencefootnotes', new CKEDITOR.dialogCommand('createreferencefootnotes', {
allowedContent: 'fn[value][page]' allowedContent: 'fn[value]'
})); }));
editor.addCommand('editreferencefootnotes', new CKEDITOR.dialogCommand('editreferencefootnotes', { editor.addCommand('editreferencefootnotes', new CKEDITOR.dialogCommand('editreferencefootnotes', {
allowedContent: 'fn[value][page]' allowedContent: 'fn[value]'
})); }));
// Drupal Wysiwyg requirement: The first argument to editor.ui.addButton() // Drupal Wysiwyg requirement: The first argument to editor.ui.addButton()
@ -94,7 +94,7 @@
})(); })();
CKEDITOR.plugins.reference_footnotes = { CKEDITOR.plugins.reference_footnotes = {
createFootnote: function( editor, origElement, text, value, page) { createFootnote: function( editor, origElement, text, value, reference, page) {
if (!origElement) { if (!origElement) {
var realElement = CKEDITOR.dom.element.createFromHtml('<fn></fn>'); var realElement = CKEDITOR.dom.element.createFromHtml('<fn></fn>');
} }
@ -109,6 +109,9 @@ CKEDITOR.plugins.reference_footnotes = {
if (page && page.length > 0) { if (page && page.length > 0) {
realElement.setAttribute('page', page); realElement.setAttribute('page', page);
} }
if (reference && reference.length > 0) {
realElement.setAttribute('reference', reference);
}
var fakeElement = editor.createFakeElement( realElement , 'cke_reference_footnote', 'hiddenfield', false ); var fakeElement = editor.createFakeElement( realElement , 'cke_reference_footnote', 'hiddenfield', false );
editor.insertElement(fakeElement); editor.insertElement(fakeElement);

2
src/Plugin/CKEditorPlugin/ReferenceFootnotes.php

@ -89,7 +89,7 @@ class ReferenceFootnotes extends CKEditorPluginBase {
$render = render($build); $render = render($build);
$output = trim(strip_tags($render)); $output = trim(strip_tags($render));
//$options[] = [$output, $ref_id]; //$options[] = [$output, $ref_id];
$options[] = [$output, "[bibcite_reference:$ref_id]"]; $options[] = [$output, $ref_id];
} }
return ['referenceFootnotes_list' => $options]; return ['referenceFootnotes_list' => $options];

8
src/Plugin/Filter/ReferenceFootnotesFilter.php

@ -163,7 +163,7 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
// (fixes http://drupal.org/node/194558). // (fixes http://drupal.org/node/194558).
$randstr = $this->randstr(); $randstr = $this->randstr();
$value = $this->extractAtribute($matches, 'value'); $value = $this->extractAttribute($matches, 'value');
if ($value) { if ($value) {
// A value label was found. If it is numeric, record it in $n so further // A value label was found. If it is numeric, record it in $n so further
@ -191,7 +191,8 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
// attribute. // attribute.
$value_id = preg_replace('|[^\w\-]|', '', $value); $value_id = preg_replace('|[^\w\-]|', '', $value);
$page = $this->extractAtribute($matches, 'page'); $page = $this->extractAttribute($matches, 'page');
$reference = $this->extractAttribute($matches, 'reference');
// Create a sanitized version of $text that is suitable for using as HTML // Create a sanitized version of $text that is suitable for using as HTML
// attribute value. (In particular, as the title attribute to the footnote // attribute value. (In particular, as the title attribute to the footnote
@ -211,6 +212,7 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
'text' => $matches[2], 'text' => $matches[2],
'text_clean' => $text_clean, 'text_clean' => $text_clean,
'page' => $page, 'page' => $page,
'reference' => $reference,
'fn_id' => 'footnote' . $value_id . '_' . $randstr, 'fn_id' => 'footnote' . $value_id . '_' . $randstr,
'ref_id' => 'footnoteref' . $value_id . '_' . $randstr, 'ref_id' => 'footnoteref' . $value_id . '_' . $randstr,
]; ];
@ -286,7 +288,7 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
* *
* @return string * @return string
*/ */
protected function extractAtribute($matches, $attribute): string { protected function extractAttribute($matches, $attribute): string {
$value = ''; $value = '';
// Did the pattern match anything in the <fn> tag? // Did the pattern match anything in the <fn> tag?
if ($matches[1]) { if ($matches[1]) {

Loading…
Cancel
Save