diff --git a/src/services/Terms.php b/src/services/Terms.php index b11026f..3ce127a 100644 --- a/src/services/Terms.php +++ b/src/services/Terms.php @@ -28,7 +28,7 @@ public function parseTerms(Term $term): array $term->term, ]; - if ($term->synonyms != '') { + if ($term->synonyms !== '') { $synonyms = explode(',', $term->synonyms); $terms = array_merge($terms, $synonyms); } @@ -50,18 +50,18 @@ public function renderTerms(string $text, Glossary $glossary): string $originalText = $text; try { + $termTemplate = $glossary->termTemplate !== '' ? $glossary->termTemplate : '{{ text }}'; $replacements = []; $terms = Term::find()->glossary($glossary)->all(); - $globalIndex0 = 0; $usedTerms = []; foreach ($terms as $term) { - $template = Html::modifyTagAttributes($glossary->termTemplate, [ + $template = Html::modifyTagAttributes($termTemplate, [ 'class' => 'glossary', 'data-glossary-term' => 'term-' . $term->id, ]); - $index0 = 0; + $index = 0; $words = $this->parseTerms($term); foreach ($words as $word) { @@ -73,15 +73,11 @@ public function renderTerms(string $text, Glossary $glossary): string if (!$term->caseSensitive) { $pattern .= 'i'; } - $text = s($text)->replaceMatches($pattern, function ($matches) use ($term, $template, &$replacements, &$index0, &$globalIndex0, $view, &$usedTerms, $glossary) { + $text = s($text)->replaceMatches($pattern, function ($matches) use ($term, $template, &$replacements, &$index, $view, &$usedTerms, $glossary) { try { $replacement = trim($view->renderString($template, [ 'term' => $term, 'text' => $matches[0], - 'termIndex0' => $index0, - 'termIndex' => ($index0 + 1), - 'globalIndex0' => $globalIndex0, - 'globalIndex' => ($globalIndex0 + 1), ], 'site')); } catch (SyntaxError $e) { Craft::error($e->getMessage(), 'glossary'); @@ -92,19 +88,12 @@ public function renderTerms(string $text, Glossary $glossary): string return $term; } - $token = $term->uid . '-' . $index0; + $token = $term->uid . '-' . $index++; $replacements[$token] = $replacement; - $index0++; - $globalIndex0++; - $variables = $term->getFieldValues(); - $variables['index0'] = $index0; - $variables['index'] = $index0 + 1; - $variables['globalIndex0'] = $globalIndex0; - $variables['globalIndex'] = $globalIndex0 + 1; try { - $usedTerms[$term->id] = $view->renderTemplate($glossary->contentTemplate, $variables); + $usedTerms[$term->id] = $view->renderTemplate($glossary->tooltipTemplate, $variables); } catch (SyntaxError $e) { Craft::error($e->getMessage(), 'glossary'); } @@ -113,6 +102,7 @@ public function renderTerms(string $text, Glossary $glossary): string }); } } + foreach ($replacements as $token => $replacement) { $text = s($text)->replace('{{%' . $token . '%}}', $replacement); } @@ -123,6 +113,7 @@ public function renderTerms(string $text, Glossary $glossary): string 'id' => 'term-' . $id, ]); } + $this->renderedTerms = Html::tag('div', $renderedTerms, [ 'id' => 'glossary-terms', 'style' => 'display: none;',