Search code examples
phpdependency-injectionsonata-adminsymfony6

Render twig template in Sonata-Admin Bulk-Action: You cannot use the "renderView"


I created a Sonata Admin-Class which I also added a customized Batch-Action:

protected function configureBatchActions(array $actions): array
{
    $actions =  parent::configureBatchActions($actions);
    $actions['compareConfig'] = [
        'ask_confirmation' => false,
        'controller' => 'app.config_compare_controller::batchCompareConfigAction'
    ];

    return $actions;
}

The service-definition (services.yaml):

    app.config_compare_controller:
    class: App\Controller\DatabasesCompareController
    arguments: [ '@service_container' ]
    calls:
        - [ setContainer, [ '@service_container' ] ]

Controller (DatabasesCompareController):

public function batchCompareConfigAction(ProxyQueryInterface $query, AdminInterface $admin): ?Response {
...
dump($this->container->has('twig')); // => false
    return $this->render('Admin/Databases/config_compare.html.twig', [
        'content' => $content
    ]);

Due to the fact, that in another controller twig renders fine the following error-message confuses me:

You cannot use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle" (in vendor/symfony/framework-bundle/Controller/AbstractController.php)

Symfony: 6.0.11 Sonata-Admin: 4.17.0

What can I do to render my output?


Solution

  • I solved it myself: You have to add the twig-Enviroment as an argument.

       app.config_compare_controller:
            class: App\Controller\DatabasesCompareController
            arguments: [ '@twig' ]
            calls:
                - [ setContainer, [ '@service_container' ] ]