To do this, get the ContentView object and add it what you need as params :.
$contentView = $event->getContentView();
Returns the location when applicable (viewing a location basically)
if ( $contentView->hasParameter( 'location' ) )
$location = $contentView->getParameter( 'location' );
Content is always available.
$content = $contentView->getParameter( 'content' );
Set your own variables that will be exposed in the template
The following will expose "foo" and "complex" variables in the view template.
$contentView->addParameters(
array(
'foo' => 'bar',
'complex' => $someObject
)
);
public function onPreContentView( PreContentViewEvent $event ) { $contentView = $event->getContentView(); $contentView->addParameters( array( 'foo' => 'bar', 'osTypes' => array( 'osx', 'linux', 'win' ) ) ); }
/** * If the event's view has a defined template, sets the view's template identifier, * and the 'pagelayout' parameter. * * @param PreContentViewEvent $event */ public function setUserRegistrationTemplates(PreContentViewEvent $event) { $view = $event->getContentView(); foreach ($this->viewTemplates as $viewClass => $template) { if ($view instanceof $viewClass) { $view->setTemplateIdentifier($template); $view->addParameters(['pagelayout' => $this->pagelayout]); } } }
/** * @param PreContentViewEvent $event */ public function onPreContentView(PreContentViewEvent $event) { $contentView = $event->getContentView(); /** @var \Truffo\eZContentDecoratorBundle\Decorator\ContentDecoratorFactory $contentDecoratorFactory */ $contentDecoratorFactory = $this->container->get('ezcontentdecorator.services.factory'); if ($contentView->hasParameter('location')) { $location = $contentView->getParameter('location'); /** @var \Truffo\eZContentDecoratorBundle\Decorator\ContentDecorator $contentDecorator */ $contentDecorator = $contentDecoratorFactory->getContentDecorator($location); $contentView->addParameters([$contentDecorator->getContentTypeIdentifier() => $contentDecorator, 'decorator' => $contentDecorator]); } }