From 2cb75e8cf762c4f19c5a3fb56ed2411c8312966c Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Mon, 16 Mar 2026 13:00:15 +0000 Subject: [PATCH 01/21] Merge in latest main since this is an old branch Update settings response for multisite configs --- src/Plugin.php | 9 ++++++--- src/templates/settings.twig | 7 +++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 01a0ab2..a8b6a48 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -10,6 +10,7 @@ use craft\elements\Entry; use craft\events\DefineHtmlEvent; use craft\helpers\UrlHelper; +use craft\helpers\ConfigHelper; use craft\web\Controller; use craft\web\View; use fostercommerce\entrytyperules\assetbundles\entrytyperules\EntryTypeRulesAsset; @@ -65,6 +66,8 @@ function (DefineHtmlEvent $event): void { public function getSettingsResponse(): mixed { + $site = Craft::$app->request->getParam('site'); + $overrides = Craft::$app->getConfig()->getConfigFromFile($this->handle); /** @var Controller $controller */ @@ -73,9 +76,9 @@ public function getSettingsResponse(): mixed 'entry-type-rules/settings', [ 'settings' => $this->getSettings(), - 'overrides' => $overrides, - 'sectionsUrl' => UrlHelper::cpUrl('settings/sections'), - 'entriesUrl' => UrlHelper::cpUrl('entries'), + 'overrides' => ConfigHelper::localizedValue($overrides, $site), + 'sectionsUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('settings/sections', $site)), + 'entriesUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('entries', $site)), ] ); } diff --git a/src/templates/settings.twig b/src/templates/settings.twig index 8a675ae..ed5fc2b 100755 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -29,6 +29,9 @@ {% set configOverride = overrides.sections is defined %} +{# If we are using overrides from a config file then use those values otherwise use the settings #} +{% set configSettings = configOverride ? overrides : settings %} + {% set sections = craft.app.entries.getAllSections() %} {% set groupOptions = [] %} @@ -104,7 +107,7 @@ {% for entryType in section.entryTypes %} {% set entryTypeCount = craft.entries.section(section.handle).type(entryType.handle).count %} - {% set limitValue = settings.sections[section.handle][entryType.handle].limit|default('') %} + {% set limitValue = configSettings.sections[section.handle][entryType.handle].limit|default('') %} {{ entryType.name }} {{ entryType.handle }}
@@ -137,7 +140,7 @@ options: groupOptions, showAllOption: true, allValue: '', - values: settings.sections[section.handle][entryType.handle].userGroups|default('') + values: configSettings.sections[section.handle][entryType.handle].userGroups|default('') }) }} From 090380db41705a512ac620c0499a8294d7e913c0 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Mon, 16 Mar 2026 14:38:11 +0000 Subject: [PATCH 02/21] define a default settings controller and set variables to pass to template to work with multisite --- src/Plugin.php | 59 +++++++++++++++++++------- src/controllers/SettingsController.php | 33 ++++++++++++++ src/templates/settings.twig | 8 +--- 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index a8b6a48..6713fe3 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -9,9 +9,11 @@ use craft\base\Plugin as BasePlugin; use craft\elements\Entry; use craft\events\DefineHtmlEvent; +use craft\events\RegisterUrlRulesEvent; use craft\helpers\UrlHelper; use craft\helpers\ConfigHelper; use craft\web\Controller; +use craft\web\UrlManager; use craft\web\View; use fostercommerce\entrytyperules\assetbundles\entrytyperules\EntryTypeRulesAsset; use fostercommerce\entrytyperules\models\Settings; @@ -26,14 +28,24 @@ */ class Plugin extends BasePlugin { + + public static ?Plugin $plugin; + public bool $hasCpSettings = true; + public static ?Settings $settings = null; + /** * @throws InvalidConfigException */ public function init(): void { parent::init(); + self::$plugin = $this; + + /** @var Settings $settings */ + $settings = self::$plugin->getSettings(); + self::$settings = $settings; $this->setComponents([ 'service' => Service::class, @@ -62,25 +74,42 @@ function (DefineHtmlEvent $event): void { } } ); + + + Event::on( + UrlManager::class, + UrlManager::EVENT_REGISTER_CP_URL_RULES, + function(RegisterUrlRulesEvent $event) { + // Register Control Panel routes + $event->rules = array_merge( + $event->rules, + [ + 'entry-type-rules' => 'entry-type-rules/settings', + // 'entry-type-rules/' => 'entry-type-rules/settings', + ], + ); + } + ); } public function getSettingsResponse(): mixed { - $site = Craft::$app->request->getParam('site'); - - $overrides = Craft::$app->getConfig()->getConfigFromFile($this->handle); - - /** @var Controller $controller */ - $controller = Craft::$app->controller; - return $controller->renderTemplate( - 'entry-type-rules/settings', - [ - 'settings' => $this->getSettings(), - 'overrides' => ConfigHelper::localizedValue($overrides, $site), - 'sectionsUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('settings/sections', $site)), - 'entriesUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('entries', $site)), - ] - ); + // $site = Craft::$app->request->getParam('site'); + + // $overrides = Craft::$app->getConfig()->getConfigFromFile($this->handle); + + // /** @var Controller $controller */ + // $controller = Craft::$app->controller; + // return $controller->renderTemplate( + // 'entry-type-rules/settings', + // [ + // 'settings' => $this->getSettings(), + // 'overrides' => ConfigHelper::localizedValue($overrides, $site), + // 'sectionsUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('settings/sections', $site)), + // 'entriesUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('entries', $site)), + // ] + // ); + return Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('entry-type-rules/settings')); } protected function createSettingsModel(): ?Model diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index af4f0c4..04005bf 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -5,6 +5,9 @@ use Craft; use craft\errors\MissingComponentException; +use craft\helpers\ConfigHelper; +use craft\helpers\Cp; +use craft\helpers\UrlHelper; use craft\web\Controller; use craft\web\Request; use fostercommerce\entrytyperules\models\Settings; @@ -18,6 +21,36 @@ class SettingsController extends Controller { protected array|int|bool $allowAnonymous = []; + + public function actionIndex(): Response + { + $siteHandle = Craft::$app->request->getParam('site'); + // Get the site to edit + $siteHandle = $siteHandle ?? Cp::requestedSite()->handle; + $siteId = Craft::$app->getSites()->getSiteByHandle($siteHandle)->id; + + $variables = []; + + $overrides = Craft::$app->getConfig()->getConfigFromFile('entry-type-rules'); + + $variables = [ + 'settings' => Plugin::$settings, + 'overrides' => ConfigHelper::localizedValue($overrides, $siteHandle), + 'sectionsUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('settings/sections', $siteHandle)), + 'entriesUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('entries', $siteHandle)), + 'siteHandle' => $siteHandle, + 'siteId' => $siteId, + ]; + + /** @var Controller $controller */ + $controller = Craft::$app->controller; + return $controller->renderTemplate( + 'entry-type-rules/settings', + $variables + ); + } + + /** * Handle a request going to our plugin's action URL for saving settings, * e.g.: actions/craft-entry-type-rules/save-settings diff --git a/src/templates/settings.twig b/src/templates/settings.twig index ed5fc2b..ae9981d 100755 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -16,16 +16,12 @@ {% extends "_layouts/cp" %} {% import "_includes/forms" as forms %} + + {% do view.registerAssetBundle("fostercommerce\\entrytyperules\\assetbundles\\entrytyperules\\EntryTypeRulesSettingsAsset") %} {% set title = "Entry Type Rules"|t('entry-type-rules') %} {% set fullPageForm = true %} -{% set crumbs = [ - { - label: 'Settings'|t('entry-type-rules'), - url: url('settings'), - } -] %} {% set configOverride = overrides.sections is defined %} From ec0d4c938c7c4297b14f27caf0a1979ae35d6e73 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Mon, 16 Mar 2026 15:23:40 +0000 Subject: [PATCH 03/21] qa fixes --- src/Plugin.php | 49 +++++++++----------------- src/controllers/SettingsController.php | 11 +++--- 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 6713fe3..7708b25 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -11,8 +11,6 @@ use craft\events\DefineHtmlEvent; use craft\events\RegisterUrlRulesEvent; use craft\helpers\UrlHelper; -use craft\helpers\ConfigHelper; -use craft\web\Controller; use craft\web\UrlManager; use craft\web\View; use fostercommerce\entrytyperules\assetbundles\entrytyperules\EntryTypeRulesAsset; @@ -28,8 +26,7 @@ */ class Plugin extends BasePlugin { - - public static ?Plugin $plugin; + public static ?Plugin $plugin = null; public bool $hasCpSettings = true; @@ -43,9 +40,8 @@ public function init(): void parent::init(); self::$plugin = $this; - /** @var Settings $settings */ - $settings = self::$plugin->getSettings(); - self::$settings = $settings; + self::$plugin->getSettings(); + //self::$settings = $settings; $this->setComponents([ 'service' => Service::class, @@ -77,39 +73,26 @@ function (DefineHtmlEvent $event): void { Event::on( - UrlManager::class, - UrlManager::EVENT_REGISTER_CP_URL_RULES, - function(RegisterUrlRulesEvent $event) { - // Register Control Panel routes - $event->rules = array_merge( - $event->rules, + UrlManager::class, + UrlManager::EVENT_REGISTER_CP_URL_RULES, + function (RegisterUrlRulesEvent $event): void { + // Register Control Panel routes + $event->rules = array_merge( + $event->rules, [ - 'entry-type-rules' => 'entry-type-rules/settings', + 'entry-type-rules' => 'entry-type-rules/settings', // 'entry-type-rules/' => 'entry-type-rules/settings', ], - ); - } - ); + ); + } + ); } public function getSettingsResponse(): mixed { - // $site = Craft::$app->request->getParam('site'); - - // $overrides = Craft::$app->getConfig()->getConfigFromFile($this->handle); - - // /** @var Controller $controller */ - // $controller = Craft::$app->controller; - // return $controller->renderTemplate( - // 'entry-type-rules/settings', - // [ - // 'settings' => $this->getSettings(), - // 'overrides' => ConfigHelper::localizedValue($overrides, $site), - // 'sectionsUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('settings/sections', $site)), - // 'entriesUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('entries', $site)), - // ] - // ); - return Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('entry-type-rules/settings')); + /** @var \craft\web\Response $response */ + $response = Craft::$app->getResponse(); + return $response->redirect(UrlHelper::cpUrl('entry-type-rules/settings')); } protected function createSettingsModel(): ?Model diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 04005bf..5b94b01 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -21,20 +21,18 @@ class SettingsController extends Controller { protected array|int|bool $allowAnonymous = []; - public function actionIndex(): Response { - $siteHandle = Craft::$app->request->getParam('site'); - // Get the site to edit - $siteHandle = $siteHandle ?? Cp::requestedSite()->handle; - $siteId = Craft::$app->getSites()->getSiteByHandle($siteHandle)->id; + $site = Cp::requestedSite(); + $siteHandle = $site?->handle; + $siteId = $site?->id; $variables = []; $overrides = Craft::$app->getConfig()->getConfigFromFile('entry-type-rules'); $variables = [ - 'settings' => Plugin::$settings, + 'settings' => Plugin::$plugin?->getSettings(), 'overrides' => ConfigHelper::localizedValue($overrides, $siteHandle), 'sectionsUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('settings/sections', $siteHandle)), 'entriesUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('entries', $siteHandle)), @@ -50,7 +48,6 @@ public function actionIndex(): Response ); } - /** * Handle a request going to our plugin's action URL for saving settings, * e.g.: actions/craft-entry-type-rules/save-settings From 713268857b43540f21cd50bc93ecd7ee8171983e Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Mon, 16 Mar 2026 17:02:05 +0000 Subject: [PATCH 04/21] fixing phpstan issues --- src/Plugin.php | 5 ++ src/controllers/SettingsController.php | 84 +++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/Plugin.php b/src/Plugin.php index 7708b25..8fd8463 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -95,6 +95,11 @@ public function getSettingsResponse(): mixed return $response->redirect(UrlHelper::cpUrl('entry-type-rules/settings')); } + public function getPluginName(): ?string + { + return $this->name; + } + protected function createSettingsModel(): ?Model { return new Settings(); diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 5b94b01..37a2258 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -5,13 +5,16 @@ use Craft; use craft\errors\MissingComponentException; +use craft\helpers\ArrayHelper; use craft\helpers\ConfigHelper; use craft\helpers\Cp; use craft\helpers\UrlHelper; +use craft\models\Site; use craft\web\Controller; use craft\web\Request; use fostercommerce\entrytyperules\models\Settings; use fostercommerce\entrytyperules\Plugin; +use Illuminate\Support\Collection; use yii\base\InvalidConfigException; use yii\web\BadRequestHttpException; use yii\web\MethodNotAllowedHttpException; @@ -29,17 +32,24 @@ public function actionIndex(): Response $variables = []; + $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; + $overrides = Craft::$app->getConfig()->getConfigFromFile('entry-type-rules'); $variables = [ 'settings' => Plugin::$plugin?->getSettings(), 'overrides' => ConfigHelper::localizedValue($overrides, $siteHandle), 'sectionsUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('settings/sections', $siteHandle)), - 'entriesUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('entries', $siteHandle)), 'siteHandle' => $siteHandle, + 'siteHandleUri' => $siteHandleUri, 'siteId' => $siteId, + 'crumbs' => $this->_buildCrumbs(), ]; + $this->_buildCrumbs(); + + + /** @var Controller $controller */ $controller = Craft::$app->controller; return $controller->renderTemplate( @@ -79,4 +89,76 @@ public function actionSaveSettings(): Response return $this->redirectToPostedUrl(); } + + + /** + * @return array>|string>|string|null>> + */ + private function _buildCrumbs(): array + { + $sites = Craft::$app->getSites(); + $requestedSite = Cp::requestedSite() ?? Craft::$app->getSites()->getPrimarySite(); + $requestedSiteId = $requestedSite->id; + $requestedSiteName = $requestedSite->name; + + $siteCrumbItems = []; + $siteGroups = Craft::$app->getSites()->getAllGroups(); + $crumbSites = Collection::make($sites->getAllSites()) + ->map(fn (Site $site) => [ + 'site' => $site, + ]) + ->keyBy(fn (array $site) => $site['site']->id) + ->all(); + + foreach ($siteGroups as $siteGroup) { + $groupSites = $siteGroup->getSites(); + + if (empty($groupSites)) { + continue; + } + + $groupSiteItems = array_map(fn (Site $site) => [ + 'status' => $crumbSites[$site->id]['site']->status ?? null, + 'label' => Craft::t('site', $site->name), + 'url' => UrlHelper::cpUrl("entry-type-rules?site={$site->handle}"), + 'hidden' => ! isset($crumbSites[$site->id]), + 'selected' => $site->id === $requestedSiteId, + 'attributes' => [ + 'data' => [ + 'site-id' => $site->id, + ], + ], + ], $groupSites); + + if (count($siteGroups) > 1) { + $siteCrumbItems[] = [ + 'heading' => Craft::t('site', $siteGroup->name), + 'items' => $groupSiteItems, + 'hidden' => ! ArrayHelper::contains($groupSiteItems, fn (array $item) => ! $item['hidden']), + ]; + } else { + array_push($siteCrumbItems, ...$groupSiteItems); + } + } + // Add in the breadcrumbs + $crumbs = [ + [ + 'id' => 'language-menu', + 'icon' => 'world', + 'label' => Craft::t( + 'site', + $requestedSiteName + ), + 'menu' => [ + 'items' => $siteCrumbItems, + 'label' => Craft::t('site', 'Select site'), + ], + ], + [ + 'label' => Plugin::$plugin?->getPluginName(), + ], + ]; + + return $crumbs; + } } From 38352b7b0c830f7b4407e04573db91ade0359129 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Mon, 16 Mar 2026 17:52:55 +0000 Subject: [PATCH 05/21] add site id to settings form --- src/templates/settings.twig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/templates/settings.twig b/src/templates/settings.twig index ae9981d..a409a81 100755 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -65,6 +65,11 @@ value: 'entry-type-rules'}) }} + {{ forms.hidden({ + name: 'siteId', + value: siteId + })}} + {% for section in sections | filter(section => section.type != 'single') %} {% if not loop.first %}
{% endif %} From 8ad785726b3128c3125e95663244024b0499e89d Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Tue, 17 Mar 2026 17:11:24 +0000 Subject: [PATCH 06/21] Can't get settings to save from CP. Seeking help --- src/Plugin.php | 2 +- src/controllers/SettingsController.php | 5 +++-- src/templates/settings.twig | 12 +++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 8fd8463..74e813e 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -81,7 +81,7 @@ function (RegisterUrlRulesEvent $event): void { $event->rules, [ 'entry-type-rules' => 'entry-type-rules/settings', - // 'entry-type-rules/' => 'entry-type-rules/settings', + 'entry-type-rules/settings' => 'entry-type-rules/settings', ], ); } diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 37a2258..bdb8388 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -35,7 +35,6 @@ public function actionIndex(): Response $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; $overrides = Craft::$app->getConfig()->getConfigFromFile('entry-type-rules'); - $variables = [ 'settings' => Plugin::$plugin?->getSettings(), 'overrides' => ConfigHelper::localizedValue($overrides, $siteHandle), @@ -70,10 +69,12 @@ public function actionIndex(): Response public function actionSaveSettings(): Response { $this->requirePostRequest(); - /** @var Request $request */ $request = Craft::$app->getRequest(); + $siteHandle = Craft::$app->getSites()->getSiteById($request->getBodyParam('siteId'))->handle; + + /** @var Plugin $plugin */ $plugin = Plugin::getInstance(); diff --git a/src/templates/settings.twig b/src/templates/settings.twig index a409a81..1276ec2 100755 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -16,6 +16,8 @@ {% extends "_layouts/cp" %} {% import "_includes/forms" as forms %} +{% set siteHandle = siteHandle ?? craft.app.getSites().getPrimarySite().handle %} +{% set siteId = siteId ?? craft.app.getSites().getPrimarySite().id %} {% do view.registerAssetBundle("fostercommerce\\entrytyperules\\assetbundles\\entrytyperules\\EntryTypeRulesSettingsAsset") %} @@ -26,7 +28,7 @@ {% set configOverride = overrides.sections is defined %} {# If we are using overrides from a config file then use those values otherwise use the settings #} -{% set configSettings = configOverride ? overrides : settings %} +{% set configSettings = configOverride ? overrides : (settings ?? []) %} {% set sections = craft.app.entries.getAllSections() %} @@ -108,7 +110,7 @@ {% for entryType in section.entryTypes %} {% set entryTypeCount = craft.entries.section(section.handle).type(entryType.handle).count %} - {% set limitValue = configSettings.sections[section.handle][entryType.handle].limit|default('') %} + {% set limitValue = configSettings.sections[siteHandle][section.handle][entryType.handle].limit|default('') %} {{ entryType.name }} {{ entryType.handle }}
@@ -124,7 +126,7 @@
{{ forms.textField({ id: (entryType.handle ~ '_limit'), - name: ('sections[' ~ section.handle ~ ']['~ entryType.handle ~ '][limit]'), + name: (siteHandle ~ '[sections][' ~ section.handle ~ ']['~ entryType.handle ~ '][limit]'), type: 'number', size: 5, min: 0, @@ -137,11 +139,11 @@ {{ forms.checkboxSelectField({ id: (entryType.handle ~ '_userGroups'), - name: ('sections[' ~ section.handle ~ ']['~ entryType.handle ~ '][userGroups]'), + name: (siteHandle ~ '[sections][' ~ section.handle ~ ']['~ entryType.handle ~ '][userGroups]'), options: groupOptions, showAllOption: true, allValue: '', - values: configSettings.sections[section.handle][entryType.handle].userGroups|default('') + values: configSettings.sections[siteHandle][section.handle][entryType.handle].userGroups|default('') }) }} From 9e03460188703ea7c106906c6b9b5a7f4127c7e1 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Tue, 17 Mar 2026 17:22:10 +0000 Subject: [PATCH 07/21] udpate settings template --- src/templates/settings.twig | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/templates/settings.twig b/src/templates/settings.twig index 1276ec2..bdc9776 100755 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -41,6 +41,7 @@ {% endfor %} {% block content %} +{{ dump(settings.sections)}} {% if sections is empty %}

{{ 'configureSections'|t('entry-type-rules', {'url' : sectionsUrl })|raw }}

@@ -110,7 +111,7 @@ {% for entryType in section.entryTypes %} {% set entryTypeCount = craft.entries.section(section.handle).type(entryType.handle).count %} - {% set limitValue = configSettings.sections[siteHandle][section.handle][entryType.handle].limit|default('') %} + {% set limitValue = configSettings.sections[section.handle][entryType.handle].limit|default('') %} {{ entryType.name }} {{ entryType.handle }}
@@ -126,7 +127,7 @@
{{ forms.textField({ id: (entryType.handle ~ '_limit'), - name: (siteHandle ~ '[sections][' ~ section.handle ~ ']['~ entryType.handle ~ '][limit]'), + name: ('sections[' ~ section.handle ~ ']['~ entryType.handle ~ '][limit]'), type: 'number', size: 5, min: 0, @@ -139,11 +140,11 @@ {{ forms.checkboxSelectField({ id: (entryType.handle ~ '_userGroups'), - name: (siteHandle ~ '[sections][' ~ section.handle ~ ']['~ entryType.handle ~ '][userGroups]'), + name: ('sections[' ~ section.handle ~ ']['~ entryType.handle ~ '][userGroups]'), options: groupOptions, showAllOption: true, allValue: '', - values: configSettings.sections[siteHandle][section.handle][entryType.handle].userGroups|default('') + values: configSettings.sections[section.handle][entryType.handle].userGroups|default('') }) }} From 9a278508795a964bbaf14da79c16e9dce17afc2b Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Tue, 17 Mar 2026 18:00:06 +0000 Subject: [PATCH 08/21] retrieve old settings model and merge with new --- src/controllers/SettingsController.php | 5 +++++ src/templates/settings.twig | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index bdb8388..8a6ad83 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -78,10 +78,15 @@ public function actionSaveSettings(): Response /** @var Plugin $plugin */ $plugin = Plugin::getInstance(); + /** @var Settings $oldSettings */ + $oldSettings = Plugin::$plugin->getSettings(); + $settings = new Settings([ 'sections' => $request->getBodyParam('sections'), ]); + $settings->sections = ArrayHelper::merge($oldSettings->sections ?? [], $settings->sections ?? []); + if (! $settings->validate() || ! Craft::$app->getPlugins()->savePluginSettings($plugin, $settings->toArray())) { Craft::$app->getSession()->setError(Craft::t('app', 'Couldn’t save plugin settings.')); } else { diff --git a/src/templates/settings.twig b/src/templates/settings.twig index bdc9776..aec6733 100755 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -111,7 +111,7 @@ {% for entryType in section.entryTypes %} {% set entryTypeCount = craft.entries.section(section.handle).type(entryType.handle).count %} - {% set limitValue = configSettings.sections[section.handle][entryType.handle].limit|default('') %} + {% set limitValue = configSettings.sections[siteHandle][section.handle][entryType.handle].limit|default('') %} {{ entryType.name }} {{ entryType.handle }}
@@ -127,7 +127,7 @@
{{ forms.textField({ id: (entryType.handle ~ '_limit'), - name: ('sections[' ~ section.handle ~ ']['~ entryType.handle ~ '][limit]'), + name: ('sections[' ~ siteHandle ~ '][' ~ section.handle ~ ']['~ entryType.handle ~ '][limit]'), type: 'number', size: 5, min: 0, @@ -140,11 +140,11 @@ {{ forms.checkboxSelectField({ id: (entryType.handle ~ '_userGroups'), - name: ('sections[' ~ section.handle ~ ']['~ entryType.handle ~ '][userGroups]'), + name: ('sections[' ~ siteHandle ~ '][' ~ section.handle ~ ']['~ entryType.handle ~ '][userGroups]'), options: groupOptions, showAllOption: true, allValue: '', - values: configSettings.sections[section.handle][entryType.handle].userGroups|default('') + values: configSettings.sections[siteHandle][section.handle][entryType.handle].userGroups|default('') }) }} From bdbf0d99a81f4f761d0514063486efb5fd71a62d Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Wed, 18 Mar 2026 15:21:53 +0000 Subject: [PATCH 09/21] fixes to get cp settings working --- src/controllers/SettingsController.php | 18 ++++++++++++------ src/templates/settings.twig | 9 ++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 8a6ad83..01aa1d6 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -35,9 +35,11 @@ public function actionIndex(): Response $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; $overrides = Craft::$app->getConfig()->getConfigFromFile('entry-type-rules'); + // dd(Plugin::$plugin?->getSettings()); + $variables = [ - 'settings' => Plugin::$plugin?->getSettings(), - 'overrides' => ConfigHelper::localizedValue($overrides, $siteHandle), + 'settings' => Plugin::$plugin?->getSettings()->toArray(), + 'overrides' => $overrides, 'sectionsUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('settings/sections', $siteHandle)), 'siteHandle' => $siteHandle, 'siteHandleUri' => $siteHandleUri, @@ -45,6 +47,7 @@ public function actionIndex(): Response 'crumbs' => $this->_buildCrumbs(), ]; + $this->_buildCrumbs(); @@ -74,18 +77,21 @@ public function actionSaveSettings(): Response $siteHandle = Craft::$app->getSites()->getSiteById($request->getBodyParam('siteId'))->handle; - /** @var Plugin $plugin */ $plugin = Plugin::getInstance(); + $newSettings = $request->getBodyParam('sections'); + /** @var Settings $oldSettings */ - $oldSettings = Plugin::$plugin->getSettings(); + $oldSettings = $plugin->getSettings()->toArray(); + + $mergedSettings = ArrayHelper::merge($oldSettings['sections'] ?? [], $newSettings ?? []); + $settings = new Settings([ - 'sections' => $request->getBodyParam('sections'), + 'sections' => $mergedSettings, ]); - $settings->sections = ArrayHelper::merge($oldSettings->sections ?? [], $settings->sections ?? []); if (! $settings->validate() || ! Craft::$app->getPlugins()->savePluginSettings($plugin, $settings->toArray())) { Craft::$app->getSession()->setError(Craft::t('app', 'Couldn’t save plugin settings.')); diff --git a/src/templates/settings.twig b/src/templates/settings.twig index aec6733..a5b9361 100755 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -41,7 +41,6 @@ {% endfor %} {% block content %} -{{ dump(settings.sections)}} {% if sections is empty %}

{{ 'configureSections'|t('entry-type-rules', {'url' : sectionsUrl })|raw }}

@@ -111,7 +110,7 @@ {% for entryType in section.entryTypes %} {% set entryTypeCount = craft.entries.section(section.handle).type(entryType.handle).count %} - {% set limitValue = configSettings.sections[siteHandle][section.handle][entryType.handle].limit|default('') %} + {% set limitValue = configSettings.sections[section.handle][entryType.handle].limit[siteHandle]|default('') %} {{ entryType.name }} {{ entryType.handle }}
@@ -127,7 +126,7 @@
{{ forms.textField({ id: (entryType.handle ~ '_limit'), - name: ('sections[' ~ siteHandle ~ '][' ~ section.handle ~ ']['~ entryType.handle ~ '][limit]'), + name: ('sections[' ~ section.handle ~ ']['~ entryType.handle ~ '][limit][' ~ siteHandle ~ ']'), type: 'number', size: 5, min: 0, @@ -140,11 +139,11 @@ {{ forms.checkboxSelectField({ id: (entryType.handle ~ '_userGroups'), - name: ('sections[' ~ siteHandle ~ '][' ~ section.handle ~ ']['~ entryType.handle ~ '][userGroups]'), + name: ('sections[' ~ section.handle ~ ']['~ entryType.handle ~ '][userGroups][' ~ siteHandle ~']'), options: groupOptions, showAllOption: true, allValue: '', - values: configSettings.sections[siteHandle][section.handle][entryType.handle].userGroups|default('') + values: configSettings.sections[section.handle][entryType.handle].userGroups[siteHandle]|default('') }) }} From 8cd0a7f490803c7be9683f26d72653c92b1c6b16 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Wed, 18 Mar 2026 17:08:59 +0000 Subject: [PATCH 10/21] Update service to apply rules using new config values --- src/services/Service.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/services/Service.php b/src/services/Service.php index e2a9002..0359246 100755 --- a/src/services/Service.php +++ b/src/services/Service.php @@ -6,6 +6,8 @@ use craft\base\Component; use craft\elements\Entry; +use craft\helpers\ConfigHelper; +use craft\helpers\Cp; use craft\web\User; use fostercommerce\entrytyperules\models\Settings; use fostercommerce\entrytyperules\Plugin; @@ -21,6 +23,8 @@ public function getLockedEntryTypes(int $sectionId, User $user): array // We will return an array of locked entry type IDs $lockedEntryTypes = []; + $site = Cp::requestedSite(); + // Get the plugins settings /** @var Settings $settings */ $settings = Plugin::getInstance()?->getSettings(); @@ -51,24 +55,24 @@ public function getLockedEntryTypes(int $sectionId, User $user): array // Loop through the locked entry type settings foreach ($lockedTypesSettings as $typeHandle => $setting) { // Get the count of each entry type and compare it to the limit value - $limit = $setting['limit'] ?? 0; + $limit = ConfigHelper::localizedValue($setting['limit'], $site->handle) ?? 0; if ($limit > 0) { $entryCount = Entry::find()->sectionId($sectionId)->type($typeHandle)->count(); - if ($entryCount >= $setting['limit']) { + if ($entryCount >= $limit) { $lockedEntryTypes[] = $entryTypesIdsMap[$typeHandle]; } } // Check the users groups against the userGroup setting - if (isset($setting['userGroups']) && is_array($setting['userGroups'])) { - $matchedGroups = array_intersect($setting['userGroups'], $userGroupArray); + $userGroups = ConfigHelper::localizedValue($setting['userGroups']); + if (is_array($userGroups)) { + $matchedGroups = array_intersect($userGroups, $userGroupArray); if ($matchedGroups === [] && ! $user->getIsAdmin()) { $lockedEntryTypes[] = $entryTypesIdsMap[$typeHandle]; } } } - return array_unique($lockedEntryTypes); } } From 2f7ea9c61dae7ec87ee624f089baba9f24f8c32d Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Wed, 18 Mar 2026 17:36:31 +0000 Subject: [PATCH 11/21] fix for improper typing of old settings --- src/controllers/SettingsController.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 01aa1d6..e2066f1 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -83,9 +83,9 @@ public function actionSaveSettings(): Response $newSettings = $request->getBodyParam('sections'); /** @var Settings $oldSettings */ - $oldSettings = $plugin->getSettings()->toArray(); + $oldSettings = $plugin->getSettings(); - $mergedSettings = ArrayHelper::merge($oldSettings['sections'] ?? [], $newSettings ?? []); + $mergedSettings = ArrayHelper::merge($oldSettings->toArray()['sections'] ?? [], $newSettings ?? []); $settings = new Settings([ @@ -93,6 +93,7 @@ public function actionSaveSettings(): Response ]); + if (! $settings->validate() || ! Craft::$app->getPlugins()->savePluginSettings($plugin, $settings->toArray())) { Craft::$app->getSession()->setError(Craft::t('app', 'Couldn’t save plugin settings.')); } else { From 58765730e00ee28668183a1ffaef8141bffb16ac Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Wed, 18 Mar 2026 17:53:19 +0000 Subject: [PATCH 12/21] fix for setting user groups --- src/controllers/SettingsController.php | 28 +++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index e2066f1..162fc69 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -82,10 +82,11 @@ public function actionSaveSettings(): Response $newSettings = $request->getBodyParam('sections'); - /** @var Settings $oldSettings */ - $oldSettings = $plugin->getSettings(); + $oldSettings = $plugin->getSettings()->toArray(); - $mergedSettings = ArrayHelper::merge($oldSettings->toArray()['sections'] ?? [], $newSettings ?? []); + $this->_removeUserGroupsForSite($oldSettings, $siteHandle); + + $mergedSettings = ArrayHelper::merge($oldSettings['sections'] ?? [], $newSettings ?? []); $settings = new Settings([ @@ -174,4 +175,25 @@ private function _buildCrumbs(): array return $crumbs; } + + + function _removeUserGroupsForSite(array &$array, string $targetSite, ?string $currentSite = null): void { + foreach ($array as $key => &$value) { + // Track when we enter a site-specific branch + $nextSite = $currentSite; + if ($key === 'firstSite' || $key === 'secondSite') { + $nextSite = $key; + } + + // Remove userGroups only if we're inside the target site + if ($key === 'userGroups' && $currentSite === $targetSite) { + unset($array[$key]); + continue; + } + + if (is_array($value)) { + $this->_removeUserGroupsForSite($value, $targetSite, $nextSite); + } + } + } } From ba8a8e0b56907b58f7137d56d0f5fec80f656e45 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Wed, 18 Mar 2026 17:54:15 +0000 Subject: [PATCH 13/21] ecs fixes --- src/controllers/SettingsController.php | 41 +++++++++++++------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 162fc69..2234f94 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -104,6 +104,26 @@ public function actionSaveSettings(): Response return $this->redirectToPostedUrl(); } + public function _removeUserGroupsForSite(array &$array, string $targetSite, ?string $currentSite = null): void + { + foreach ($array as $key => &$value) { + // Track when we enter a site-specific branch + $nextSite = $currentSite; + if ($key === 'firstSite' || $key === 'secondSite') { + $nextSite = $key; + } + + // Remove userGroups only if we're inside the target site + if ($key === 'userGroups' && $currentSite === $targetSite) { + unset($array[$key]); + continue; + } + + if (is_array($value)) { + $this->_removeUserGroupsForSite($value, $targetSite, $nextSite); + } + } + } /** * @return array>|string>|string|null>> @@ -175,25 +195,4 @@ private function _buildCrumbs(): array return $crumbs; } - - - function _removeUserGroupsForSite(array &$array, string $targetSite, ?string $currentSite = null): void { - foreach ($array as $key => &$value) { - // Track when we enter a site-specific branch - $nextSite = $currentSite; - if ($key === 'firstSite' || $key === 'secondSite') { - $nextSite = $key; - } - - // Remove userGroups only if we're inside the target site - if ($key === 'userGroups' && $currentSite === $targetSite) { - unset($array[$key]); - continue; - } - - if (is_array($value)) { - $this->_removeUserGroupsForSite($value, $targetSite, $nextSite); - } - } - } } From c42f6938136828a7d079e92e184b3f27fbf70fe2 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Thu, 19 Mar 2026 16:37:26 +0000 Subject: [PATCH 14/21] deal with default values specified in config file --- src/controllers/SettingsController.php | 85 +++++++++++++++++++------- 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 2234f94..866395d 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -35,7 +35,11 @@ public function actionIndex(): Response $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; $overrides = Craft::$app->getConfig()->getConfigFromFile('entry-type-rules'); - // dd(Plugin::$plugin?->getSettings()); + + // walk through overrides array, + // if the value set for 'limit' is not an array then replace it with an array of siteHandles all with same value + // if the value set for limit is an array and there is a key for '*' then replace it with an array of siteHandles all with same value + $this->_globalValues($overrides); $variables = [ 'settings' => Plugin::$plugin?->getSettings()->toArray(), @@ -86,6 +90,7 @@ public function actionSaveSettings(): Response $this->_removeUserGroupsForSite($oldSettings, $siteHandle); + $mergedSettings = ArrayHelper::merge($oldSettings['sections'] ?? [], $newSettings ?? []); @@ -104,26 +109,6 @@ public function actionSaveSettings(): Response return $this->redirectToPostedUrl(); } - public function _removeUserGroupsForSite(array &$array, string $targetSite, ?string $currentSite = null): void - { - foreach ($array as $key => &$value) { - // Track when we enter a site-specific branch - $nextSite = $currentSite; - if ($key === 'firstSite' || $key === 'secondSite') { - $nextSite = $key; - } - - // Remove userGroups only if we're inside the target site - if ($key === 'userGroups' && $currentSite === $targetSite) { - unset($array[$key]); - continue; - } - - if (is_array($value)) { - $this->_removeUserGroupsForSite($value, $targetSite, $nextSite); - } - } - } /** * @return array>|string>|string|null>> @@ -195,4 +180,62 @@ private function _buildCrumbs(): array return $crumbs; } + + + function _removeUserGroupsForSite(array &$array, string $targetSite, ?string $currentSite = null): void + { + foreach ($array as $key => &$value) { + // Track when we enter a site-specific branch + $nextSite = $currentSite; + if ($key === 'firstSite' || $key === 'secondSite') { + $nextSite = $key; + } + + // Remove userGroups only if we're inside the target site + if ($key === 'userGroups' && $currentSite === $targetSite) { + unset($array[$key]); + continue; + } + + if (is_array($value)) { + $this->_removeUserGroupsForSite($value, $targetSite, $nextSite); + } + } + } + + + private function _globalValues(array &$array): void + { + // get all sitehandles + $sites = Craft::$app->getSites()->getAllSites(); + $siteHandles = array_map(function($site) { + return $site->handle; + }, $sites); + + foreach ($array as $key => &$value) { + // if we only have an integer as a limit value, then set the value to be an array of site handles set to the value + if ($key === 'limit' && !is_array($value)) { + $limitValue = $value; + $value = array_fill_keys($siteHandles, $limitValue); + } + + // if we have an array of limit values, but the array contains the key '*' + // then set any undefined sites to use the value for '*' + if ($key === 'limit' and is_array($value)) { + if(array_key_exists('*', $value)){ + $limitValue = $value['*']; + $missingSiteHandles = array_values(array_diff($siteHandles, array_keys($value))); + $defaultedSiteHandles = array_fill_keys($missingSiteHandles, $limitValue); + $value = array_merge($value, $defaultedSiteHandles); + // unset the '*' + unset($value['*']); + } + } + + + if (is_array($value)) { + $this->_globalValues($value); + } + } + } } From 324b28162e2d7ef1676be8590996327d3c3c7197 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Thu, 19 Mar 2026 16:58:22 +0000 Subject: [PATCH 15/21] Get sections in default controller action so we can easily only return sections applicable to the site being edited --- src/controllers/SettingsController.php | 5 +++++ src/templates/settings.twig | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 866395d..7d8f75c 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -29,7 +29,11 @@ public function actionIndex(): Response $site = Cp::requestedSite(); $siteHandle = $site?->handle; $siteId = $site?->id; + $sections = Craft::$app->getEntries()->getAllSections(); + $enabledSections = array_filter($sections, function($section) use ($siteId) { + return $section->getSiteSettings()[$siteId]->enabledByDefault ?? false; + }); $variables = []; $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; @@ -42,6 +46,7 @@ public function actionIndex(): Response $this->_globalValues($overrides); $variables = [ + 'sections' => $enabledSections, 'settings' => Plugin::$plugin?->getSettings()->toArray(), 'overrides' => $overrides, 'sectionsUrl' => ConfigHelper::localizedValue(UrlHelper::cpUrl('settings/sections', $siteHandle)), diff --git a/src/templates/settings.twig b/src/templates/settings.twig index a5b9361..3516465 100755 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -30,8 +30,6 @@ {# If we are using overrides from a config file then use those values otherwise use the settings #} {% set configSettings = configOverride ? overrides : (settings ?? []) %} -{% set sections = craft.app.entries.getAllSections() %} - {% set groupOptions = [] %} {% for group in craft.app.userGroups.allGroups %} {% set groupOptions = groupOptions|merge([{ @@ -41,6 +39,7 @@ {% endfor %} {% block content %} +{{ dump(sections)}} {% if sections is empty %}

{{ 'configureSections'|t('entry-type-rules', {'url' : sectionsUrl })|raw }}

@@ -73,7 +72,6 @@ })}} {% for section in sections | filter(section => section.type != 'single') %} - {% if not loop.first %}
{% endif %}
From 0a577b8740a87f91d4ba77be5547c604c97f5e27 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Thu, 19 Mar 2026 17:10:08 +0000 Subject: [PATCH 16/21] remove debug dump --- src/templates/settings.twig | 1 - 1 file changed, 1 deletion(-) diff --git a/src/templates/settings.twig b/src/templates/settings.twig index 3516465..87d7775 100755 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -39,7 +39,6 @@ {% endfor %} {% block content %} -{{ dump(sections)}} {% if sections is empty %}

{{ 'configureSections'|t('entry-type-rules', {'url' : sectionsUrl })|raw }}

From a4d66337dd6911fec4b145e4573f55b262a738e3 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Thu, 19 Mar 2026 17:11:36 +0000 Subject: [PATCH 17/21] add todo note --- src/controllers/SettingsController.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 7d8f75c..2396616 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -237,6 +237,8 @@ private function _globalValues(array &$array): void } } + //TODO global settings for usergroups + if (is_array($value)) { $this->_globalValues($value); From 1de90c9aec5ebdbbcc2bbe57b64d785c26c32970 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Thu, 19 Mar 2026 17:21:01 +0000 Subject: [PATCH 18/21] Apply * setting for usergroups set in config file --- src/controllers/SettingsController.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 2396616..f19694c 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -225,21 +225,19 @@ private function _globalValues(array &$array): void } // if we have an array of limit values, but the array contains the key '*' + // or there is a '*' array in the userGroups // then set any undefined sites to use the value for '*' - if ($key === 'limit' and is_array($value)) { + if (($key === 'limit' and is_array($value)) || $key === 'userGroups') { if(array_key_exists('*', $value)){ - $limitValue = $value['*']; + $defaultValue = $value['*']; $missingSiteHandles = array_values(array_diff($siteHandles, array_keys($value))); - $defaultedSiteHandles = array_fill_keys($missingSiteHandles, $limitValue); + $defaultedSiteHandles = array_fill_keys($missingSiteHandles, $defaultValue); $value = array_merge($value, $defaultedSiteHandles); // unset the '*' unset($value['*']); } } - //TODO global settings for usergroups - - if (is_array($value)) { $this->_globalValues($value); } From d2961df88a7372cdd6587191c3f7c8a9bc2cfb2f Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Fri, 20 Mar 2026 09:21:02 +0000 Subject: [PATCH 19/21] ECS fixes --- src/controllers/SettingsController.php | 51 ++++++++++++-------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index f19694c..78e36bf 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -31,7 +31,7 @@ public function actionIndex(): Response $siteId = $site?->id; $sections = Craft::$app->getEntries()->getAllSections(); - $enabledSections = array_filter($sections, function($section) use ($siteId) { + $enabledSections = array_filter($sections, function ($section) use ($siteId) { return $section->getSiteSettings()[$siteId]->enabledByDefault ?? false; }); $variables = []; @@ -114,6 +114,26 @@ public function actionSaveSettings(): Response return $this->redirectToPostedUrl(); } + public function _removeUserGroupsForSite(array &$array, string $targetSite, ?string $currentSite = null): void + { + foreach ($array as $key => &$value) { + // Track when we enter a site-specific branch + $nextSite = $currentSite; + if ($key === 'firstSite' || $key === 'secondSite') { + $nextSite = $key; + } + + // Remove userGroups only if we're inside the target site + if ($key === 'userGroups' && $currentSite === $targetSite) { + unset($array[$key]); + continue; + } + + if (is_array($value)) { + $this->_removeUserGroupsForSite($value, $targetSite, $nextSite); + } + } + } /** * @return array>|string>|string|null>> @@ -186,40 +206,17 @@ private function _buildCrumbs(): array return $crumbs; } - - function _removeUserGroupsForSite(array &$array, string $targetSite, ?string $currentSite = null): void - { - foreach ($array as $key => &$value) { - // Track when we enter a site-specific branch - $nextSite = $currentSite; - if ($key === 'firstSite' || $key === 'secondSite') { - $nextSite = $key; - } - - // Remove userGroups only if we're inside the target site - if ($key === 'userGroups' && $currentSite === $targetSite) { - unset($array[$key]); - continue; - } - - if (is_array($value)) { - $this->_removeUserGroupsForSite($value, $targetSite, $nextSite); - } - } - } - - private function _globalValues(array &$array): void { // get all sitehandles $sites = Craft::$app->getSites()->getAllSites(); - $siteHandles = array_map(function($site) { + $siteHandles = array_map(function ($site) { return $site->handle; }, $sites); foreach ($array as $key => &$value) { // if we only have an integer as a limit value, then set the value to be an array of site handles set to the value - if ($key === 'limit' && !is_array($value)) { + if ($key === 'limit' && ! is_array($value)) { $limitValue = $value; $value = array_fill_keys($siteHandles, $limitValue); } @@ -228,7 +225,7 @@ private function _globalValues(array &$array): void // or there is a '*' array in the userGroups // then set any undefined sites to use the value for '*' if (($key === 'limit' and is_array($value)) || $key === 'userGroups') { - if(array_key_exists('*', $value)){ + if (array_key_exists('*', $value)) { $defaultValue = $value['*']; $missingSiteHandles = array_values(array_diff($siteHandles, array_keys($value))); $defaultedSiteHandles = array_fill_keys($missingSiteHandles, $defaultValue); From 2ad355b2e17dc08a31a1eb7e777ad10f680670b9 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Fri, 20 Mar 2026 09:25:14 +0000 Subject: [PATCH 20/21] rector fixes --- src/controllers/SettingsController.php | 35 +++++++++++--------------- src/services/Service.php | 1 + 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 78e36bf..8dbe43c 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -31,9 +31,7 @@ public function actionIndex(): Response $siteId = $site?->id; $sections = Craft::$app->getEntries()->getAllSections(); - $enabledSections = array_filter($sections, function ($section) use ($siteId) { - return $section->getSiteSettings()[$siteId]->enabledByDefault ?? false; - }); + $enabledSections = array_filter($sections, fn($section) => $section->getSiteSettings()[$siteId]->enabledByDefault ?? false); $variables = []; $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; @@ -148,10 +146,10 @@ private function _buildCrumbs(): array $siteCrumbItems = []; $siteGroups = Craft::$app->getSites()->getAllGroups(); $crumbSites = Collection::make($sites->getAllSites()) - ->map(fn (Site $site) => [ + ->map(fn (Site $site): array => [ 'site' => $site, ]) - ->keyBy(fn (array $site) => $site['site']->id) + ->keyBy(fn (array $site): ?int => $site['site']->id) ->all(); foreach ($siteGroups as $siteGroup) { @@ -161,7 +159,7 @@ private function _buildCrumbs(): array continue; } - $groupSiteItems = array_map(fn (Site $site) => [ + $groupSiteItems = array_map(fn (Site $site): array => [ 'status' => $crumbSites[$site->id]['site']->status ?? null, 'label' => Craft::t('site', $site->name), 'url' => UrlHelper::cpUrl("entry-type-rules?site={$site->handle}"), @@ -178,12 +176,13 @@ private function _buildCrumbs(): array $siteCrumbItems[] = [ 'heading' => Craft::t('site', $siteGroup->name), 'items' => $groupSiteItems, - 'hidden' => ! ArrayHelper::contains($groupSiteItems, fn (array $item) => ! $item['hidden']), + 'hidden' => ! ArrayHelper::contains($groupSiteItems, fn (array $item): bool => ! $item['hidden']), ]; } else { array_push($siteCrumbItems, ...$groupSiteItems); } } + // Add in the breadcrumbs $crumbs = [ [ @@ -210,9 +209,7 @@ private function _globalValues(array &$array): void { // get all sitehandles $sites = Craft::$app->getSites()->getAllSites(); - $siteHandles = array_map(function ($site) { - return $site->handle; - }, $sites); + $siteHandles = array_map(fn($site) => $site->handle, $sites); foreach ($array as $key => &$value) { // if we only have an integer as a limit value, then set the value to be an array of site handles set to the value @@ -224,16 +221,14 @@ private function _globalValues(array &$array): void // if we have an array of limit values, but the array contains the key '*' // or there is a '*' array in the userGroups // then set any undefined sites to use the value for '*' - if (($key === 'limit' and is_array($value)) || $key === 'userGroups') { - if (array_key_exists('*', $value)) { - $defaultValue = $value['*']; - $missingSiteHandles = array_values(array_diff($siteHandles, array_keys($value))); - $defaultedSiteHandles = array_fill_keys($missingSiteHandles, $defaultValue); - $value = array_merge($value, $defaultedSiteHandles); - // unset the '*' - unset($value['*']); - } - } + if (($key === 'limit' && is_array($value) || $key === 'userGroups') && array_key_exists('*', $value)) { + $defaultValue = $value['*']; + $missingSiteHandles = array_values(array_diff($siteHandles, array_keys($value))); + $defaultedSiteHandles = array_fill_keys($missingSiteHandles, $defaultValue); + $value = array_merge($value, $defaultedSiteHandles); + // unset the '*' + unset($value['*']); + } if (is_array($value)) { $this->_globalValues($value); diff --git a/src/services/Service.php b/src/services/Service.php index 0359246..49a6e9a 100755 --- a/src/services/Service.php +++ b/src/services/Service.php @@ -73,6 +73,7 @@ public function getLockedEntryTypes(int $sectionId, User $user): array } } } + return array_unique($lockedEntryTypes); } } From b078122ef5c9017bbbd6f3c8790f4e4f057285d5 Mon Sep 17 00:00:00 2001 From: Pete Eveleigh Date: Fri, 20 Mar 2026 09:25:42 +0000 Subject: [PATCH 21/21] ecs fix --- src/controllers/SettingsController.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 8dbe43c..2bc95c2 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -31,7 +31,7 @@ public function actionIndex(): Response $siteId = $site?->id; $sections = Craft::$app->getEntries()->getAllSections(); - $enabledSections = array_filter($sections, fn($section) => $section->getSiteSettings()[$siteId]->enabledByDefault ?? false); + $enabledSections = array_filter($sections, fn ($section) => $section->getSiteSettings()[$siteId]->enabledByDefault ?? false); $variables = []; $siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; @@ -209,7 +209,7 @@ private function _globalValues(array &$array): void { // get all sitehandles $sites = Craft::$app->getSites()->getAllSites(); - $siteHandles = array_map(fn($site) => $site->handle, $sites); + $siteHandles = array_map(fn ($site) => $site->handle, $sites); foreach ($array as $key => &$value) { // if we only have an integer as a limit value, then set the value to be an array of site handles set to the value @@ -222,13 +222,13 @@ private function _globalValues(array &$array): void // or there is a '*' array in the userGroups // then set any undefined sites to use the value for '*' if (($key === 'limit' && is_array($value) || $key === 'userGroups') && array_key_exists('*', $value)) { - $defaultValue = $value['*']; - $missingSiteHandles = array_values(array_diff($siteHandles, array_keys($value))); - $defaultedSiteHandles = array_fill_keys($missingSiteHandles, $defaultValue); - $value = array_merge($value, $defaultedSiteHandles); - // unset the '*' - unset($value['*']); - } + $defaultValue = $value['*']; + $missingSiteHandles = array_values(array_diff($siteHandles, array_keys($value))); + $defaultedSiteHandles = array_fill_keys($missingSiteHandles, $defaultValue); + $value = array_merge($value, $defaultedSiteHandles); + // unset the '*' + unset($value['*']); + } if (is_array($value)) { $this->_globalValues($value);