Skip to content

Commit

Permalink
Start support for relation queries in Craft 5.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Aug 9, 2024
1 parent c8a9b9f commit 068f0d2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 254 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Fixed

- Fixed the nested element type view in the Blitz Diagnostics utility.
- Fixed the nested element type count displayed in the Blitz Diagnostics utility.

## 5.6.2 - 2024-08-05

Expand Down
12 changes: 12 additions & 0 deletions src/helpers/ElementQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ public static function getNormalizedElementQueryIdParam(mixed $value): mixed
return $value;
}

/**
* Returns whether the element query has numeric IDs that may be related element IDs.
*/
public static function hasRelatedElementIds(ElementQuery $elementQuery): bool
{
if (!is_array($elementQuery->id)) {
return false;
}

return ArrayHelper::isNumeric($elementQuery->id);
}

/**
* Returns whether the element query has fixed IDs.
*/
Expand Down
10 changes: 9 additions & 1 deletion src/services/GenerateCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ public function addElementQuery(ElementQuery $elementQuery): void
return;
}

// Don’t proceed if the query has related element IDs, but add them in case this is a relation field query so that disabled elements will trigger a refresh whenever enabled. Required as of Craft 5.3.0.
// https://github.com/putyourlightson/craft-blitz/issues/555
if (ElementQueryHelper::hasRelatedElementIds($elementQuery)) {
$this->generateData->addElementIds($elementQuery->id);

return;
}

// Don’t proceed if the query has fixed IDs or slugs
if (ElementQueryHelper::hasFixedIdsOrSlugs($elementQuery)) {
return;
Expand Down Expand Up @@ -289,7 +297,7 @@ public function addElementQuery(ElementQuery $elementQuery): void
return;
}

// Don’t proceed if this is a relation field query
// Don’t proceed if this is a relation field query. Required to support relations saved prior to Craft 5.3.0.
if (ElementQueryHelper::isRelationFieldQuery($elementQuery)) {
$this->addRelatedElementIds($elementQuery);

Expand Down
8 changes: 8 additions & 0 deletions src/services/HintsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public function checkElementQuery(ElementQuery $elementQuery): void
return;
}

// TODO: Figure out how to add field hints for related element IDs.
// Required as of Craft 5.3.0.
if (ElementQueryHelper::hasRelatedElementIds($elementQuery)) {
//$this->addFieldHint($field->id);
return;
}

// Required to support relations saved prior to Craft 5.3.0.
if (!ElementQueryHelper::isRelationFieldQuery($elementQuery)) {
return;
}
Expand Down
Loading

0 comments on commit 068f0d2

Please sign in to comment.