Product layer extensions

Question:

When should I extend the Frontastic domain models such as Frontastic\Common\ProductApiBundle\Domain\Variant and how should I do it?

Answer:

It depends on what you want to do and what properties or features that you're missing. It's possible to extend the domain objects like variant, product, and so on.

To set the values, you'll need to use our LifecycleDecorators. In these, you can assign the values from the dangerousInner* to the necessary properties, see our API Decorators article for more information.

The dangerousInner* properties will contain all the information that was returned by your commerce Backend. These then get stripped so they're not sent to the frontend, which is why you need a LifecycleDecorator to enrich your own variant. This is done for performance and security reasons (and also because certain objects aren't possible to encode as JSON).

Below is an example of how you could set a certain property inside a LifecycleDecorator which extends our product:

class ExtendedProduct extends Product
{
     /**
     * @var string
     */
     public $fancyProperty;
     public fancyVariantProperty;
}

use ProductApi\LifecycleEventDecorator\BaseImplementation;
class ProductWithFancyProperty extends BaseImplementation
{
     public function beforeProduct(
   	ProductApi $productApi,
  	$query,
  	string $mode = ProductApi::QUERY_SYNC
     ): ?array {
    	$query->loadDangerousInnerData = true;
	return null;
     }

     public function beforeQuery(
 	$productApi,
  	ProductQuery $query,
  	string $mode = ProductApi::QUERY_SYNC
     ): ?array {
	$query->loadDangerousInnerData = true;
	return null;
     }

     public function afterGetProduct(
	ProductApi $productApi,
	?Product $product
     ): ?Product {
        return $this->extendProduct($product);
     }

    public function afterQuery(
	$productApi,
	?Result $result
    ): ?Result {
        if ($result === null) {
            return null;
        }

        $result->items = array_map(
            function (Product $product) {
                return $this->extendProduct($product);
            },
            $result->items
        );

        return $result;
    }

    public function extendProduct(?Product $product)
    {
        if ($product === null) {
            return null;
        }

        $myExtendedProduct = new ExtendedProduct(get_object_vars($product));
        $myExtendedProduct->fancyProperty = $product->dangerousInnerProduct-->fancyProperty;
        $myExtendedProduct->fancyVariantProperty = $product->variants[0]        
		->dangerousInnerVariant['attributes']['fancyProperty'];

        return $myExtendedProduct;
    }
}

There are some methods that use Query as a parameter that contains the loadDangerousInner property. Those queries are ProductQuery, CategoryQuery, SingleProductQuery, and ProductTypeQuery. Based on the value of loadDangerousInner, the dangerousInner* will be set as part of the response object.

So, if you need to access the dangerousInner* you might want to set the value of loadDangerousInner as true as we've done in the above example.


‹Back

FAQs

Still need help? Contact us Contact us