eZ Publish 5 Tip: How to make a print preview page or any other pagelayout override

by Ivo Lukač -
eZ Publish 5 Tips

There is a lot to learn about new eZ Publish 5 stack. The reason is simple: new architecture and new frameworks are being used. Lot of legacy knowledge is going to be deprecated, many tips & tricks you know now are no longer usable. The good thing is that the new platform is much more powerful simply because it uses Symfony framework. This blog post gives a perfect example on how to deal with problems when trying to use the new stack in your project.

The problem

So we started an eZ Publish 5 based implementation. We implemented the pagelayout in new template engine Twig with 4 blocks for head, header, content and footer sections. We created a Layout Controller to fetch the data needed, e.g. menu links. You can find this kind of example in the eZDemoBundle.

Then we got to a problem. To implement the print preview feature we needed a separate pagelayout template. The old way was to use Layout module with Set view by adding "/layout/set/print" at the beginning of some eZ resource. It doesn't work any more because the output of that legacy module is going to use the new default pagelayout.html.twig, and this is exactly what we didn't want.

So we decided to try it in a new Symfony/eZPublish5 kind of way.

The solution

In our Layout controller a new action was added. It simply executed the viewLocation method which is actually executed by standard eZ View Controller. And then we sent the result to a specific print pagelayout template.

Controller/LayoutController.php

/**
 * Action for rendering print page
 */
public function printAction($locationId)
{
    $response = $this->get( 'ez_content' )->viewLocation( $locationId, 'full', false, array());

    return $this->render(
        'YourBundle::pagelayout_print.html.twig',
        array(
            'content' => $response->getContent()
        )
    );
} 

To put the new action in action we added new route rule which connected the url "/print/{locationId}" with the code above.

Resources/config/routing.yml

your_route_for_print:
    path:      /print/{locationId}
    defaults:  { _controller: YourBundle:Layout:print }
    requirements:
        locationId:  \d+

Last step is using the template extending feature from Twig to inherit the default pagelayout and replace the blocks with print specific stuff.

Resources/views/pagelayout_print.html.twig

{% extends 'YourBundle::pagelayout.html.twig' %}

{% block head %}
    specific head and stylesheets
{% endblock %}

{% block header %}
    print specific header
{% endblock %}

{% block content %}
    {{ content|raw }}
{% endblock %}

{% block footer %}
    print specific footer
{% endblock %}

And thats it. Of course, the example could be a bit more complex. For instance, we could support full nice urls instead of just locationId, but this simple example is to show how we should approach problem solving with eZ Publish 5. Instead of trying to force using some old legacy features we should instead research how to solve it more elegantly by using Symfony.

Happy New eZ Publish 5 Year!

Comments

This site uses cookies. Some of these cookies are essential, while others help us improve your experience by providing insights into how the site is being used.

For more detailed information on the cookies we use, please check our Privacy Policy.

  • Necessary cookies enable core functionality. The website cannot function properly without these cookies, and can only be disabled by changing your browser preferences.