/** * Sets $templateIdentifier to the content view. * This decorator only supports closures. * * Must throw a \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType exception if $templateIdentifier is invalid. * * @param \Closure $templateIdentifier * * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType */ public function setTemplateIdentifier($templateIdentifier) { if (!$templateIdentifier instanceof \Closure) { throw new InvalidArgumentType('templateIdentifier', '\\Closure', $templateIdentifier); } $this->contentView->setTemplateIdentifier($templateIdentifier); }
/** * Builds a ContentView object from $viewConfig. * * @param array $viewConfig * * @return ContentView */ protected function buildContentView(array $viewConfig) { $view = new ContentView(); $view->setConfigHash($viewConfig); if (isset($viewConfig['template'])) { $view->setTemplateIdentifier($viewConfig['template']); } if (isset($viewConfig['controller'])) { $view->setControllerReference(new ControllerReference($viewConfig['controller'])); } return $view; }
/** * Displays the list of blog_post * Note: This is a fully customized controller action, it will generate the response and call * the view. Since it is not calling the ViewControler we don't need to match a specific * method signature. * * @param \eZ\Publish\Core\MVC\Symfony\View\ContentView $view * @param \Symfony\Component\HttpFoundation\Request $request * * @return \Symfony\Component\HttpFoundation\Response */ public function listBlogPostsAction(ContentView $view, Request $request) { $viewParameters = $request->attributes->get('viewParameters'); // This could be changed to use dynamic parameters injection $languages = $this->getConfigResolver()->getParameter('languages'); // Using the criteria helper (a demobundle custom service) to generate our query's criteria. // This is a good practice in order to have less code in your controller. $criteria = $this->get('ezdemo.criteria_helper')->generateListBlogPostCriterion($view->getLocation(), $viewParameters, $languages); // Generating query $query = new Query(); $query->query = $criteria; $query->sortClauses = array(new SortClause\Field('blog_post', 'publication_date', Query::SORT_DESC, $languages[0])); // Initialize pagination. $pager = new Pagerfanta(new ContentSearchAdapter($query, $this->getRepository()->getSearchService())); $pager->setMaxPerPage($this->container->getParameter('ezdemo.blog.blog_post_list.limit')); $pager->setCurrentPage($request->get('page', 1)); $view->addParameters(['pagerBlog' => $pager]); // The template identifier can be set from the controller action $view->setTemplateIdentifier('eZDemoBundle:full:blog.html.twig'); return $view; }
/** * @dataProvider badTemplateIdentifierProvider * * @expectedException eZ\Publish\Core\Base\Exceptions\InvalidArgumentType * * @param $badTemplateIdentifier */ public function testSetTemplateIdentifierWrongType($badTemplateIdentifier) { $contentView = new ContentView(); $contentView->setTemplateIdentifier($badTemplateIdentifier); }
public function testGetControllerMatchedView() { $id = 123; $viewType = 'full'; $templateIdentifier = 'FooBundle:full:template.twig.html'; $customController = 'FooBundle::bar'; $this->request->attributes->add(array('_controller' => 'ez_content:viewLocation', 'locationId' => $id, 'viewType' => $viewType)); $this->viewBuilderRegistry->expects($this->once())->method('getFromRegistry')->will($this->returnValue($this->viewBuilderMock)); $viewObject = new ContentView($templateIdentifier); $viewObject->setControllerReference(new ControllerReference($customController)); $this->viewBuilderMock->expects($this->once())->method('buildView')->will($this->returnValue($viewObject)); $this->event->expects($this->once())->method('setController'); $this->controllerResolver->expects($this->once())->method('getController')->will($this->returnValue(function () { })); $this->controllerListener->getController($this->event); $this->assertEquals($customController, $this->request->attributes->get('_controller')); $expectedView = new ContentView(); $expectedView->setTemplateIdentifier($templateIdentifier); $expectedView->setControllerReference(new ControllerReference($customController)); $this->assertEquals($expectedView, $this->request->attributes->get('view')); }