Skip to content

Commit

Permalink
Make sites chippable
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Nov 20, 2024
1 parent c8ecf6e commit 2b4b857
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
- Added `craft\base\conditions\BaseElementSelectConditionRule::setElementIds()`.
- Added `craft\fields\data\LinkData::$urlSuffix`.
- Added `craft\fields\data\LinkData::getUrl()`.
- `craft\models\Site` now implements `craft\base\Chippable`.
8 changes: 5 additions & 3 deletions src/elements/conditions/SiteConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use craft\base\conditions\BaseMultiSelectConditionRule;
use craft\base\ElementInterface;
use craft\elements\db\ElementQueryInterface;
use craft\helpers\ArrayHelper;
use craft\models\Site;

/**
* Site condition rule.
Expand Down Expand Up @@ -37,8 +37,10 @@ public function getExclusiveQueryParams(): array
*/
protected function options(): array
{
$sites = Craft::$app->getSites()->getEditableSites();
return ArrayHelper::map($sites, 'uid', 'name');
return array_map(fn(Site $site) => [
'label' => $site->getUiLabel(),
'value' => $site->uid,
], Craft::$app->getSites()->getEditableSites());
}

/**
Expand Down
27 changes: 25 additions & 2 deletions src/models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace craft\models;

use Craft;
use craft\base\Chippable;
use craft\base\Model;
use craft\helpers\App;
use craft\i18n\Locale;
Expand All @@ -29,8 +30,14 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class Site extends Model
class Site extends Model implements Chippable
{
public static function get(int|string $id): ?static
{
/** @phpstan-ignore-next-line */
return Craft::$app->getSites()->getSiteById($id);
}

/**
* @var int|null ID
*/
Expand Down Expand Up @@ -104,6 +111,22 @@ class Site extends Model
*/
private ?string $_language = null;

/**
* @inheritdoc
*/
public function getId(): string|int|null
{
return $this->id;
}

/**
* @inheritdoc
*/
public function getUiLabel(): string
{
return Craft::t('site', $this->getName());
}

/**
* Returns the site’s name.
*
Expand Down Expand Up @@ -264,7 +287,7 @@ public function attributes(): array
*/
public function __toString(): string
{
return Craft::t('site', $this->getName()) ?: static::class;
return $this->getUiLabel() ?: static::class;
}

/**
Expand Down

0 comments on commit 2b4b857

Please sign in to comment.