For more information about this repository, visit the project page at https://www.drupal.org/project/bibcite_footnotes
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
6.5 KiB
185 lines
6.5 KiB
<?php |
|
|
|
/** |
|
* @file |
|
* Contains bibcite_footnotes.module. |
|
*/ |
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
use Drupal\Core\Routing\RouteMatchInterface; |
|
use Drupal\Core\Url; |
|
use Drupal\Core\Link; |
|
use Drupal\bibcite_footnotes\CitationTools; |
|
|
|
/** |
|
* Implements hook_theme(). |
|
*/ |
|
function bibcite_footnotes_theme() { |
|
return [ |
|
'bibcite_footnote_link' => [ |
|
'render element' => 'fn', |
|
'template' => 'bibcite-footnote-link', |
|
], |
|
'bibcite_footnote_list' => [ |
|
'render element' => 'footnotes', |
|
'path' => drupal_get_path('module', 'bibcite_footnotes') . '/templates', |
|
'template' => 'bibcite-footnote-list', |
|
'variables' => [ |
|
'notes' => [], |
|
'note_type' => [], |
|
'config' => [], |
|
], |
|
], |
|
]; |
|
} |
|
|
|
/**_bibcite_foot |
|
* Implements hook_help(). |
|
*/ |
|
function bibcite_footnotes_help($route_name, RouteMatchInterface $route_match) { |
|
switch ($route_name) { |
|
// Main module help for the bibcite_footnotes module. |
|
case 'help.page.bibcite_footnotes': |
|
$output = ''; |
|
$output .= '<h3>' . t('About') . '</h3>'; |
|
$output .= '<p>' . t('Inline footnote links for BibCite References') . '</p>'; |
|
return $output; |
|
|
|
default: |
|
} |
|
} |
|
|
|
/** |
|
* Implementation of hook_preprocess_bibcite_footnote_list(). |
|
* |
|
* Gatehrs all notes and prints out Notes and References as separate lists. |
|
* |
|
* @param $variables Theme variables. |
|
* |
|
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException |
|
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
|
*/ |
|
function bibcite_footnotes_preprocess_bibcite_footnote_list(&$variables) { |
|
$config = \Drupal::config('filter.format.rich_text_references');//filters.filter_reference_footnotes.settings'); |
|
$footnotes = $variables['footnotes']; |
|
|
|
$notes = [ |
|
'#theme' => 'item_list', |
|
'#list_type' => 'ul', |
|
'#title' => $config->get('filters.filter_reference_footnotes.settings.notes_section_label'), |
|
'#attributes' => ['class' => 'footnotes'], |
|
'#wrapper_attributes' => ['class' => 'container'], |
|
]; |
|
|
|
$notes['#attached']['library'][] = 'bibcite_footnotes/reference_footnote'; |
|
$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'], |
|
'#wrapper_attributes' => [ |
|
'class' => ['footnote'], |
|
], |
|
]; |
|
$build = []; |
|
$reference_entity_id = $fn['reference']; |
|
|
|
$footnote_link_text = ($dont_show_backlink_text && $reference_entity_id) ? '^' : $fn['value']; |
|
if (!is_array($fn['ref_id'])) { |
|
$url = Url::fromUserInput('#' . $fn['ref_id'], ['attributes' => ['id' => $fn['fn_id'], 'class' => 'footnote-link']]); |
|
$link = Link::fromTextAndUrl(($footnote_link_text), $url)->toRenderable(); |
|
$build['footnote-link'] = $link; |
|
$override_page_in_citation = $fn['page'] ? $fn['page'] : FALSE; |
|
} |
|
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; |
|
|
|
$url = Url::fromUserInput('#' . $fn['ref_id'][0], ['attributes' => ['id' => $fn['fn_id'], 'class' => 'footnote-link']]); |
|
$build['footnote-link'] = Link::fromTextAndUrl($footnote_link_text, $url)->toRenderable(); |
|
|
|
foreach ($fn['ref_id'] as $ref) { |
|
$url = Url::fromUserInput( '#' . $ref, ['attributes' => ['id' => $fn['fn_id'], 'class' => 'footnote-multi']]); |
|
$build[] = Link::fromTextAndUrl($abc[$i], $url)->toRenderable(); |
|
|
|
$i++; |
|
} |
|
$override_page_in_citation = FALSE; |
|
} |
|
|
|
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)) { |
|
$citation_build = $citation_tools->getRenderableReference($reference_entity_id); |
|
if ($override_page_in_citation) { |
|
$citation_build['#data']['page'] = $override_page_in_citation; |
|
} |
|
$build[] = $citation_build; |
|
$render = render($build); |
|
$item['#markup'] = $render; |
|
$item['sort'] = trim(strip_tags(render($citation_build))); |
|
$references['#items'][] = $item; |
|
|
|
} |
|
|
|
} |
|
|
|
$variables['notes'] = $notes; |
|
|
|
if ($sort_references_by == 'alphabetical') { |
|
usort($references['#items'], '_bibcite_footnotes_reference_array_cmp'); |
|
} |
|
$variables['references'] = $references; |
|
} |
|
|
|
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_bibcite_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. |
|
|
|
$class = 'see-footnote'; |
|
// Generate the hover text |
|
$citation_tools = new CitationTools(); |
|
$citation_entity_id = $fn['reference']; |
|
$citation_data = $citation_tools->getRenderableReference($citation_entity_id); |
|
|
|
// Citation contains a page reference, so construct parenthetical footnote. |
|
if (!empty($fn['page'])) { |
|
$fn['value'] = "({$fn['value']}, {$fn['page']})"; |
|
$citation_data['#data']['page'] = $fn['page']; |
|
$class .= '-inline'; |
|
} |
|
|
|
$title = trim(strip_tags(render($citation_data))); |
|
$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; |
|
}
|
|
|