Browse Source

Put more logic into Filter from theme preprocess function.

ibidem
Alexander O'Neill 6 years ago
parent
commit
69700fa2f7
  1. 41
      bibcite_footnotes.module
  2. 16
      src/Plugin/Filter/ReferenceFootnotesFilter.php

41
bibcite_footnotes.module

@ -10,6 +10,7 @@ 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; use Drupal\bibcite_footnotes\CitationTools;
use Drupal\bibcite_footnotes\Plugin\Filter\ReferenceFootnotesFilter;
/** /**
* Implements hook_theme(). * Implements hook_theme().
@ -60,20 +61,20 @@ function bibcite_footnotes_help($route_name, RouteMatchInterface $route_match) {
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/ */
function bibcite_footnotes_preprocess_bibcite_footnote_list(&$variables) { function bibcite_footnotes_preprocess_bibcite_footnote_list(&$variables) {
$config = \Drupal::config('filter.format.rich_text_references');//filters.filter_reference_footnotes.settings'); $config = $variables['config'];
$footnotes = $variables['footnotes']; $footnotes = $variables['notes'];
$notes = [ $notes = [
'#theme' => 'item_list', '#theme' => 'item_list',
'#list_type' => 'ul', '#list_type' => 'ul',
'#title' => $config->get('filters.filter_reference_footnotes.settings.notes_section_label'), '#title' => $variables['note_type'] == ReferenceFootnotesFilter::ENDNOTE ? $config['notes_section_label'] : $config['references_section_label'],
'#attributes' => ['class' => 'footnotes'], '#attributes' => ['class' => 'footnotes'],
'#wrapper_attributes' => ['class' => 'container'], '#wrapper_attributes' => ['class' => 'container'],
]; ];
$notes['#attached']['library'][] = 'bibcite_footnotes/reference_footnote'; $notes['#attached']['library'][] = 'bibcite_footnotes/reference_footnote';
$dont_show_backlink_text = $config->get('filters.filter_reference_footnotes.settings.reference_dont_show_backlink_text'); $dont_show_backlink_text = $config['dont_show_backlink_text'];
$sort_references_by = $config->get('filters.filter_reference_footnotes.settings.reference_sort_by'); $sort_references_by = $config['reference_sort_by'];
$citation_tools = new CitationTools(); $citation_tools = new CitationTools();
foreach ($footnotes as $fn) { foreach ($footnotes as $fn) {
$item = [ $item = [
@ -111,21 +112,16 @@ function bibcite_footnotes_preprocess_bibcite_footnote_list(&$variables) {
$override_page_in_citation = FALSE; $override_page_in_citation = FALSE;
} }
if (!empty($fn['text'])) { if (!empty($fn['text']) && $variables['note_type'] == ReferenceFootnotesFilter::ENDNOTE) {
// Handle the case where an endnote contains text and a reference. // 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 // 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 // always be the title of the note. So make a copy of the biuld arrray
// and change the link text. // and change the link text.
$note_build = $build + ['#type' => 'markup', '#markup' => ' <span class="endnote-text">' . $fn['text'] . '</span>']; $build['#type'] = 'markup';
$note_build['footnote-link'] = Link::fromTextAndUrl($fn['value'], $url)->toRenderable(); $build['#markup'] = ' <span class="endnote-text">' . $fn['text'] . '</span>';
if (!empty($reference_entity_id)) { $build['footnote-link'] = Link::fromTextAndUrl($fn['value'], $url)->toRenderable();
$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)) {
@ -135,20 +131,17 @@ function bibcite_footnotes_preprocess_bibcite_footnote_list(&$variables) {
} }
$build[] = $citation_build; $build[] = $citation_build;
$render = render($build); $render = render($build);
$item['#markup'] = $render;
$item['sort'] = trim(strip_tags(render($citation_build)));
$references['#items'][] = $item;
$item['sort'] = trim(strip_tags(render($citation_build)));
} }
$item['#markup'] = $render;
$notes['#items'][] = $item;
} }
$variables['notes'] = $notes; if ($sort_references_by == 'alphabetical' && $variables['note_type'] == ReferenceFootnotesFilter::REFERENCE) {
usort($notes['#items'], '_bibcite_footnotes_reference_array_cmp');
if ($sort_references_by == 'alphabetical') {
usort($references['#items'], '_bibcite_footnotes_reference_array_cmp');
} }
$variables['references'] = $references; $variables['notes'] = $notes;
} }
function _bibcite_footnotes_reference_array_cmp($a, $b) { function _bibcite_footnotes_reference_array_cmp($a, $b) {

16
src/Plugin/Filter/ReferenceFootnotesFilter.php

@ -10,7 +10,7 @@ use Drupal\footnotes\Plugin\Filter\FootnotesFilter;
* Provides a base filter for Reference Footnotes filter. * Provides a base filter for Reference Footnotes filter.
* *
* @Filter( * @Filter(
* id = "filter_reference_footnotes", * id = "filter_reference_footnotes",footnote_list
* module = "bibcite_footnotes", * module = "bibcite_footnotes",
* title = @Translation("Reference Footnotes filter"), * title = @Translation("Reference Footnotes filter"),
* description = @Translation("You can insert footnotes directly into texts."), * description = @Translation("You can insert footnotes directly into texts."),
@ -155,11 +155,19 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
// themed as a list // themed as a list
$markup = [ $markup = [
'#theme' => 'bibcite_footnote_list', '#theme' => 'bibcite_footnote_list',
'#footnotes' => $notes, '#notes' => $notes,
'#note_type' => self::ENDNOTE, '#note_type' => self::ENDNOTE,
'#config' => $this->settings, '#config' => $this->settings,
]; ];
$str = \Drupal::service('renderer')->render($markup, FALSE); $str = \Drupal::service('renderer')->render($markup, FALSE);
$markup = [
'#theme' => 'bibcite_footnote_list',
'#notes' => $references,
'#note_type' => self::REFERENCE,
'#config' => $this->settings,
];
$str .= \Drupal::service('renderer')->render($markup, FALSE);
} }
// Reset the static variables so they can be used again next time. // Reset the static variables so they can be used again next time.
$n = 0; $n = 0;
@ -336,7 +344,6 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
$prev_reference_id = $fn['reference']; // Could be empty, that's OK. $prev_reference_id = $fn['reference']; // Could be empty, that's OK.
} }
ksm($footnotes);
} }
protected function extractNotesByType($type, $footnotes) { protected function extractNotesByType($type, $footnotes) {
@ -344,7 +351,7 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
foreach ($footnotes as $fn) { foreach ($footnotes as $fn) {
switch ($type) { switch ($type) {
case self::ENDNOTE: case self::ENDNOTE:
if (!empty(fn['text'])) { if (!empty($fn['text'])) {
$notes[] = $fn; $notes[] = $fn;
} }
break; break;
@ -354,7 +361,6 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
} }
break; break;
} }
ksm($fn);
} }
return $notes; return $notes;
} }

Loading…
Cancel
Save