Is it possible to pass an entire object through TwigFunction in twig template?

36 Views Asked by At

I have a twig template and I need to retrieve some data. So I created a TwigFunction and I'm calling it from the template with an object in parameter but when in the function, the object is empty. If I pass a specific attribute (a string) of this object, it works. So I'm wondering if it's normal or not, because I didn't find anything saying that object are not ok.

Here is my function definition :

<?php

namespace Name\Space\Twig\Extensions;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

use Psr\Log\LoggerInterface;

class CustomProvider extends AbstractExtension
{
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('get_form_token', [$this, 'getFormToken'], ['is_safe' => ['html']])
        ];
    }

    public function getFormToken($order){
        $this->logger->error(json_encode($order));
        return json_encode($order);
    }
}

Thank you in advance

0

There are 0 best solutions below