Skip to content

Commit

Permalink
Refactored custom backend view implementations.
Browse files Browse the repository at this point in the history
This implementation gets rid of the route attribute 'custom_backend_view = true'.
It introduces a new twig function to wrap specific content blocks with the be_main
template of the Contao backend. This way is more intuitive and a less explicit approach.
  • Loading branch information
sheeep committed Jan 23, 2017
1 parent a7f7ea4 commit 3c06d92
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 53 deletions.
32 changes: 0 additions & 32 deletions src/EventListener/CustomBackendViewListener.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/Resources/config/listener.yml
Expand Up @@ -146,8 +146,3 @@ services:
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

contao.listener.custom_backend_view:
class: Contao\CoreBundle\EventListener\CustomBackendViewListener
tags:
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }
8 changes: 8 additions & 0 deletions src/Resources/config/services.yml
Expand Up @@ -117,3 +117,11 @@ services:
- "_contao_fe_attributes"
calls:
- ["setName", ["contao_frontend"]]

contao.twig.base_template_wrapper:
class: Contao\CoreBundle\Twig\Extension\ContaoBaseTemplateExtension
arguments:
- "@request_stack"
public: false
tags:
- { name: twig.extension }
24 changes: 8 additions & 16 deletions src/Resources/views/Backend/be_page.html.twig
@@ -1,17 +1,9 @@
{% set _panel = block('panel') %}
{% set _errors = block('errors') %}
{% set _main = block('main') %}
{% set _headline = block('headline') %}
{% set _error = block('error') %}

{% if _panel is not empty %}
<div class="tl_panel">
{{ _panel | raw }}
</div>
{% endif %}

{% if _errors is not empty %}
<p class="tl_gerror">{{ _errors | raw }}</p>
{% endif %}

<div class="tl_listing_container">
{% block content %}
{% endblock %}
</div>
{{ contao_base_template_wrapper({
main: _main,
error: _error,
headline: _headline
}) | raw }}
49 changes: 49 additions & 0 deletions src/Twig/Extension/ContaoBaseTemplateExtension.php
@@ -0,0 +1,49 @@
<?php

namespace Contao\CoreBundle\Twig\Extension;

use Contao\BackendRoute;
use Symfony\Component\HttpFoundation\RequestStack;

class ContaoBaseTemplateExtension extends \Twig_Extension
{
protected $requestStack;

public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}

public function getFunctions()
{
return [
new \Twig_SimpleFunction('contao_base_template_wrapper', [$this, 'wrapWithBaseTemplate'])
];
}

public function wrapWithBaseTemplate(array $blocks = [])
{
$scope = $this->requestStack->getCurrentRequest()->attributes->get('_scope');

if ('backend' !== $scope) {
return '';
}

$backendRoute = new BackendRoute();
$backendTemplate = $backendRoute->getBaseTemplate();

foreach ($blocks as $key => $content) {
$backendTemplate->{$key} = $content;
}

$response = $backendRoute->run();

return $response->getContent();
}

public function getName()
{
return 'base_template_wrapper';
}

}

0 comments on commit 3c06d92

Please sign in to comment.