Skip to content

Commit

Permalink
Fix eager-loading Matrix blocks for drafts/revisions
Browse files Browse the repository at this point in the history
Fixes #5031
Fixes #5065
Fixes #5027
  • Loading branch information
brandonkelly committed Oct 12, 2019
1 parent 9480685 commit 40d20b6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Added
- Added the `allowOwnerDrafts` and `allowOwnerRevisions` Matrix block query params.
- Added the ability to specify whether to reset project config before individual tests are run. ([#5072](https://github.com/craftcms/cms/pull/5072))
- Added `craft\test\Craft::resetProjectConfig()`

Expand All @@ -11,6 +12,7 @@
- Fixed a bug where the image thumbnail would not be refreshed after using the image editor directly inside an Assets field. ([#4212](https://github.com/craftcms/cms/issues/4212))
- Fixed a bug where the `index-assets` command would bail as soon as it came across a file with a disallowed file extension. ([#5086](https://github.com/craftcms/cms/issues/5086))
- Fixed a bug where user email changes were going through email verification even if someone with permission to administrate users was making the change. ([#5088](https://github.com/craftcms/cms/issues/5088))
- Fixed a bug where it wasn’t possible to eager-load Matrix blocks on a draft. ([#5031](https://github.com/craftcms/cms/issues/5031))

## 3.3.9 - 2019-10-10

Expand Down
76 changes: 69 additions & 7 deletions src/elements/db/MatrixBlockQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ class MatrixBlockQuery extends ElementQuery
*/
public $ownerSiteId;

/**
* @var bool|null Whether the owner elements can be drafts.
* @used-by allowOwnerDrafts()
* @since 3.3.10
*/
public $allowOwnerDrafts;

/**
* @var bool|null Whether the owner elements can be revisions.
* @used-by allowOwnerRevisions()
* @since 3.3.10
*/
public $allowOwnerRevisions;

/**
* @var int|int[]|null The block type ID(s) that the resulting Matrix blocks must have.
* ---
Expand Down Expand Up @@ -247,6 +261,48 @@ public function owner(ElementInterface $owner)
return $this;
}

/**
* Narrows the query results based on whether the Matrix blocks’ owners are drafts.
*
* Possible values include:
*
* | Value | Fetches {elements}…
* | - | -
* | `true` | which can belong to a draft.
* | `false` | which cannot belong to a draft.
*
* @param bool|null $value The property value
* @return static self reference
* @uses $allowOwnerDrafts
* @since 3.3.10
*/
public function allowOwnerDrafts($value = true)
{
$this->allowOwnerDrafts = $value;
return $this;
}

/**
* Narrows the query results based on whether the Matrix blocks’ owners are revisions.
*
* Possible values include:
*
* | Value | Fetches {elements}…
* | - | -
* | `true` | which can belong to a revision.
* | `false` | which cannot belong to a revision.
*
* @param bool|null $value The property value
* @return static self reference
* @uses $allowOwnerDrafts
* @since 3.3.10
*/
public function allowOwnerRevisions($value = true)
{
$this->allowOwnerRevisions = $value;
return $this;
}

/**
* Narrows the query results based on the Matrix blocks’ block types.
*
Expand Down Expand Up @@ -393,14 +449,20 @@ protected function beforePrepare(): bool
}

// Ignore revision/draft blocks by default
if (!$this->id && !$this->ownerId) {
$allowOwnerDrafts = $this->allowOwnerDrafts ?? ($this->id || $this->ownerId);
$allowOwnerRevisions = $this->allowOwnerRevisions ?? ($this->id || $this->ownerId);

if (!$allowOwnerDrafts || !$allowOwnerRevisions) {
// todo: we will need to expand on this when Matrix blocks can be nested.
$this->subQuery
->innerJoin(Table::ELEMENTS . ' owners', '[[owners.id]] = [[matrixblocks.ownerId]]')
->andWhere([
'owners.draftId' => null,
'owners.revisionId' => null,
]);
$this->subQuery->innerJoin(Table::ELEMENTS . ' owners', '[[owners.id]] = [[matrixblocks.ownerId]]');

if (!$allowOwnerDrafts) {
$this->subQuery->andWhere(['owners.draftId' => null]);
}

if (!$allowOwnerRevisions) {
$this->subQuery->andWhere(['owners.revisionId' => null]);
}
}

return parent::beforePrepare();
Expand Down
6 changes: 5 additions & 1 deletion src/fields/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,11 @@ public function getEagerLoadingMap(array $sourceElements)
return [
'elementType' => MatrixBlock::class,
'map' => $map,
'criteria' => ['fieldId' => $this->id]
'criteria' => [
'fieldId' => $this->id,
'allowOwnerDrafts' => true,
'allowOwnerRevisions' => true,
]
];
}

Expand Down

0 comments on commit 40d20b6

Please sign in to comment.