Skip to content

Commit

Permalink
fix shortcut, empty terms and multiple calls
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed Dec 16, 2022
1 parent a901a48 commit d048358
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 16 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A glossary plugin for Craft CMS.

## Requirements

* Craft CMS >= 3.5.0
* Craft CMS >= 3.5.0

## Installation

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/GlossaryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/TermController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/elements/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down
17 changes: 9 additions & 8 deletions src/services/Terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +14,8 @@

class Terms extends Component
{
protected $renderedTerms = '';
protected string $renderedTerms = '';
protected array $usedTerms = [];

/**
* Returns all terms to search for.
Expand All @@ -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);
}

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

try {
$termTemplate = $glossary->termTemplate !== '' ? $glossary->termTemplate : '<span>{{ text }}</span>';
$termTemplate = !empty($glossary->termTemplate) ? $glossary->termTemplate : '<span>{{ text }}</span>';
$replacements = [];
$terms = Term::find()->glossary($glossary)->all();
$usedTerms = [];

foreach ($terms as $term) {
$template = Html::modifyTagAttributes($termTemplate, [
Expand All @@ -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,
Expand All @@ -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');
}
Expand All @@ -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,
]);
Expand Down
1 change: 1 addition & 0 deletions src/templates/glossary/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% import "_includes/forms" as forms %}
{% set selectedSubnavItem = 'glossaries' %}
{% set fullPageForm = true %}
{% set saveShortcutRedirect = continueEditingUrl %}

{% block actionButton %}
<div class="btngroup">
Expand Down
5 changes: 3 additions & 2 deletions src/templates/term/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
<div class="btngroup">
Expand Down

0 comments on commit d048358

Please sign in to comment.