Using the around listener

The around listeners are used when we want to change both the arguments and the returned values of an original method or add some behavior before and after an original method is called.

Looking back at the aroundGetAddToCartUrl listener method definition, you will see that it has four properties assigned in sequence—$subject, $proceed, $product, and $additional.

With the after method listener, the first property is always the $subject property, which contains the instance of the object type being observed and not the return value of the observed method. The second property is always the $proceed property of Closure. The properties following the $subject and $proceed match the properties of the observed getAddToCartUrl method in the sequential order too.

This simple rule used for transformation is as follows:

getAddToCartUrl($product, $additional = [])
aroundGetAddToCartUrl(
    $subject,
    Closure $proceed,
    $product,
    $additional = []
)

The around listener methods must have a return value. The return value is formed in such way that the parameters following the $closure parameter in the around listener method definition are passed to the $closure function call in a sequential order, as follows:

return $proceed($product, $additional);
//or
$result = $proceed($product, $additional);
return $result;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.128.129