Skip to content

Commit

Permalink
Site Group condition rule
Browse files Browse the repository at this point in the history
Resolves #15625
  • Loading branch information
brandonkelly committed Aug 30, 2024
1 parent ca91aa3 commit 0ed9968
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Release Notes for Craft CMS 4.12 (WIP)

### Content Management
- Element conditions can now have a “Site Group” rule, if there are two or more site groups. ([#15625](https://github.com/craftcms/cms/discussions/15625))

### Development
- Country field values and `craft\elements\Address::getCountry()` now return the country in the current application locale.

### Extensibility
- Added `craft\base\ApplicationTrait::getEnvId()`. ([#15313](https://github.com/craftcms/cms/issues/15313))
- Added `craft\base\ElementInterface::getRootOwner()`. ([#15534](https://github.com/craftcms/cms/discussions/15534))
- Added `craft\elements\conditions\SiteGroupConditionRule`.
- Added `craft\services\Sites::getEditableSitesByGroupId()`.
- Deprecated `craft\helpers\ElementHelper::rootElement()`. `craft\base\ElementInterface::getRootOwner()` should be used instead.
- Deprecated `craft\db\mysql\Schema::quoteDatabaseName()`.
- Deprecated `craft\db\pgqsl\Schema::quoteDatabaseName()`.
Expand Down
4 changes: 4 additions & 0 deletions src/elements/conditions/ElementCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ protected function conditionRuleTypes(): array

if (Craft::$app->getIsMultiSite() && (!$elementType || $elementType::isLocalized())) {
$types[] = SiteConditionRule::class;

if (count(Craft::$app->getSites()->getAllGroups()) > 1) {
$types[] = SiteGroupConditionRule::class;
}
}

if ($elementType !== null) {
Expand Down
73 changes: 73 additions & 0 deletions src/elements/conditions/SiteGroupConditionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace craft\elements\conditions;

use Craft;
use craft\base\conditions\BaseMultiSelectConditionRule;
use craft\base\ElementInterface;
use craft\elements\db\ElementQueryInterface;
use craft\models\Site;
use craft\models\SiteGroup;
use Illuminate\Support\Collection;

/**
* Site Group condition rule.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.12.0
*/
class SiteGroupConditionRule extends BaseMultiSelectConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritdoc
*/
public function getLabel(): string
{
return Craft::t('app', 'Site Group');
}

/**
* @inheritdoc
*/
public function getExclusiveQueryParams(): array
{
return ['site', 'siteId'];
}

/**
* @inheritdoc
*/
protected function options(): array
{
$sitesService = Craft::$app->getSites();
return Collection::make($sitesService->getAllGroups())
->filter(fn(SiteGroup $group) => !empty($sitesService->getEditableSitesByGroupId($group->id)))
->keyBy(fn(SiteGroup $group) => $group->uid)
->map(fn(SiteGroup $group) => $group->getName())
->all();
}

/**
* @inheritdoc
*/
public function modifyQuery(ElementQueryInterface $query): void
{
$sitesService = Craft::$app->getSites();
$siteIds = Collection::make((array)$this->paramValue())
->map(fn(string $uid) => $sitesService->getGroupByUid($uid))
->filter(fn(?SiteGroup $group) => $group !== null)
->map(fn(SiteGroup $group) => $sitesService->getEditableSitesByGroupId($group->id))
->flatten(1)
->map(fn(Site $site) => $site->id)
->all();
$query->siteId($siteIds);
}

/**
* @inheritdoc
*/
public function matchElement(ElementInterface $element): bool
{
return $this->matchValue($element->getSite()->getGroup()->uid);
}
}
18 changes: 18 additions & 0 deletions src/services/Sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use craft\queue\jobs\PropagateElements;
use craft\records\Site as SiteRecord;
use craft\records\SiteGroup as SiteGroupRecord;
use Illuminate\Support\Collection;
use Throwable;
use yii\base\Component;
use yii\base\Exception;
Expand Down Expand Up @@ -589,6 +590,23 @@ public function getSitesByGroupId(int $groupId, ?bool $withDisabled = null): arr
return $sites;
}

/**
* Returns editable sites by a group ID.
*
* @param int $groupId
* @param bool|null $withDisabled
* @return Site[]
* @since 4.12.0
*/
public function getEditableSitesByGroupId(int $groupId, ?bool $withDisabled = null): array
{
$editableSiteIds = array_flip($this->getEditableSiteIds());
return Collection::make($this->getSitesByGroupId($groupId, $withDisabled))
->filter(fn(Site $site) => isset($editableSiteIds[$site->id]))
->values()
->all();
}

/**
* Gets the total number of sites.
*
Expand Down
1 change: 1 addition & 0 deletions src/translations/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,7 @@
'Single' => 'Single',
'Single-line text' => 'Single-line text',
'Singles' => 'Singles',
'Site Group' => 'Site Group',
'Site Icon' => 'Site Icon',
'Site Settings' => 'Site Settings',
'Site saved.' => 'Site saved.',
Expand Down

0 comments on commit 0ed9968

Please sign in to comment.