Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/4.0.2' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Aug 17, 2022
2 parents 9c64f92 + ef44c48 commit 76e91fb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## 4.0.2 - 2022.09.17
### 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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 16 additions & 9 deletions src/services/EagerBeaverService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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];
Expand Down
14 changes: 7 additions & 7 deletions src/variables/EagerBeaverVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}
Expand Down

0 comments on commit 76e91fb

Please sign in to comment.