Add services, behaviors, variables and twig extensions to CraftCms.
, (*1)
, (*2)
Plus (for CraftCMS) adds an easy to use way of attaching behaviors to your elements using Craft's own $element->attachBehavior() method. In addition Plus adds a useful global variable 'plus' that will expose all available services you create within the Plus\Services namespace., (*3)
It's maintained by Franco Valdes., (*4)
To install Plus, just follow these steps:, (*5)
On install, Plus will copy over the install/plus directory into your root directory. This allows you to create your own behaviors and services outside of the plugin itself as all it does is attach the behaviors you create., (*6)
Folder Structure: - plus - Behaviors - Categories - BaseCategoryBehavior.php - Entries - BaseEntryBehavior.php - HomepageBehavior.php - Globals - BaseGlobalsBehavior.php - MatrixBlocks - BaseMatrixBlocksBehavior.php - Traits - ExampleAssetTrait.php - Services - LogService.php **, (*7)
Log Service is for basic debugging. Usage: - Twig: {{ plus.log.write(key, value) }} - PHP: craft()->plus->log('hello', 'world'); - PHP: plus()->log('hello', 'world');, (*8)
In addition, now the plus() function has access to all services in the Plus\Services namespace by simply calling it after plus. If outside the craft namespace be sure to add use function Craft\plus
.
- PHP
- plus()->[serviceName]->serviceMethod;, (*9)
It is pretty simple. Recently CraftCms gave us the power to attach behaviors to our element instances. With this ability also came the ability to eager load related data to help with performance. Due to this update we decided to simplify how these behaviors are used and also keep them out of your twig templates. To get a bit more technical, on crafts "onPopulateElement" event, we scan and attach behviors matching some of your elements criteria., (*10)
On the services end, we expose a plus global twig variable that has access to all created services inside the plus/Services directory. Take a look at our LogService example for more details on how that is used., (*11)
CraftCMS ships with a "Homepage" section so we've decided to ship with an example "HomepageBehavior" so that you can see how it works. It is quite straight forward, for each section you wish to attach behaviors to simply create a behavior with the same name (+ the word behavior) inside it's element types directory., (*12)
==UPDATE | 2016-10-26==, (*13)
Every public method inside this newly created behavior will be available in your templates attached to the handle you have attached it to. Take a look at this https://craftcms.com/classreference/etc/behaviors/BaseBehavior for more information about behaviors., (*14)
In short, behaviors are methods that will be attached to your [entry, matrix, category, globals] models on the fly. Saying that if you need a {% set array = entry.loadsomethingfaster %} in your template you can easily add that method inside the element you are trying to use it on. For a more resuable case, use traits or add methods to the Base[ElementType]Behavior.php instead., (*15)
New example added to HomepageBehavior.php to show how to eager load fields across channels and matrix block fields., (*16)
==UPDATE | 2016-12-19==, (*17)
Adding function plus that returns the craft plus service component. This will allow you to use plus services through the plus service __get() method. Usage:, (*18)
to call the write method inside the log service class you would, (*19)
use function Craft\plus plus()->log->write() plus()->[serviceName]->[serviceMethod]();
Just like the plus variable, plus() has access to every class inside the Plus\Services namespace., (*20)
Your blog channel section has a gallery and you want to eager load the assets needed to populate that page. Perfect! You would create a BlogBehavior.php file (extend the BaseEntryBehavior as showed on the Homepage example) and add a method to help you eager load those assets. In the hopes of clean and reusable code, if any of these behaviors are common, create a Trait and just use that OR you can add a method directly to the Basebehavior class itself., (*21)
To learn about eager loading check this out https://craftcms.com/docs/templating/eager-loading-elements., (*22)
If you need a feature, let me know and I'll add it as soon as reasonably possible. Plus is actively maintained, and I accept relevant, feature-adding pull requests. If you encounter any issues, please open an issue and I'll work with you and patch the problem. Thanks!, (*23)
As we approach Craft3 and the difficulties of multisite we will be making improvements as well to support any known changes., (*24)