Skip to content

Commit

Permalink
Fix for getRuntime on Symfony older than 3.4 (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa authored and OskarStark committed Jan 7, 2018
1 parent 8361fa8 commit 9e5fdbe
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/Controller/PageAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Command\DebugCommand;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -96,16 +99,8 @@ public function treeAction(Request $request = null)

$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();

// NEXT_MAJOR: remove bc check
// BC for Symfony < 3.2 where this runtime does not exist
$twig = $this->get('twig');
$theme = $this->admin->getFilterTheme();
if (!method_exists(AppVariable::class, 'getToken')) {
$twig->getExtension(FormExtension::class)->renderer->setTheme($formView, $theme);
} else {
$twig->getRuntime(TwigRenderer::class)->setTheme($formView, $theme);
}
$this->setFormTheme($formView, $theme);

return $this->render($this->admin->getTemplate('tree'), [
'action' => 'tree',
Expand Down Expand Up @@ -272,4 +267,28 @@ public function composeContainerShowAction(Request $request = null)
'page' => $block->getPage(),
]);
}

/**
* Sets the admin form theme to form view. Used for compatibility between Symfony versions.
*/
private function setFormTheme(FormView $formView, $theme)
{
$twig = $this->get('twig');

// BC for Symfony < 3.2 where this runtime does not exists
if (!method_exists(AppVariable::class, 'getToken')) {
$twig->getExtension(FormExtension::class)->renderer->setTheme($formView, $theme);

return;
}

// BC for Symfony < 3.4 where runtime should be TwigRenderer
if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
$twig->getRuntime(TwigRenderer::class)->setTheme($formView, $theme);

return;
}

$twig->getRuntime(FormRenderer::class)->setTheme($formView, $theme);
}
}

0 comments on commit 9e5fdbe

Please sign in to comment.