public function FootnotesFilter::process in Footnotes 8.2
Performs the filter processing.
Parameters
string $text: The text string to be filtered.
string $langcode: The language code of the text to be filtered.
Return value
\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.
Overrides FilterInterface::process
See also
\Drupal\filter\FilterProcessResult
File
- src/
Plugin/ Filter/ FootnotesFilter.php, line 75
Class
- FootnotesFilter
- Provides a base filter for Footnotes filter.
Namespace
Drupal\footnotes\Plugin\FilterCode
public function process($text, $langcode) {
// Supporting both [fn] and <fn> now. Thanks to fletchgqc
// http://drupal.org/node/268026.
// Convert all square brackets to angle brackets. This way all further code
// just manipulates angle brackets. (Angle brackets are preferred here for
// the simple reason that square brackets are more tedious to use in
// regexps).
if (is_array($text)) {
implode($text);
}
$text = preg_replace('|\\[fn([^\\]]*)\\]|', '<fn$1>', $text);
$text = preg_replace('|\\[/fn\\]|', '</fn>', $text);
$text = preg_replace('|\\[footnotes([^\\]]*)\\]|', '<footnotes$1>', $text);
// Check that there are an even number of open and closing tags.
// If there is one closing tag missing, append this to the end.
// If there is more disparity, throw a warning and continue.
// A closing tag may sometimes be missing when we are processing a teaser
// and it has been cut in the middle of the footnote.
// See http://drupal.org/node/253326
$foo = [];
$open_tags = preg_match_all("|<fn([^>]*)>|", $text, $foo);
$close_tags = preg_match_all("|</fn>|", $text, $foo);
if ($open_tags == $close_tags + 1) {
$text = $text . '</fn>';
}
elseif ($open_tags > $close_tags + 1) {
trigger_error($this
->t("You have unclosed fn tags. This is invalid and will\n produce unpredictable results."));
}
// Before doing the replacement, the callback function needs to know which
// options to use.
$this
->replaceCallback($this->settings, 'prepare');
$pattern = '|<fn([^>]*)>(.*?)</fn>|s';
$text = preg_replace_callback($pattern, [
$this,
'replaceCallback',
], $text);
// Replace tag <footnotes> with the list of footnotes.
// If tag is not present, by default add the footnotes at the end.
// Thanks to acp on drupal.org for this idea. see
// http://drupal.org/node/87226.
$footer = $this
->replaceCallback(NULL, 'output footer');
$pattern = '|(<footnotes([\\ \\/]*)>)|';
if (preg_match($pattern, $text) > 0) {
$text = preg_replace($pattern, $footer, $text, 1);
}
elseif (!empty($footer)) {
$text .= "\n\n" . $footer;
}
$result = new FilterProcessResult($text);
$result
->setAttachments([
'library' => [
'footnotes/footnotes',
],
]);
return $result;
}