Skip to content

Commit

Permalink
Craft 4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed May 11, 2022
1 parent a901a48 commit c332c05
Show file tree
Hide file tree
Showing 24 changed files with 565 additions and 788 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Release Notes for Glossary for Craft CMS

## 2.0.0 - 2022-05-04

### Added

- Craft CMS 4 compatibility.

### Changed

- Requires Craft CMS >= 4.0
- Update tippy.js to 6.3.7

## 1.0.5 - 2021-09-28

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion 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 >= 4.0.0

## Installation

Expand Down
9 changes: 3 additions & 6 deletions 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": "2.0.0",
"type": "craft-plugin",
"keywords": [
"craft",
Expand All @@ -23,7 +23,7 @@
"issues": "https://github.com/codemonauts/craft-glossary/issues"
},
"require": {
"craftcms/cms": "^3.5.0"
"craftcms/cms": "^4.0.0"
},
"autoload": {
"psr-4": {
Expand All @@ -33,9 +33,6 @@
"extra": {
"handle": "glossary",
"class": "codemonauts\\glossary\\Glossary",
"name": "Glossary",
"description": "Add glossaries with tooltips to Craft CMS.",
"changelogUrl": "https://github.com/codemonauts/craft-glossary/blob/master/CHANGELOG.md",
"documentationUrl": "https://plugins.codemonauts.com/plugins/glossary/Introduction.html"
"name": "Glossary"
}
}
44 changes: 31 additions & 13 deletions src/Glossary.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace codemonauts\glossary;

use codemonauts\glossary\elements\Term as TermElement;
use codemonauts\glossary\fieldlayoutelements\CaseSensitivityField;
use codemonauts\glossary\fieldlayoutelements\MatchSubstringField;
use codemonauts\glossary\fieldlayoutelements\SynonymsField;
Expand All @@ -14,34 +15,37 @@
use Craft;
use craft\base\Plugin;
use craft\events\DefineFieldLayoutFieldsEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterUserPermissionsEvent;
use craft\models\FieldLayout;
use craft\services\Elements;
use craft\services\Gc;
use craft\services\UserPermissions;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use yii\base\Event;

/**
* Class Glossary
*
* @property Glossaries $glossaries
* @property Terms $terms
*/
class Glossary extends Plugin
{
/**
* @inheritDoc
* @var \codemonauts\glossary\Glossary
*/
public $hasCpSettings = false;
public static Glossary $plugin;

public $hasCpSection = true;
/**
* @inheritDoc
*/
public bool $hasCpSection = true;

/**
* @inheritDoc
*/
public $schemaVersion = '1.0.2';
public string $schemaVersion = '1.0.2';

/**
* @inheritDoc
Expand All @@ -50,12 +54,20 @@ public function init(): void
{
parent::init();

self::$plugin = $this;

// Register services as components
$this->setComponents([
'glossaries' => Glossaries::class,
'terms' => Terms::class,
]);

// Register elements
Event::on(Elements::class, Elements::EVENT_REGISTER_ELEMENT_TYPES, function (RegisterComponentTypesEvent $event) {
$event->types[] = GlossaryElement::class;
$event->types[] = TermElement::class;
});

// Register Twig extension
if (Craft::$app->request->getIsSiteRequest()) {
Craft::$app->view->registerTwigExtension(new GlossaryFilter());
Expand Down Expand Up @@ -93,9 +105,12 @@ private function _cpInit(): void
{
// Register permissions
Event::on(UserPermissions::class, UserPermissions::EVENT_REGISTER_PERMISSIONS, function (RegisterUserPermissionsEvent $event) {
$event->permissions['glossary'] = [
'glossary:glossaryEdit' => ['label' => Craft::t('glossary', 'Edit Glossaries')],
'glossary:termEdit' => ['label' => Craft::t('glossary', 'Edit Terms')],
$event->permissions[] = [
'heading' => Craft::t('glossary', 'Glossary'),
'permissions' => [
'glossary:glossaryEdit' => ['label' => Craft::t('glossary', 'Edit Glossaries')],
'glossary:termEdit' => ['label' => Craft::t('glossary', 'Edit Terms')],
],
];
});

Expand All @@ -104,14 +119,17 @@ private function _cpInit(): void
$event->rules['glossary/glossaries'] = 'glossary/glossary/index';
$event->rules['glossary/glossary/new'] = 'glossary/glossary/edit';
$event->rules['glossary/glossary/<glossaryId:\d+>'] = 'glossary/glossary/edit';

$event->rules['glossary/terms'] = 'glossary/term/index';
$event->rules['glossary/term/new'] = 'glossary/term/edit';
$event->rules['glossary/term/<termId:\d+>'] = 'glossary/term/edit';
$event->rules['glossary/term/new'] = 'glossary/term/create';
$event->rules['glossary/term/<elementId:\d+>'] = 'elements/edit';
});

// Register field layout
Event::on(FieldLayout::class, FieldLayout::EVENT_DEFINE_STANDARD_FIELDS, function (DefineFieldLayoutFieldsEvent $event) {
/** @var FieldLayout $fieldLayout */
Event::on(FieldLayout::class, FieldLayout::EVENT_DEFINE_NATIVE_FIELDS, function (DefineFieldLayoutFieldsEvent $event) {
/**
* @var FieldLayout $fieldLayout
*/
$fieldLayout = $event->sender;

if ($fieldLayout->type === GlossaryElement::class) {
Expand Down
3 changes: 3 additions & 0 deletions src/controllers/GlossaryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace codemonauts\glossary\controllers;

use codemonauts\glossary\elements\Glossary;
use codemonauts\glossary\elements\Glossary as GlossaryElement;
use Craft;
use craft\base\Element;
use craft\helpers\UrlHelper;
use craft\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;
Expand Down
148 changes: 37 additions & 111 deletions src/controllers/TermController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace codemonauts\glossary\controllers;

use codemonauts\glossary\Glossary as GlossaryPlugin;
use codemonauts\glossary\elements\Glossary;
use codemonauts\glossary\elements\Glossary as GlossaryElement;
use codemonauts\glossary\elements\Term;
use codemonauts\glossary\elements\Term as TermElement;
use codemonauts\glossary\resources\GlossarySwitcher;
use Craft;
use craft\base\Element;
use craft\elements\Entry;
use craft\helpers\UrlHelper;
use craft\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;
Expand All @@ -29,126 +33,48 @@ public function actionIndex(): Response
return $this->renderTemplate('glossary/term/index');
}

public function actionEdit(int $termId = null, TermElement $term = null): Response
public function actionCreate(): ?Response
{
// Find or create new term to edit
if ($termId !== null) {
if ($term === null) {
$term = TermElement::findOne(['id' => $termId, 'status' => null]);
if (!$term) {
throw new NotFoundHttpException();
}
}
} else if ($term === null) {
$term = new TermElement();
$term->id = 0;
}

// Register JS to switch glossary
$this->getView()->registerAssetBundle(GlossarySwitcher::class);
$this->getView()->registerJs('new Craft.GlossarySwitcher();');

// Set variables
$variables['term'] = $term;
$variables['title'] = $term->id ? 'Edit term' : 'Create term';
$variables['continueEditingUrl'] = 'glossary/term/{termId}';
$variables['isNew'] = !$term->id;

// Get all glossaries and prepare for switcher
$variables['glossaries'] = [];
$glossaries = GlossaryElement::findAll();
foreach ($glossaries as $glossary) {
$variables['glossaries'][] = [
'label' => Craft::t('site', $glossary->title),
'value' => $glossary->id,
];
}

// Create custom field layout
$fieldLayout = $term->getFieldLayout();
$form = $fieldLayout->createForm($term);
$variables['tabs'] = $form->getTabMenu();
$variables['fieldsHtml'] = $form->render();
$user = Craft::$app->getUser()->getIdentity();

return $this->renderTemplate('glossary/term/_edit', $variables);
}

public function actionSave(): ?Response
{
$this->requirePostRequest();
// Create & populate the draft
$term = Craft::createObject(Term::class);
$term->enabled = true;

$request = Craft::$app->getRequest();
// Glossary
$defaultGlossary = GlossaryPlugin::$plugin->getGlossaries()->getDefaultGlossary();
$term->glossaryId = $defaultGlossary->id;

// Find or create term to save
$termId = $request->getBodyParam('termId');
if ($termId) {
$term = TermElement::findOne(['id' => $termId, 'status' => null]);
} else {
$term = new TermElement();
// Custom fields
foreach ($term->getFieldLayout()->getCustomFields() as $field) {
if (($value = $this->request->getQueryParam($field->handle)) !== null) {
$term->setFieldValue($field->handle, $value);
}
}

// Set element fields
$term->term = $request->getBodyParam('term');
$term->synonyms = $request->getBodyParam('synonyms');
$term->enabled = (bool)$request->getBodyParam('enabled');
$term->glossaryId = $request->getBodyParam('glossaryId');
$term->caseSensitive = $request->getBodyParam('caseSensitive');
$term->matchSubstring = $request->getBodyParam('matchSubstring');

// Set custom fields
$term->setFieldValuesFromRequest('fields');
$term->setScenario(Element::SCENARIO_LIVE);

// Save term
if (Craft::$app->getElements()->saveElement($term)) {
Craft::$app->getSession()->setNotice('Term saved.');

return $this->redirectToPostedUrl($term);
// Save it
$term->setScenario(Element::SCENARIO_ESSENTIALS);
if (!Craft::$app->getDrafts()->saveElementAsDraft($term, $user->getId(), null, null, false)) {
return $this->asModelFailure($term, Craft::t('app', 'Couldn’t create {type}.', [
'type' => Entry::lowerDisplayName(),
]), 'entry');
}

Craft::$app->getSession()->setError('Term not saved.');
$editUrl = $term->getCpEditUrl();

Craft::$app->getUrlManager()->setRouteParams([
'term' => $term,
]);
$response = $this->asModelSuccess($term, Craft::t('app', '{type} created.', [
'type' => Entry::displayName(),
]), 'entry', array_filter([
'cpEditUrl' => $this->request->isCpRequest ? $editUrl : null,
]));

return null;
}
if (!$this->request->getAcceptsJson()) {
$response->redirect(UrlHelper::urlWithParams($editUrl, [
'fresh' => 1,
]));
}

public function actionSwitchGlossary(): Response
{
$this->requirePostRequest();
$this->requireAcceptsJson();

$request = Craft::$app->getRequest();
$view = $this->getView();

// Create new term and set element fields to posted values
$term = new TermElement();
$term->id = $request->getBodyParam('termId');
$term->enabled = (bool)$request->getBodyParam('enabled');
$term->term = $request->getBodyParam('term');
$term->synonyms = $request->getBodyParam('synonyms');
$term->glossaryId = $request->getBodyParam('glossaryId');
$term->caseSensitive = $request->getBodyParam('caseSensitive');
$term->matchSubstring = $request->getBodyParam('matchSubstring');
$term->setFieldValuesFromRequest('fields');

// Get new glossary to switch to
$glossary = GlossaryElement::findOne(['id' => $term->glossaryId]);

// Create new custom field layout based on new glossary
$form = $glossary->getFieldLayout()->createForm($term);
$tabs = $form->getTabMenu();

return $this->asJson([
'tabsHtml' => count($tabs) > 1 ? $view->renderTemplate('_includes/tabs', [
'tabs' => $tabs,
]) : null,
'fieldsHtml' => $form->render(),
'headHtml' => $view->getHeadHtml(),
'bodyHtml' => $view->getBodyHtml(),
]);
return $response;
}

public function actionDelete(): ?Response
Expand All @@ -158,7 +84,7 @@ public function actionDelete(): ?Response
// Get term to delete
$termId = Craft::$app->getRequest()->getRequiredBodyParam('termId');
$term = TermElement::find()
->anyStatus()
->status(null)
->id($termId)
->one();

Expand Down
Loading

0 comments on commit c332c05

Please sign in to comment.