1
votes

I need to override behavior of sylius_partial_product_latest route to have ability render template with additional parameter.

The simple example: at the different pages I need to show from 2 to 4 products at a time with carousel scrolling through 4-8 products. Now in the SyliusWebBundle:Frontend/Product:latest template I have:

{{ grid(products) }}

But I need in some cases:

{{ grid(products, 2) }}

In others:

{{ grid(products, 3) }}
{# or it can be 4 or any other number #}

Now in template I have only products var (it served by Resources mechanism), I can pass variables to the repository where I can add it to my products array, but it is not a good way. It can be accomplished by simple passing variables to the query:

{{ render(url('sylius_partial_product_latest', {'size': 3, 'limit': 2, 'template': 'SyliusWebBundle:Frontend/Product:latest.html.twig'})) }}

I need to have ability to pass variable size to template SyliusWebBundle:Frontend/Product:latest outside of products.

I tried override the ProductBundle and ResourceBundle... But overriding controllers with custom action (I do not want to override indexAction or showAction) led me nowhere. PHP doesn't hit my controller at all. Instead it hit showAction of ResourceBundle. And, yes, I overrode routing to point my controller.

If I override ProductController with new route and use it in my template it throws exception:

An exception has been thrown during the rendering of a template ("Controller "sylius.controller.product:partialAction" for URI "/partial" is not callable.") in SyliusWebBundle:Frontend/Product:show.html.twig at line 136.

When I change _controller to point my controller in standard Symfony manner I have next exception:

Catchable fatal error: Argument 1 passed to Sylius\Bundle\ResourceBundle\Controller\ResourceController::__construct() must be an instance of Sylius\Bundle\ResourceBundle\Controller\Configuration, none given, called in /Users/mihail/Sites/magazin/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php on line 77 and defined in /Users/mihail/Sites/magazin/vendor/sylius/sylius/src/Sylius/Bundle/ResourceBundle/Controller/ResourceController.php on line 63

1
This is a pagination problem ? Maybe you can try to use pagerfanta to do the job ? With the "MaxPerPage" value in your controllerHornth
It is not about pagination.Michael Sivolobov

1 Answers

2
votes

There are 2 ways that you could go with this..

Controller

You could extend the controller and pass the variable into your template the same way you could with any other controller.

Request

You could just pass the variable to your controller through the GET parameters as the request like your..

{{ render(url('sylius_partial_product_latest', {'size': 3, 'limit': 2, 'template': 'SyliusWebBundle:Frontend/Product:latest.html.twig'})) }}

.. and then get that in your template using the request object like..

{{ grid(product, app.request.get('size')) }} // with a default or null