Skip to content

Commit

Permalink
Respect the target site for :empty:/:notempty:
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jun 14, 2022
1 parent 87aef78 commit fe7ce94
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Fixed a bug where element types’ `actions()` methods were getting called for all `element-indexes/*` action requests.
- Fixed a bug where the `install` command would run non-interactively even if not all needed options were passed, resulting in an error after the database tables had been added. ([#11305](https://github.com/craftcms/cms/issues/11305))
- Fixed a viewport clipping bug on the control panel’s Login page. ([#11372](https://github.com/craftcms/cms/pull/11372))
- Fixed a bug where filtering an element query by a relational field using `:empty:`/`:notempty:` wasn’t factoring in the field’s “Which site should entries be related from?” setting properly.

### Security
- Environment-aware control panel fields no longer suggest environment variables that begin with `HTTP_`.
Expand Down
24 changes: 12 additions & 12 deletions src/fields/BaseRelationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use yii\base\Event;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
use yii\db\Expression;
use yii\validators\NumberValidator;

/**
Expand Down Expand Up @@ -502,7 +503,7 @@ public function modifyElementsQuery(ElementQueryInterface $query, $value)
->leftJoin(["elements_sites_$ns" => DbTable::ELEMENTS_SITES], [
'and',
"[[elements_sites_$ns.elementId]] = [[elements_$ns.id]]",
["elements_sites_$ns.siteId" => $query->siteId],
["elements_sites_$ns.siteId" => $this->_targetSiteId() ?? new Expression('[[elements_sites.siteId]]')],
])
->where("[[relations_$ns.sourceId]] = [[elements.id]]")
->andWhere([
Expand Down Expand Up @@ -1063,21 +1064,20 @@ protected function inputSiteId(ElementInterface $element = null)
*/
protected function targetSiteId(ElementInterface $element = null): int
{
if (Craft::$app->getIsMultiSite()) {
if ($this->targetSiteId) {
try {
return Craft::$app->getSites()->getSiteByUid($this->targetSiteId)->id;
} catch (SiteNotFoundException $exception) {
Craft::warning($exception->getMessage(), __METHOD__);
}
}
return $this->_targetSiteId() ?? $element->siteId ?? Craft::$app->getSites()->getCurrentSite()->id;
}

if ($element !== null) {
return $element->siteId;
private function _targetSiteId(): ?int
{
if ($this->targetSiteId && Craft::$app->getIsMultiSite()) {
try {
return Craft::$app->getSites()->getSiteByUid($this->targetSiteId)->id;
} catch (SiteNotFoundException $exception) {
Craft::warning($exception->getMessage(), __METHOD__);
}
}

return Craft::$app->getSites()->getCurrentSite()->id;
return null;
}

/**
Expand Down

0 comments on commit fe7ce94

Please sign in to comment.