diff --git a/footnotes.services.yml b/footnotes.services.yml index 1ff97e980b581b482dc6f33557450800560d3114..6d621d74d59850efac88a1cdd8da4c9b9122482b 100644 --- a/footnotes.services.yml +++ b/footnotes.services.yml @@ -8,4 +8,4 @@ services: footnotes.twig.FallbackSpacelessTwig: class: Drupal\footnotes\Twig\FootnotesSpacelessTwig tags: - - { name: twig.extension } \ No newline at end of file + - { name: twig.extension } diff --git a/src/Plugin/Filter/FootnotesFilter.php b/src/Plugin/Filter/FootnotesFilter.php index 3c308d27a0220799f7c88d6f9bb6009f6e94f0de..178a85f0850e7401c7b211ae0a41edb823beb1c9 100644 --- a/src/Plugin/Filter/FootnotesFilter.php +++ b/src/Plugin/Filter/FootnotesFilter.php @@ -7,7 +7,6 @@ use Drupal\Component\Render\PlainTextOutput; use Drupal\Component\Serialization\Json; use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Html; -use Drupal\Component\Utility\Random; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Render\Markup; @@ -351,17 +350,13 @@ class FootnotesFilter extends FilterBase implements ContainerFactoryPluginInterf } // Build the render array for the footnote. - $random = new Random(); - $random_string = $random->name(12); - $text_id = Crypt::hashBase64($text); - $text_id = str_replace('_', '', $text_id); $footnote = [ 'value' => $value, 'backlink_value' => $value, 'text' => $text instanceof MarkupInterface ? $text : Markup::create($text), 'text_clean' => PlainTextOutput::renderFromHtml((string) $text), - 'fn_id' => 'footnote' . $value . '_' . $text_id . '_' . $random_string, - 'ref_id' => 'footnoteref' . $value . '_' . $text_id . '_' . $random_string, + 'fn_id' => 'footnote' . $value, + 'ref_id' => 'footnoteref' . $value, 'instance' => 1, 'is_auto' => $is_auto, 'is_same_text' => TRUE, @@ -374,14 +369,31 @@ class FootnotesFilter extends FilterBase implements ContainerFactoryPluginInterf } // Record the target text. - $this->storedFootnotes[$key][$footnote['fn_id']] = $footnote; + $fn_id = $footnote['fn_id']; + $counter = 1; + while (isset($this->storedFootnotes[$key][$fn_id])) { + $fn_id = $footnote['fn_id'] . '-' . $counter; + $counter++; + } + $this->storedFootnotes[$key][$fn_id] = $footnote; // If there are multiple stored footnotes, set the backlink value. if (count($this->storedFootnotes[$key]) > 1) { $alphabet = range('a', 'z'); $counter = 0; + $stored_sub_keys = array_keys($this->storedFootnotes[$key]); + $first_sub_key = reset($stored_sub_keys); foreach ($this->storedFootnotes[$key] as &$stored_footnote) { $stored_footnote['backlink_value'] = $stored_footnote['value'] . $alphabet[$counter]; + + // Set reference and footnote ID's to be relative to the first footnote. + // So any subsequent footnotes after a first one like this: + // - #footnote1. + // Would become: + // - #footnote1-1. + // - #footnote1-2. + $stored_footnote['fn_id'] = $this->storedFootnotes[$key][$first_sub_key]['fn_id'] . '-' . $counter; + $stored_footnote['ref_id'] = $this->storedFootnotes[$key][$first_sub_key]['ref_id'] . '-' . $counter; $counter++; }