How do I extend pipelines?

The payload data can be altered using the extend() method. This is typically done from within a Service Provider. If the code is unique to a particular store it could be placed in the boot() method of the AppServiceProvider. If it is to be included as part of a module, then it can be added to the setup method of the module's Service Provider.

For example, if a module needs to add an option to items to flag them as requiring gift wrap, the CartItemBuilder pipeline class should be extended in the module's ServiceProvider as shown below:

<?php

namespace Acme\MyModule;

use Aero\Cart\CartItemOption;
use Aero\Common\Providers\ModuleServiceProvider;
use Aero\Store\Pipelines\CartItemBuilder;

class ServiceProvider extends ModuleServiceProvider
{
    public function setup() 
    {
        CartItemBuilder::extend(function ($item) {
            if (request()->has('gift_wrap')) {
                $option = CartItemOption::create('Gift wrap');

                $item->addOption($option);
            }
        });
    }
}

To adjust a payload which is a simple string, ensure that the payload variable is passed by reference so that it can be directly modified:

\Aero\Store\Pipelines\ContentForHead::extend(function (&$content) {
    $content .= "";

    // or ...

    $content .= view('my_module::script-snippet');
});

Articles in this section

Was this article helpful?
0 out of 0 found this helpful