From 0e08e023329f9d49b54d1bb695cf23d7cc4705ba Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Tue, 16 Aug 2022 17:59:10 -0400 Subject: [PATCH 1/3] refactor: Handle passing in `ElementQuery`'s and `Collection`'s ([#32](https://github.com/nystudio107/craft-eagerbeaver/issues/32)) --- src/services/EagerBeaverService.php | 25 ++++++++++++++++--------- src/variables/EagerBeaverVariable.php | 14 +++++++------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/services/EagerBeaverService.php b/src/services/EagerBeaverService.php index 265763a..b600fb9 100644 --- a/src/services/EagerBeaverService.php +++ b/src/services/EagerBeaverService.php @@ -15,6 +15,8 @@ use craft\base\Component; use craft\base\Element; use craft\base\ElementInterface; +use craft\elements\db\ElementQueryInterface; +use Illuminate\Support\Collection; use function is_array; /** @@ -31,24 +33,29 @@ class EagerBeaverService extends Component /** * Eager-loads additional elements onto a given set of elements. * - * @param ElementInterface|array $elements The root element models that should - * be updated with the eager-loaded - * elements - * @param string|array $with Dot-delimited paths of the elements - * that should be eager-loaded into the - * root elements + * @param ElementInterface|ElementQueryInterface|Collection|array $elements The root + * element models that should be updated with the eager-loaded elements + * @param string|array $with Dot-delimited paths of the elements that should be + * eager-loaded into the root elements */ - public function eagerLoadElements(ElementInterface|array $elements, array|string $with): void + public function eagerLoadElements(ElementInterface|ElementQueryInterface|Collection|array $elements, array|string $with): void { + // Normalize Collections to an array of Elements + if ($elements instanceof Collection) { + $elements = $elements->toArray(); + } + // Normalize ElementQuery's to an array of Elements + if ($elements instanceof ElementQueryInterface) { + $elements = $elements->all(); + } // Bail if there aren't even any elements if (empty($elements)) { return; } - + // Normalize an individual element to an array of Elements if (!is_array($elements)) { $elements = [$elements]; } - // We are assuming all of these elements are of the same type /** @var Element $element */ $element = $elements[0]; diff --git a/src/variables/EagerBeaverVariable.php b/src/variables/EagerBeaverVariable.php index 2981517..d5ee447 100644 --- a/src/variables/EagerBeaverVariable.php +++ b/src/variables/EagerBeaverVariable.php @@ -12,6 +12,8 @@ namespace nystudio107\eagerbeaver\variables; use craft\base\ElementInterface; +use craft\elements\db\ElementQueryInterface; +use Illuminate\Support\Collection; use nystudio107\eagerbeaver\EagerBeaver; /** @@ -30,14 +32,12 @@ class EagerBeaverVariable /** * Eager-loads additional elements onto a given set of elements. * - * @param ElementInterface|array $elements The root element models that should - * be updated with the eager-loaded - * elements - * @param string|array $with Dot-delimited paths of the elements - * that should be eager-loaded into the - * root elements + * @param ElementInterface|ElementQueryInterface|Collection|array $elements The root + * element models that should be updated with the eager-loaded elements + * @param string|array $with Dot-delimited paths of the elements that should be + * eager-loaded into the root elements */ - public function eagerLoadElements(ElementInterface|array $elements, array|string $with): void + public function eagerLoadElements(ElementInterface|ElementQueryInterface|Collection|array $elements, array|string $with): void { EagerBeaver::$plugin->eagerBeaverService->eagerLoadElements($elements, $with); } From b2c4149abcb2d2c81042b32d1216f64f34f3b345 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Tue, 16 Aug 2022 17:59:27 -0400 Subject: [PATCH 2/3] chore: Version 4.0.2 --- CHANGELOG.md | 4 ++++ composer.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ff4d1e..6fc8371 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## 4.0.2 - UNRELEASED +### Changed +* Handle passing in `ElementQuery`'s and `Collection`'s ([#32](https://github.com/nystudio107/craft-eagerbeaver/issues/32)) + ## 4.0.1 - 2022.08.18 ### Changed * Add `allow-plugins` to `composer.json` to allow CI tests to work diff --git a/composer.json b/composer.json index 0eb6b2b..4871ea4 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft-eagerbeaver", "description": "Allows you to eager load elements from auto-injected Entry elements on demand from your templates.", "type": "craft-plugin", - "version": "4.0.1", + "version": "4.0.2", "keywords": [ "craft", "cms", From ef44c488da9c07a47f2fc6406996faf1af49b763 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 17 Aug 2022 11:57:32 -0400 Subject: [PATCH 3/3] chore: Version 4.0.2 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fc8371..4be28ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file. -## 4.0.2 - UNRELEASED +## 4.0.2 - 2022.09.17 ### Changed * Handle passing in `ElementQuery`'s and `Collection`'s ([#32](https://github.com/nystudio107/craft-eagerbeaver/issues/32))