diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ba5cb4..a837e2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Release Notes for Glossary for Craft CMS +## 1.0.6 - 2022-12-16 + +### Fixed + +- Missing definitions when applying the glossary multiple times. (#5) +- Remove empty strings from terms breaking the frontend. (#7) +- Fixed shortcut to save terms and glossaries. (#8) + ## 1.0.5 - 2021-09-28 ### Fixed @@ -10,7 +18,7 @@ ### Added -- Add the term element to the term template variables. So you can now acces the hole term element within the template. +- Add the term element to the term template variables. So you can now access the hole term element within the template. ### Deprecated diff --git a/README.md b/README.md index 230a35c..3d89a57 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A glossary plugin for Craft CMS. ## Requirements - * Craft CMS >= 3.5.0 +* Craft CMS >= 3.5.0 ## Installation @@ -17,7 +17,7 @@ Open your terminal and go to your Craft project: ``` shell cd /path/to/project composer require codemonauts/craft-glossary -./craft install/plugin glossary +./craft plugin/install glossary ``` ## Usage diff --git a/composer.json b/composer.json index 33f7442..47431ee 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "codemonauts/craft-glossary", "description": "Add glossaries with tooltips to Craft CMS.", - "version": "1.0.5", + "version": "1.0.6", "type": "craft-plugin", "keywords": [ "craft", diff --git a/src/controllers/GlossaryController.php b/src/controllers/GlossaryController.php index 9c0b4e7..e59f046 100644 --- a/src/controllers/GlossaryController.php +++ b/src/controllers/GlossaryController.php @@ -43,7 +43,7 @@ public function actionEdit(int $glossaryId = null, GlossaryElement $glossary = n // Set variables $variables['glossary'] = $glossary; $variables['title'] = $glossary->id ? 'Edit glossary' : 'Create glossary'; - $variables['continueEditingUrl'] = 'glossary/glossary/{glossaryId}'; + $variables['continueEditingUrl'] = 'glossary/glossary/{id}'; $variables['isNew'] = !$glossary->id; $variables['fieldLayout'] = $glossary->getFieldLayout(); diff --git a/src/controllers/TermController.php b/src/controllers/TermController.php index 35bfc3c..8e0e473 100644 --- a/src/controllers/TermController.php +++ b/src/controllers/TermController.php @@ -51,7 +51,7 @@ public function actionEdit(int $termId = null, TermElement $term = null): Respon // Set variables $variables['term'] = $term; $variables['title'] = $term->id ? 'Edit term' : 'Create term'; - $variables['continueEditingUrl'] = 'glossary/term/{termId}'; + $variables['continueEditingUrl'] = 'glossary/term/{id}'; $variables['isNew'] = !$term->id; // Get all glossaries and prepare for switcher diff --git a/src/elements/Term.php b/src/elements/Term.php index 7cced04..ac3820f 100644 --- a/src/elements/Term.php +++ b/src/elements/Term.php @@ -294,6 +294,7 @@ public function defineRules(): array $rules = parent::defineRules(); $rules[] = [['term'], 'required']; + $rules[] = [['term', 'synonyms'], 'trim']; $rules[] = [['glossaryId'], 'integer']; $rules[] = [['caseSensitive', 'matchSubstring'], 'boolean']; diff --git a/src/services/Terms.php b/src/services/Terms.php index 5261101..8940981 100644 --- a/src/services/Terms.php +++ b/src/services/Terms.php @@ -5,6 +5,7 @@ use codemonauts\glossary\elements\Glossary; use codemonauts\glossary\elements\Term; use Craft; +use craft\helpers\ArrayHelper; use craft\helpers\Html; use Exception; use Twig\Error\SyntaxError; @@ -13,7 +14,8 @@ class Terms extends Component { - protected $renderedTerms = ''; + protected string $renderedTerms = ''; + protected array $usedTerms = []; /** * Returns all terms to search for. @@ -28,12 +30,12 @@ public function parseTerms(Term $term): array $term->term, ]; - if ($term->synonyms !== '') { + if (!empty($term->synonyms)) { $synonyms = explode(',', $term->synonyms); $terms = array_merge($terms, $synonyms); } - return $terms; + return ArrayHelper::filterEmptyStringsFromArray($terms); } /** @@ -50,10 +52,9 @@ public function renderTerms(string $text, Glossary $glossary): string $originalText = $text; try { - $termTemplate = $glossary->termTemplate !== '' ? $glossary->termTemplate : '{{ text }}'; + $termTemplate = !empty($glossary->termTemplate) ? $glossary->termTemplate : '{{ text }}'; $replacements = []; $terms = Term::find()->glossary($glossary)->all(); - $usedTerms = []; foreach ($terms as $term) { $template = Html::modifyTagAttributes($termTemplate, [ @@ -74,7 +75,7 @@ public function renderTerms(string $text, Glossary $glossary): string if (!$term->caseSensitive) { $pattern .= 'i'; } - $text = s($text)->replaceMatches($pattern, function ($matches) use ($term, $template, &$replacements, &$index, $view, &$usedTerms, $glossary) { + $text = s($text)->replaceMatches($pattern, function ($matches) use ($term, $template, &$replacements, &$index, $view, $glossary) { try { $replacement = trim($view->renderString($template, [ 'term' => $term, @@ -99,7 +100,7 @@ public function renderTerms(string $text, Glossary $glossary): string $variables['term'] = $term; try { - $usedTerms[$term->id] = $view->renderTemplate($glossary->tooltipTemplate, $variables, 'site'); + $this->usedTerms[$term->id] = $view->renderTemplate($glossary->tooltipTemplate, $variables, 'site'); } catch (SyntaxError $e) { Craft::error($e->getMessage(), 'glossary'); } @@ -114,7 +115,7 @@ public function renderTerms(string $text, Glossary $glossary): string } $renderedTerms = ''; - foreach ($usedTerms as $id => $usedTerm) { + foreach ($this->usedTerms as $id => $usedTerm) { $renderedTerms .= Html::tag('div', $usedTerm, [ 'id' => 'term-' . $id, ]); diff --git a/src/templates/glossary/_edit.twig b/src/templates/glossary/_edit.twig index e71faff..38b9ec6 100644 --- a/src/templates/glossary/_edit.twig +++ b/src/templates/glossary/_edit.twig @@ -2,6 +2,7 @@ {% import "_includes/forms" as forms %} {% set selectedSubnavItem = 'glossaries' %} {% set fullPageForm = true %} +{% set saveShortcutRedirect = continueEditingUrl %} {% block actionButton %}
diff --git a/src/templates/term/_edit.twig b/src/templates/term/_edit.twig index f5f9b06..ce43703 100644 --- a/src/templates/term/_edit.twig +++ b/src/templates/term/_edit.twig @@ -2,8 +2,9 @@ {% import "_includes/forms" as forms %} {% set element = term %} {% set selectedSubnavItem = 'terms' %} -{% set fullPageForm = false %} -{% set showStatusToggles = true %} +{% set fullPageForm = true %} +{% set canUpdateSource = true %} +{% set saveShortcutRedirect = continueEditingUrl %} {% block actionButton %}