From 0bb8ccd4bbfa88650a2968e5ecde8aaf6709ad8b Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 23 Oct 2024 08:41:58 -0700 Subject: [PATCH 1/2] No more SitefilterTrait::$enabled --- CHANGELOG-WIP.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 134bd378df3..8d1ffdd322f 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -16,7 +16,6 @@ - Added `craft\filters\BasicHttpAuthLogin`. ([#15720](https://github.com/craftcms/cms/pull/15720)) - Added `craft\filters\BasicHttpAuthStatic`. ([#15720](https://github.com/craftcms/cms/pull/15720)) - Added `craft\filters\ConditionalFilterTrait`. ([#15948](https://github.com/craftcms/cms/pull/15948)) -- Added `craft\filters\SiteFilterTrait::$enabled`. ([#15720](https://github.com/craftcms/cms/pull/15720)) - Added `craft\filters\UtilityAccess`. - Added `craft\helpers\UrlHelper::encodeUrl()`. ([#15838](https://github.com/craftcms/cms/issues/15838)) - Added `craft\services\Addresses::EVENT_DEFINE_ADDRESS_COUNTRIES`. ([#15711](https://github.com/craftcms/cms/pull/15711)) From 9f9e61cab032407e0f6119fce9a46a14b9c1fae5 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 23 Oct 2024 08:42:49 -0700 Subject: [PATCH 2/2] Language condition rule Resolves #15952 --- CHANGELOG-WIP.md | 1 + src/elements/conditions/ElementCondition.php | 1 + .../conditions/LanguageConditionRule.php | 63 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/elements/conditions/LanguageConditionRule.php diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 8d1ffdd322f..325012c4ebf 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -1,6 +1,7 @@ # Release Notes for Craft CMS 4.13 (WIP) ### Administration +- Added the “Language” element condition rule. ([#15952](https://github.com/craftcms/cms/discussions/15952)) - Added `pc/*` commands as an alias of `project-config/*`. - Added the `--except`, `--minor-only`, and `--patch-only` options to the `update` command. ([#15829](https://github.com/craftcms/cms/pull/15829)) diff --git a/src/elements/conditions/ElementCondition.php b/src/elements/conditions/ElementCondition.php index 4db897b43ec..d42a103b2c0 100644 --- a/src/elements/conditions/ElementCondition.php +++ b/src/elements/conditions/ElementCondition.php @@ -130,6 +130,7 @@ protected function conditionRuleTypes(): array if (Craft::$app->getIsMultiSite() && (!$elementType || $elementType::isLocalized())) { $types[] = SiteConditionRule::class; + $types[] = LanguageConditionRule::class; if (count(Craft::$app->getSites()->getAllGroups()) > 1) { $types[] = SiteGroupConditionRule::class; diff --git a/src/elements/conditions/LanguageConditionRule.php b/src/elements/conditions/LanguageConditionRule.php new file mode 100644 index 00000000000..22c0a1df141 --- /dev/null +++ b/src/elements/conditions/LanguageConditionRule.php @@ -0,0 +1,63 @@ + + * @since 4.13.0 + */ +class LanguageConditionRule extends BaseMultiSelectConditionRule implements ElementConditionRuleInterface +{ + /** + * @inheritdoc + */ + public function getLabel(): string + { + return Craft::t('app', 'Language'); + } + + /** + * @inheritdoc + */ + public function getExclusiveQueryParams(): array + { + return ['site', 'siteId']; + } + + /** + * @inheritdoc + */ + protected function options(): array + { + return ArrayHelper::map( + Craft::$app->getI18n()->getSiteLocales(), + fn(Locale $locale) => $locale->id, + fn(Locale $locale) => $locale->getDisplayName(Craft::$app->language), + ); + } + + /** + * @inheritdoc + */ + public function modifyQuery(ElementQueryInterface $query): void + { + $query->language($this->paramValue()); + } + + /** + * @inheritdoc + */ + public function matchElement(ElementInterface $element): bool + { + return $this->matchValue($element->getLanguage()); + } +}