Skip to content

Commit 3db93e8

Browse files
authored
Merge pull request #16367 from craftcms/feature/ability-to-set-request-site
Ability to specify the site for a request
2 parents 92cdb09 + 6c260ef commit 3db93e8

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG-WIP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
## Development
1010
- Added the `primarySite` global Twig variable. ([#16370](https://github.com/craftcms/cms/discussions/16370))
1111
- The `duration` Twig filter now has a `language` argument. ([#16332](https://github.com/craftcms/cms/pull/16332))
12+
- Added support for specifying the current site via an `X-Craft-Site` header set to a site ID or handle. ([#16367](https://github.com/craftcms/cms/pull/16367))
1213
- Deprecated the `ucfirst` Twig filter. `capitalize` should be used instead.
1314

1415
## Extensibility

src/config/app.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@
183183
],
184184
'sites' => [
185185
'class' => craft\services\Sites::class,
186-
'currentSite' => craft\helpers\App::env('CRAFT_SITE'),
187186
],
188187
'i18n' => [
189188
'class' => craft\i18n\I18N::class,

src/web/Request.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use craft\helpers\StringHelper;
1818
use craft\models\Site;
1919
use craft\services\Sites;
20+
use yii\base\InvalidArgumentException;
2021
use yii\base\InvalidConfigException;
2122
use yii\db\Exception as DbException;
2223
use yii\di\Instance;
@@ -1516,6 +1517,20 @@ private function _requestedSite(?int &$siteScore = null): Site
15161517
return $site;
15171518
}
15181519

1520+
// Is CRAFT_SITE or X-Craft-Site present?
1521+
$siteId = App::env('CRAFT_SITE') ?? $this->getHeaders()->get('X-Craft-Site');
1522+
if ($siteId !== null) {
1523+
if (is_numeric($siteId)) {
1524+
$site = $this->sites->getSiteById($siteId, false);
1525+
} else {
1526+
$site = $this->sites->getSiteByHandle($siteId, false);
1527+
}
1528+
if (!$site && Craft::$app->getIsInstalled() && !Craft::$app->getUpdates()->getIsCraftUpdatePending()) {
1529+
throw new InvalidArgumentException("Invalid site: $siteId");
1530+
}
1531+
return $site;
1532+
}
1533+
15191534
$sites = $this->sites->getAllSites(false);
15201535

15211536
if (empty($sites)) {

0 commit comments

Comments
 (0)