From 9f9e61cab032407e0f6119fce9a46a14b9c1fae5 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 23 Oct 2024 08:42:49 -0700 Subject: [PATCH] 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()); + } +}