Skip to content

Commit

Permalink
Language condition rule
Browse files Browse the repository at this point in the history
Resolves #15952
  • Loading branch information
brandonkelly committed Oct 23, 2024
1 parent 0bb8ccd commit 9f9e61c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
@@ -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))

Expand Down
1 change: 1 addition & 0 deletions src/elements/conditions/ElementCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
63 changes: 63 additions & 0 deletions src/elements/conditions/LanguageConditionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace craft\elements\conditions;

use Craft;
use craft\base\conditions\BaseMultiSelectConditionRule;
use craft\base\ElementInterface;
use craft\elements\db\ElementQueryInterface;
use craft\helpers\ArrayHelper;
use craft\i18n\Locale;

/**
* Site condition rule.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @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());
}
}

0 comments on commit 9f9e61c

Please sign in to comment.