Skip to content
Snippets Groups Projects
Commit e577b77d authored by Tim Diels's avatar Tim Diels Committed by Stefan Auditor
Browse files

Issue #3005964 by tim-diels, awolfey: Move Glossify tip sanitizing to theme function

parent e046d016
No related branches found
No related tags found
1 merge request!9Issue #3005964: Move Glossify tip sanitizing to theme function
...@@ -14,6 +14,7 @@ function glossify_theme() { ...@@ -14,6 +14,7 @@ function glossify_theme() {
'variables' => [ 'variables' => [
'word' => NULL, 'word' => NULL,
'tip' => NULL, 'tip' => NULL,
'tip_raw' => NULL,
'langcode' => NULL, 'langcode' => NULL,
], ],
], ],
...@@ -21,6 +22,7 @@ function glossify_theme() { ...@@ -21,6 +22,7 @@ function glossify_theme() {
'variables' => [ 'variables' => [
'word' => NULL, 'word' => NULL,
'tip' => NULL, 'tip' => NULL,
'tip_raw' => NULL,
'tipurl' => NULL, 'tipurl' => NULL,
], ],
], ],
......
...@@ -148,11 +148,13 @@ abstract class GlossifyBase extends FilterBase { ...@@ -148,11 +148,13 @@ abstract class GlossifyBase extends FilterBase {
} }
else { else {
$tip = ''; $tip = '';
$tip_raw = '';
if ($displaytype == 'links' || $displaytype == 'tooltips_links') { if ($displaytype == 'links' || $displaytype == 'tooltips_links') {
// Insert the matched term instance as link. // Insert the matched term instance as link.
if ($displaytype == 'tooltips_links') { if ($displaytype == 'tooltips_links') {
$tip = $this->sanitizeTip((string) $term->tip, $tooltip_truncate); $tip = $this->sanitizeTip((string) $term->tip, $tooltip_truncate);
$tip_raw = $this->sanitizeRawTip((string) $term->tip, $tooltip_truncate);
} }
if (\Drupal::hasContainer()) { if (\Drupal::hasContainer()) {
$tipurl = Url::fromUri('internal:' . str_replace('[id]', $term->id, $urlpattern)); $tipurl = Url::fromUri('internal:' . str_replace('[id]', $term->id, $urlpattern));
...@@ -164,6 +166,7 @@ abstract class GlossifyBase extends FilterBase { ...@@ -164,6 +166,7 @@ abstract class GlossifyBase extends FilterBase {
'#theme' => 'glossify_link', '#theme' => 'glossify_link',
'#word' => $term_txt, '#word' => $term_txt,
'#tip' => $tip, '#tip' => $tip,
'#tip_raw' => $tip_raw,
'#tipurl' => $tipurl, '#tipurl' => $tipurl,
]; ];
$word = $this->renderLink($word_link); $word = $this->renderLink($word_link);
...@@ -172,11 +175,13 @@ abstract class GlossifyBase extends FilterBase { ...@@ -172,11 +175,13 @@ abstract class GlossifyBase extends FilterBase {
// Has to be 'tooltips'. // Has to be 'tooltips'.
// Insert the matched term instance as tooltip. // Insert the matched term instance as tooltip.
$tip = $this->sanitizeTip($term->tip, $tooltip_truncate); $tip = $this->sanitizeTip($term->tip, $tooltip_truncate);
$tip_raw = $this->sanitizeRawTip($term->tip, $tooltip_truncate);
$word_tip = [ $word_tip = [
'#theme' => 'glossify_tooltip', '#theme' => 'glossify_tooltip',
'#word' => $term_txt, '#word' => $term_txt,
'#tip' => $tip, '#tip' => $tip,
'#tip_raw' => $tip_raw,
'#langcode' => $langcode, '#langcode' => $langcode,
]; ];
$word = $this->renderTip($word_tip); $word = $this->renderTip($word_tip);
...@@ -245,4 +250,25 @@ abstract class GlossifyBase extends FilterBase { ...@@ -245,4 +250,25 @@ abstract class GlossifyBase extends FilterBase {
return $tip; return $tip;
} }
/**
* Cleanup and truncate raw tip text.
*
* @param string $tip
* The tooltip string.
* @param bool $truncate
* Whether to truncate the tooltip string.
*
* @return string
* The prepared tooltip string.
*/
private function sanitizeRawTip($tip, $truncate = TRUE) {
// Maximise tooltip text length.
if ($truncate) {
// @todo: How to properly truncate the tip if it contains HTML?
// @see https://www.drupal.org/project/drupal/issues/2279655
$tip = Unicode::truncate($tip, 300, TRUE, TRUE);
}
return $tip;
}
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
* Available variables: * Available variables:
* - word: the text where the tooltips applies to. * - word: the text where the tooltips applies to.
* - tip: the tooltip text. * - tip: the tooltip text.
* - tip_raw: the tooltip text in its raw format.
* - tipurl: the tooltip link. * - tipurl: the tooltip link.
* *
* @ingroup themeable * @ingroup themeable
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
* Available variables: * Available variables:
* - word: the text where the tooltips applies to. * - word: the text where the tooltips applies to.
* - tip: the tooltip text. * - tip: the tooltip text.
* - tip_raw: the tooltip text in its raw format.
* - langcode: the tooltip language code. * - langcode: the tooltip language code.
* *
* @ingroup themeable * @ingroup themeable
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment