|
|
@ -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'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|