├── src ├── translations │ └── en │ │ └── eager-beaver.php ├── variables │ └── EagerBeaverVariable.php ├── twigextensions │ └── EagerBeaverTwigExtension.php ├── services │ └── EagerBeaverService.php ├── icon.svg └── EagerBeaver.php ├── LICENSE.md ├── CHANGELOG.md ├── composer.json └── README.md /src/translations/en/eager-beaver.php: -------------------------------------------------------------------------------- 1 | '{name} plugin loaded' 25 | ]; 26 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 nystudio107 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Eager Beaver Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | ## 4.0.2 - 2022.09.17 6 | ### Changed 7 | * Handle passing in `ElementQuery`'s and `Collection`'s ([#32](https://github.com/nystudio107/craft-eagerbeaver/issues/32)) 8 | 9 | ## 4.0.1 - 2022.08.18 10 | ### Changed 11 | * Add `allow-plugins` to `composer.json` to allow CI tests to work 12 | 13 | ### Fixed 14 | * Fixed an issue with the short syntax type declarations ([#31]https://github.com/nystudio107/craft-eagerbeaver/pull/31) 15 | 16 | ## 4.0.0 - 2022.06.01 17 | ### Added 18 | * Initial Craft CMS 4 release 19 | 20 | ## 4.0.0-beta.1 - 2022.03.11 21 | 22 | ### Added 23 | 24 | * Initial Craft CMS 4 compatibility 25 | 26 | ## 1.0.4 - 2018.07.30 27 | ### Changed 28 | * Code cleanup 29 | * Updated README.md with native Craft examples 30 | 31 | ## 1.0.3 - 2018.03.02 32 | ### Changed 33 | * Fixed deprecation errors from Craft CMS 3 RC13 34 | 35 | ## 1.0.2 - 2017.02.01 36 | ### Changed 37 | * Renamed the composer package name to `craft-eagerbeaver` 38 | 39 | ## 1.0.1 - 2017.12.06 40 | ### Changed 41 | * Updated to require craftcms/cms `^3.0.0-RC1` 42 | * Switched to `Craft::$app->view->registerTwigExtension` to register the Twig extension 43 | 44 | ## 1.0.0 - 2017-09-30 45 | ### Added 46 | - Initial release 47 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nystudio107/craft-eagerbeaver", 3 | "description": "Allows you to eager load elements from auto-injected Entry elements on demand from your templates.", 4 | "type": "craft-plugin", 5 | "version": "4.0.2", 6 | "keywords": [ 7 | "craft", 8 | "cms", 9 | "craftcms", 10 | "craft-plugin", 11 | "eager", 12 | "loading", 13 | "eager loading", 14 | "eager beaver" 15 | ], 16 | "support": { 17 | "docs": "https://nystudio107.com/docs/eager-beaver/", 18 | "issues": "https://nystudio107.com/plugins/eager-beaver/support", 19 | "source": "https://github.com/nystudio107/craft-eagerbeaver" 20 | }, 21 | "license": "MIT", 22 | "authors": [ 23 | { 24 | "name": "nystudio107", 25 | "homepage": "https://nystudio107.com" 26 | } 27 | ], 28 | "require": { 29 | "craftcms/cms": "^4.0.0" 30 | }, 31 | "config": { 32 | "allow-plugins": { 33 | "craftcms/plugin-installer": true, 34 | "yiisoft/yii2-composer": true 35 | }, 36 | "optimize-autoloader": true, 37 | "sort-packages": true 38 | }, 39 | "autoload": { 40 | "psr-4": { 41 | "nystudio107\\eagerbeaver\\": "src/" 42 | } 43 | }, 44 | "extra": { 45 | "name": "Eager Beaver", 46 | "handle": "eager-beaver", 47 | "class": "nystudio107\\eagerbeaver\\EagerBeaver" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/variables/EagerBeaverVariable.php: -------------------------------------------------------------------------------- 1 | eagerBeaverService->eagerLoadElements($elements, $with); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/) 2 | 3 | # DEPRECATED 4 | 5 | This Craft CMS 4.x plugin is no longer supported or maintained, but it is fully functional, and you may continue to use it as you see fit. The license also allows you to fork it and make changes as needed for legacy support reasons. 6 | 7 | Instead, use [Craft's native functionality](https://nystudio107.com/blog/cutting-the-cord-removing-plugins#plugin-1-eager-beaver) for eager loading, or with Craft 5 or later, use `.eagerly()` for [lazy eager loading](https://craftcms.com/docs/5.x/development/eager-loading.html#lazy-eager-loading). 8 | 9 | # Eager Beaver plugin for Craft CMS 4.x 10 | 11 | Allows you to eager load elements from auto-injected Entry elements on demand from your templates. 12 | 13 | ![Screenshot](./docs/docs/resources/img/plugin-logo.png) 14 | 15 | Related: [Eager Beaver for Craft 2.6.x](https://github.com/nystudio107/eagerbeaver) 16 | 17 | Learn More: [Speed up your Craft CMS Templates with Eager Loading](https://nystudio107.com/blog/speed-up-your-craft-cms-templates-with-eager-loading) 18 | ## Requirements 19 | 20 | This plugin requires Craft CMS 4.0.0 or later. 21 | 22 | ## Installation 23 | 24 | To install the plugin, follow these instructions. 25 | 26 | 1. Open your terminal and go to your Craft project: 27 | 28 | cd /path/to/project 29 | 30 | 2. Then tell Composer to load the plugin: 31 | 32 | composer require nystudio107/craft-eagerbeaver 33 | 34 | 3. Install the plugin via `./craft install/plugin eager-beaver` via the CLI, or in the Control Panel, go to Settings → Plugins and click the “Install” button for Eager Beaver. 35 | 36 | ## Documentation 37 | 38 | Click here -> [Eager Beaver Documentation](https://nystudio107.com/plugins/eager-beaver/documentation) 39 | 40 | ## Eager Beaver Roadmap 41 | 42 | Some things to do, and ideas for potential features: 43 | 44 | * Release it 45 | 46 | Brought to you by [nystudio107](https://nystudio107.com) 47 | -------------------------------------------------------------------------------- /src/twigextensions/EagerBeaverTwigExtension.php: -------------------------------------------------------------------------------- 1 | eagerLoadElements($elements, $with); 47 | }), 48 | ]; 49 | } 50 | 51 | /** 52 | * Eager-loads additional elements onto a given set of elements. 53 | * 54 | * @param ElementInterface|array $elements The root element models that should 55 | * be updated with the eager-loaded 56 | * elements 57 | * @param string|array $with Dot-delimited paths of the elements 58 | * that should be eager-loaded into the 59 | * root elements 60 | */ 61 | public function eagerLoadElements(ElementInterface|array $elements, array|string $with): void 62 | { 63 | EagerBeaver::$plugin->eagerBeaverService->eagerLoadElements($elements, $with); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/services/EagerBeaverService.php: -------------------------------------------------------------------------------- 1 | toArray(); 46 | } 47 | // Normalize ElementQuery's to an array of Elements 48 | if ($elements instanceof ElementQueryInterface) { 49 | $elements = $elements->all(); 50 | } 51 | // Bail if there aren't even any elements 52 | if (empty($elements)) { 53 | return; 54 | } 55 | // Normalize an individual element to an array of Elements 56 | if (!is_array($elements)) { 57 | $elements = [$elements]; 58 | } 59 | // We are assuming all of these elements are of the same type 60 | /** @var Element $element */ 61 | $element = $elements[0]; 62 | $elementType = $element::class; 63 | Craft::$app->elements->eagerLoadElements($elementType, $elements, $with); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 12 | 13 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/EagerBeaver.php: -------------------------------------------------------------------------------- 1 | EagerBeaverService::class, 66 | ]; 67 | 68 | parent::__construct($id, $parent, $config); 69 | } 70 | 71 | // Public Methods 72 | // ========================================================================= 73 | 74 | /** 75 | * @inheritdoc 76 | */ 77 | public function init(): void 78 | { 79 | parent::init(); 80 | self::$plugin = $this; 81 | 82 | // Add in our Twig extensions 83 | Craft::$app->view->registerTwigExtension(new EagerBeaverTwigExtension()); 84 | 85 | // Register our variables 86 | Event::on( 87 | CraftVariable::class, 88 | CraftVariable::EVENT_INIT, 89 | static function (Event $event): void { 90 | /** @var CraftVariable $variable */ 91 | $variable = $event->sender; 92 | $variable->set('eagerBeaver', EagerBeaverVariable::class); 93 | } 94 | ); 95 | 96 | Craft::info( 97 | Craft::t( 98 | 'eager-beaver', 99 | '{name} plugin loaded', 100 | ['name' => $this->name] 101 | ), 102 | __METHOD__ 103 | ); 104 | } 105 | } 106 | --------------------------------------------------------------------------------