Global navigation

   Documentation Center
   eZ Studio & eZ Platform
     User Manual
     Technical Manual
     Glossary
   eZ Publish 4.x / legacy

 
eZ Publish (5.x)

eZ Publish 5.x | For eZ Platform & eZ Studio topics see Technical manual and User manual, for eZ Publish 4.x and Legacy topics see eZ Publish legacy

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt
Note
titleVersion compatibility

This recipe is compatible with eZ Publish 5.2 / 2013.07

 

Table of Contents

Enhanced views for Content/Location

In some cases, displaying a content item/location via the built-in ViewController is not sufficient and will lead you to do many sub-requests in order to access different parameters.

...

In those cases, you may want to use your own controller to display the current content/location instead of using the built-in ViewController.

Description

This feature covers 2 general use cases:

  • Lets you configure a custom controller with the configured matcher rules.
  • Lets you override the built-in view controller in a clean way.

Matching custom controllers

This is possible with the following piece of configuration:

...

The only requirement here is that your action method has a similar signature than ViewController::viewLocation() or ViewController::viewContent() (depending on what you're matching of course). However, note that all arguments are not mandatory since Symfony is clever enough to know what to inject in your action method. Hence you're not forced to mimic the ViewController's signature strictly. For example, if you omit $layout and $params arguments, it will be still valid. Symfony will just avoid to inject them in your action method.

Original ViewController signatures

Code Block
languagephp
titleviewLocation() signature
/**
 * Main action for viewing content through a location in the repository.
 *
 * @param int $locationId
 * @param string $viewType
 * @param boolean $layout
 * @param array $params
 *
 * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
 * @throws \Exception
 *
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function viewLocation( $locationId, $viewType, $layout = false, array $params = array() )

...

Warning
titleWarning on caching

Using your own controller, it is your responsibility to define cache rules, like for every custom controller !

So don't forget to set cache rules and the appropriate X-Location-Id header in the returned Response object.

See built-in ViewController for more details on this.

Examples

Enriching built-in ViewController

This example shows how to use a custom controller to enrich the final configured view template. Your controller will here forward the request to the built-in ViewController with some additional parameters.

...

Code Block
titlearticle_test.html.twig
{% extends noLayout ? viewbaseLayout : "eZDemoBundle::pagelayout.html.twig" %}

{% block content %}
    <h1>{{ ez_render_field( content, 'title' ) }}</h1>
    <h2>{{ myCustomVariable }}</h2>
    {{ ez_render_field( content, 'body' ) }}
{% endblock %}

Using a custom controller to get full control

This example shows you how to configure and use your own controller to handle a location.

...

Code Block
titlecustom_controller_folder.html.twig
{% extends "eZDemoBundle::pagelayout.html.twig" %}

{% block content %}
<h1>{{ ez_render_field( content, 'title' ) }}</h1>
    <h1>{{ foo }}</h1>
    <ul>
    {% for os in osTypes %}
        <li>{{ os }}</li>
    {% endfor %}
    </ul>
{% endblock %}

Overriding the built-in ViewController

One other way to keep control on what is passed to the view is to use your own controller instead of the built-in ViewController.

...