From 67e3f5710f8c81cb7e3327b7eea2e80a284fdd80 Mon Sep 17 00:00:00 2001 From: Rene Merino Date: Fri, 10 May 2024 23:16:30 -0400 Subject: [PATCH 1/2] Reset Plugin `userGroup` settings if the selected group was deleted --- src/LockedEntries.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/LockedEntries.php b/src/LockedEntries.php index b918f6e..a33d5c6 100644 --- a/src/LockedEntries.php +++ b/src/LockedEntries.php @@ -8,15 +8,17 @@ use craft\base\Element; use craft\base\Model; use craft\base\Plugin; -use craft\elements\db\ElementQuery; use craft\elements\Entry; +use craft\elements\db\ElementQuery; use craft\events\AuthorizationCheckEvent; use craft\events\CancelableEvent; use craft\events\DefineHtmlEvent; use craft\events\ModelEvent; +use craft\events\UserGroupEvent; use craft\helpers\Cp; use craft\helpers\Html; use craft\services\Elements; +use craft\services\UserGroups; use yii\base\Event; /** @@ -160,9 +162,7 @@ function(AuthorizationCheckEvent $event) { } ); - /** - * Modify query to exclude entries that are 'locked' - */ + // Modify query to exclude entries that are 'locked' Event::on( ElementQuery::class, ElementQuery::EVENT_BEFORE_PREPARE, @@ -186,6 +186,25 @@ function(CancelableEvent $event) { } } ); + + // Reset Plugin `userGroup` settings if the selected group was deleted + Event::on( + UserGroups::class, + UserGroups::EVENT_BEFORE_APPLY_GROUP_DELETE, + function (UserGroupEvent $e) { + if ($e->userGroup->id == (int)$this->getSettings()->userGroup) { + $settings = array_merge( + $this->getSettings()->toArray(), + ['userGroup' => ''] + ); + + // Emmit message if saved successfully + if (Craft::$app->getPlugins()->savePluginSettings($this, $settings)) { + Craft::$app->getSession()->setSuccess('Locked Entries plugin settings updated!', $settings); + } + } + } + ); } protected function createSettingsModel(): ?Model From cb12b9121fff4b58f9c94cbec6cb9021c2d0e854 Mon Sep 17 00:00:00 2001 From: Rene Merino Date: Thu, 3 Oct 2024 21:21:29 -0400 Subject: [PATCH 2/2] Support Craft5 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 286ea14..a37bd6e 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ }, "require": { "php": ">=8.0.2", - "craftcms/cms": "^4.0.0" + "craftcms/cms": "^4.0.0|^5.0.0" }, "require-dev": { "craftcms/ecs": "dev-main",