cms
cms copied to clipboard
[3.x]: Extend ElementQuery and being unable to modify the list of sites using siteId()
What happened?
Description
I've created a Widget that makes a query and wanted to filter by site, but I always get the default site in the query instead
$submissions = Submission::find()
->siteId($this->sites) // this does nothing
->siteIdOverride($this->sites) // this sets the siteId correct site list
->all();
class SubmissionQuery extends ElementQuery
{
/**
* Can't use siteId directly, since it will be overwritten when the prepare method is called
*
* @param array $value
* @return $this
*/
public function siteIdOverride(array $value): self
{
$this->sitesOverride = $value;
return $this;
}
protected function beforePrepare(): bool
{
$sites = $this->siteId; // has default site in it
$sitesOverride = $this->sitesOverride; // correct list of sites
$this->siteId($this->sitesOverride);
return parent::beforePrepare();
}
}
Expected behavior
If a site is already set, craft should perform the query with that siteId instead
Actual behavior
$siteId will be overwriten with the default site in the prepare statement
Craft CMS version
3.7.26
PHP version
8.1
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
-
Have you overridden your element type’s static isLocalized() method to return true?
If the element type isn’t localizable, its element queries will always stick to the primary site.