Behaviors
You are viewing documentation for an unreleased version of Craft CMS. Please be aware that the material is changing frequently and may be incomplete or inaccurate, and links may point back to older versions.
There is no direct equivalent to Yii’s behavior (opens new window) system, but classes that use Illuminate\Support\Traits\Macroable can be extended at runtime using macros (opens new window).
Anything that extends our base component class (CraftCms\Cms\Component\Component) is “macroable.”
Define macros from your plugin’s bootPlugin() method:
use CraftCms\Cms\Site\Data\Site;
public function bootPlugin()
{
Site::macro('isB2b', function() {
return str_contains($this->handle, 'biz');
});
}
Then, wherever you’re using a site…
{{ currentSite.isB2b() ? 'Welcome, Mr. Business!' : 'Hey, bud!' }}
Laravel does not have an instance-level event system, but you can colocate multiple event handlers in a subscriber (opens new window).