Skip to content

Commit d048358

Browse files
committed
fix shortcut, empty terms and multiple calls
1 parent a901a48 commit d048358

File tree

9 files changed

+28
-16
lines changed

9 files changed

+28
-16
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Release Notes for Glossary for Craft CMS
22

3+
## 1.0.6 - 2022-12-16
4+
5+
### Fixed
6+
7+
- Missing definitions when applying the glossary multiple times. (#5)
8+
- Remove empty strings from terms breaking the frontend. (#7)
9+
- Fixed shortcut to save terms and glossaries. (#8)
10+
311
## 1.0.5 - 2021-09-28
412

513
### Fixed
@@ -10,7 +18,7 @@
1018

1119
### Added
1220

13-
- Add the term element to the term template variables. So you can now acces the hole term element within the template.
21+
- Add the term element to the term template variables. So you can now access the hole term element within the template.
1422

1523
### Deprecated
1624

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A glossary plugin for Craft CMS.
88

99
## Requirements
1010

11-
* Craft CMS >= 3.5.0
11+
* Craft CMS >= 3.5.0
1212

1313
## Installation
1414

@@ -17,7 +17,7 @@ Open your terminal and go to your Craft project:
1717
``` shell
1818
cd /path/to/project
1919
composer require codemonauts/craft-glossary
20-
./craft install/plugin glossary
20+
./craft plugin/install glossary
2121
```
2222

2323
## Usage

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "codemonauts/craft-glossary",
33
"description": "Add glossaries with tooltips to Craft CMS.",
4-
"version": "1.0.5",
4+
"version": "1.0.6",
55
"type": "craft-plugin",
66
"keywords": [
77
"craft",

src/controllers/GlossaryController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function actionEdit(int $glossaryId = null, GlossaryElement $glossary = n
4343
// Set variables
4444
$variables['glossary'] = $glossary;
4545
$variables['title'] = $glossary->id ? 'Edit glossary' : 'Create glossary';
46-
$variables['continueEditingUrl'] = 'glossary/glossary/{glossaryId}';
46+
$variables['continueEditingUrl'] = 'glossary/glossary/{id}';
4747
$variables['isNew'] = !$glossary->id;
4848
$variables['fieldLayout'] = $glossary->getFieldLayout();
4949

src/controllers/TermController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function actionEdit(int $termId = null, TermElement $term = null): Respon
5151
// Set variables
5252
$variables['term'] = $term;
5353
$variables['title'] = $term->id ? 'Edit term' : 'Create term';
54-
$variables['continueEditingUrl'] = 'glossary/term/{termId}';
54+
$variables['continueEditingUrl'] = 'glossary/term/{id}';
5555
$variables['isNew'] = !$term->id;
5656

5757
// Get all glossaries and prepare for switcher

src/elements/Term.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ public function defineRules(): array
294294
$rules = parent::defineRules();
295295

296296
$rules[] = [['term'], 'required'];
297+
$rules[] = [['term', 'synonyms'], 'trim'];
297298
$rules[] = [['glossaryId'], 'integer'];
298299
$rules[] = [['caseSensitive', 'matchSubstring'], 'boolean'];
299300

src/services/Terms.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use codemonauts\glossary\elements\Glossary;
66
use codemonauts\glossary\elements\Term;
77
use Craft;
8+
use craft\helpers\ArrayHelper;
89
use craft\helpers\Html;
910
use Exception;
1011
use Twig\Error\SyntaxError;
@@ -13,7 +14,8 @@
1314

1415
class Terms extends Component
1516
{
16-
protected $renderedTerms = '';
17+
protected string $renderedTerms = '';
18+
protected array $usedTerms = [];
1719

1820
/**
1921
* Returns all terms to search for.
@@ -28,12 +30,12 @@ public function parseTerms(Term $term): array
2830
$term->term,
2931
];
3032

31-
if ($term->synonyms !== '') {
33+
if (!empty($term->synonyms)) {
3234
$synonyms = explode(',', $term->synonyms);
3335
$terms = array_merge($terms, $synonyms);
3436
}
3537

36-
return $terms;
38+
return ArrayHelper::filterEmptyStringsFromArray($terms);
3739
}
3840

3941
/**
@@ -50,10 +52,9 @@ public function renderTerms(string $text, Glossary $glossary): string
5052
$originalText = $text;
5153

5254
try {
53-
$termTemplate = $glossary->termTemplate !== '' ? $glossary->termTemplate : '<span>{{ text }}</span>';
55+
$termTemplate = !empty($glossary->termTemplate) ? $glossary->termTemplate : '<span>{{ text }}</span>';
5456
$replacements = [];
5557
$terms = Term::find()->glossary($glossary)->all();
56-
$usedTerms = [];
5758

5859
foreach ($terms as $term) {
5960
$template = Html::modifyTagAttributes($termTemplate, [
@@ -74,7 +75,7 @@ public function renderTerms(string $text, Glossary $glossary): string
7475
if (!$term->caseSensitive) {
7576
$pattern .= 'i';
7677
}
77-
$text = s($text)->replaceMatches($pattern, function ($matches) use ($term, $template, &$replacements, &$index, $view, &$usedTerms, $glossary) {
78+
$text = s($text)->replaceMatches($pattern, function ($matches) use ($term, $template, &$replacements, &$index, $view, $glossary) {
7879
try {
7980
$replacement = trim($view->renderString($template, [
8081
'term' => $term,
@@ -99,7 +100,7 @@ public function renderTerms(string $text, Glossary $glossary): string
99100
$variables['term'] = $term;
100101

101102
try {
102-
$usedTerms[$term->id] = $view->renderTemplate($glossary->tooltipTemplate, $variables, 'site');
103+
$this->usedTerms[$term->id] = $view->renderTemplate($glossary->tooltipTemplate, $variables, 'site');
103104
} catch (SyntaxError $e) {
104105
Craft::error($e->getMessage(), 'glossary');
105106
}
@@ -114,7 +115,7 @@ public function renderTerms(string $text, Glossary $glossary): string
114115
}
115116

116117
$renderedTerms = '';
117-
foreach ($usedTerms as $id => $usedTerm) {
118+
foreach ($this->usedTerms as $id => $usedTerm) {
118119
$renderedTerms .= Html::tag('div', $usedTerm, [
119120
'id' => 'term-' . $id,
120121
]);

src/templates/glossary/_edit.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{% import "_includes/forms" as forms %}
33
{% set selectedSubnavItem = 'glossaries' %}
44
{% set fullPageForm = true %}
5+
{% set saveShortcutRedirect = continueEditingUrl %}
56

67
{% block actionButton %}
78
<div class="btngroup">

src/templates/term/_edit.twig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
{% import "_includes/forms" as forms %}
33
{% set element = term %}
44
{% set selectedSubnavItem = 'terms' %}
5-
{% set fullPageForm = false %}
6-
{% set showStatusToggles = true %}
5+
{% set fullPageForm = true %}
6+
{% set canUpdateSource = true %}
7+
{% set saveShortcutRedirect = continueEditingUrl %}
78

89
{% block actionButton %}
910
<div class="btngroup">

0 commit comments

Comments
 (0)