Skip to content

Commit 9f9e61c

Browse files
committed
Language condition rule
Resolves #15952
1 parent 0bb8ccd commit 9f9e61c

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

CHANGELOG-WIP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Release Notes for Craft CMS 4.13 (WIP)
22

33
### Administration
4+
- Added the “Language” element condition rule. ([#15952](https://github.com/craftcms/cms/discussions/15952))
45
- Added `pc/*` commands as an alias of `project-config/*`.
56
- Added the `--except`, `--minor-only`, and `--patch-only` options to the `update` command. ([#15829](https://github.com/craftcms/cms/pull/15829))
67

src/elements/conditions/ElementCondition.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ protected function conditionRuleTypes(): array
130130

131131
if (Craft::$app->getIsMultiSite() && (!$elementType || $elementType::isLocalized())) {
132132
$types[] = SiteConditionRule::class;
133+
$types[] = LanguageConditionRule::class;
133134

134135
if (count(Craft::$app->getSites()->getAllGroups()) > 1) {
135136
$types[] = SiteGroupConditionRule::class;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace craft\elements\conditions;
4+
5+
use Craft;
6+
use craft\base\conditions\BaseMultiSelectConditionRule;
7+
use craft\base\ElementInterface;
8+
use craft\elements\db\ElementQueryInterface;
9+
use craft\helpers\ArrayHelper;
10+
use craft\i18n\Locale;
11+
12+
/**
13+
* Site condition rule.
14+
*
15+
* @author Pixel & Tonic, Inc. <[email protected]>
16+
* @since 4.13.0
17+
*/
18+
class LanguageConditionRule extends BaseMultiSelectConditionRule implements ElementConditionRuleInterface
19+
{
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getLabel(): string
24+
{
25+
return Craft::t('app', 'Language');
26+
}
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
public function getExclusiveQueryParams(): array
32+
{
33+
return ['site', 'siteId'];
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function options(): array
40+
{
41+
return ArrayHelper::map(
42+
Craft::$app->getI18n()->getSiteLocales(),
43+
fn(Locale $locale) => $locale->id,
44+
fn(Locale $locale) => $locale->getDisplayName(Craft::$app->language),
45+
);
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function modifyQuery(ElementQueryInterface $query): void
52+
{
53+
$query->language($this->paramValue());
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function matchElement(ElementInterface $element): bool
60+
{
61+
return $this->matchValue($element->getLanguage());
62+
}
63+
}

0 commit comments

Comments
 (0)