Skip to content

Commit

Permalink
Merge pull request #544 from kimausloos/master
Browse files Browse the repository at this point in the history
[NodeBundle] Allow more flexible entity controller handling
  • Loading branch information
Roderik van der Veer committed Jul 13, 2015
2 parents b232ed5 + ba77d4b commit 3d72165
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Kunstmaan/NodeBundle/EventListener/RenderContextListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kunstmaan\NodeBundle\EventListener;

use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\Templating\EngineInterface;

Expand All @@ -29,8 +30,13 @@ public function __construct(EngineInterface $templating, EntityManager $em)
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$nodeTranslation = $request->attributes->get('_nodeTranslation');
$response = $event->getControllerResult();
if ($response instanceof Response) { //if its a response, just continue
return;
}

$request = $event->getRequest();
$nodeTranslation = $request->attributes->get('_nodeTranslation');

if ($nodeTranslation) {
$entity = $request->attributes->get('_entity');
Expand Down Expand Up @@ -62,8 +68,14 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
$parameters = $renderContext;
}

// Sent the response here, another option is to let the symfony kernel.view listener handle it
$event->setResponse($this->templating->renderResponse($entity->getDefaultView(), $parameters));
if (is_array($response)) { //if the response is an array, merge with rendercontext
$parameters = array_merge($parameters, $response);
}

//set the rendercontext with all params as response, plus the template in the request attribs
//the SensioFrameworkExtraBundle kernel.view will handle everything else
$event->setControllerResult((array)$parameters);
$request->attributes->set('_template', $entity->getDefaultView());
}
}
}

0 comments on commit 3d72165

Please sign in to comment.