2017 © Pedro Peláez
 

craft-plugin plus

Add services, behaviors, variables and twig extensions to CraftCms.

image

fvaldes33/plus

Add services, behaviors, variables and twig extensions to CraftCms.

  • Sunday, July 22, 2018
  • by fvaldes33
  • Repository
  • 1 Watchers
  • 9 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 5 Versions
  • 7 % Grown

The README.md

Plus, (*1)

Craft PHP, (*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)

Features

  • Attach behaviors the Craft way and clean up your templates.
  • Eager load what you need.
  • Create services that are available both in templates and php.

Installation

To install Plus, just follow these steps:, (*5)

  1. Upload the Plus/ folder to your craft/plugins/ folder.
  2. Go to Settings > Plugins from your Craft control panel.
  3. Enable the Plus plugin and DONE.

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)

How does it work?

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)

Usage

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)

Example

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)

Feature Requests & Issues

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)

Feature Roadmap

As we approach Craft3 and the difficulties of multisite we will be making improvements as well to support any known changes., (*24)

The Versions

22/07 2018

v2.x-dev

2.9999999.9999999.9999999-dev

Add services, behaviors, variables and twig extensions to CraftCms.

  Sources   Download

The Requires

  • php >=7.0.0

 

by Franco Valdes

06/11 2017

2.0.3

2.0.3.0

Add services, behaviors, variables and twig extensions to CraftCms.

  Sources   Download

The Requires

  • php >=7.0.0

 

by Franco Valdes

06/11 2017

2.0.2

2.0.2.0

Add services, behaviors, variables and twig extensions to CraftCms.

  Sources   Download

The Requires

  • php >=7.0.0

 

by Franco Valdes

04/11 2017

dev-master

9999999-dev

  Sources   Download

04/11 2017

2.0.1

2.0.1.0

Add services, behaviors, variables and twig extensions to CraftCms.

  Sources   Download

The Requires

  • php >=7.0.0

 

by Franco Valdes