Extension action traits

Similarly to the local action traits explored on the page before, extension action traits allow you to hook into an action method without overriding it directly thus maintaining the default trait logic. Moreover, since they're traits, they're easily reusable!

We'll continue using the output action for consistency. An output extension trait must make use of the OutputtableExtensionTrait like this:

<?php

namespace DeepWebSolutions\Plugins\MyTestPlugin\Actions;

use DeepWebSolutions\Framework\Foundations\Actions\Outputtable\OutputFailureException;
use DeepWebSolutions\Framework\Foundations\Actions\Outputtable\OutputtableExtensionTrait;

trait MyOutputExtensionTrait {
    use OutputtableExtensionTrait; // USE THIS
    
    protected function my_output_extension(): ?OutputFailureException {
        // do something
        
        return null;
    }
}

Just like with the local action trait, it's enough to "import" your extension trait into your class -- no need to use the default OutputtableTrait too. And just like with the local action traits, the concepts explained here apply to all defined actions (you just need to use the corresponding extension trait).

There is one thing you need to pay attention to -- the name of the extension method. It's no coincidence that the example above uses the method my_output_extension. The name of the method is derived from the name of the trait. Basically these are the rules:

  • If the trait has the suffix Trait, it is removed.

  • Before every capital letter, an underscore is added.

  • The string is turned to lowercase.

Therefore, since my example trait is named MyOutputExtensionTrait, the method must be named my_output_extension. If you misname it, it will simply be ignored.

The Foundations module comes with a few pre-built extension traits, but they usually don't make sense unless their context is fully understood. We will explore them on other pages.

Last updated