Skip to content

Commit

Permalink
Block rendering templates in backend
Browse files Browse the repository at this point in the history
Injected `RequestStack` and `ScopeMatcher` into several Grid element classes and added checks to bypass rendering when a backend request is detected. This prevents unnecessary output in the backend context, improving efficiency and clarity.
  • Loading branch information
stefansl committed Oct 16, 2024
1 parent a01e922 commit e3b6320
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Element/GridColumnStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Contao\ContentModel;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\DependencyInjection\Attribute\AsContentElement;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -31,8 +33,18 @@
#[AsContentElement(type: 'cp_column_start', category: 'cp_grid', template: 'ce_grid_column_start')]
class GridColumnStart extends AbstractContentElementController
{
public function __construct(
readonly RequestStack $requestStack,
readonly ScopeMatcher $scopeMatcher,
) {
}

protected function getResponse(Template $template, ContentModel $model, Request $request): Response
{
if ($this->scopeMatcher->isBackendRequest()) {
return new Response('');
}

return $template->getResponse();
}
}
19 changes: 18 additions & 1 deletion src/Element/GridColumnStop.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Contao\ContentModel;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\DependencyInjection\Attribute\AsContentElement;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -28,11 +30,26 @@
* @author Stefan Schulz-Lauterbach <[email protected]>
*/

#[AsContentElement('cp_column_stop', 'cp_grid', template: 'ce_grid_column_stop')]
#[AsContentElement(type: 'cp_column_stop', category: 'cp_grid', template: 'ce_grid_column_stop')]
class GridColumnStop extends AbstractContentElementController
{
public function __construct(
readonly RequestStack $requestStack,
readonly ScopeMatcher $scopeMatcher,
) {
}

protected function getResponse(Template $template, ContentModel $model, Request $request): Response
{
if ($this->scopeMatcher->isBackendRequest()) {
return new Response('');
}

$parentKey = ($model->ptable ?: 'tl_article') . '__' . $model->pid;
if (isset($GLOBALS['TL_CP_GRID'][$parentKey]) && !$GLOBALS['TL_CP_GRID'][$parentKey]['active']) {
$GLOBALS['TL_CP_GRID'][$parentKey]['active'] = true;
}

return $template->getResponse();
}
}
12 changes: 12 additions & 0 deletions src/Element/GridStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Contao\ContentModel;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\DependencyInjection\Attribute\AsContentElement;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -31,9 +33,19 @@
#[AsContentElement(type: 'cp_grid_start', category: 'cp_grid', template: 'ce_grid_start')]
class GridStart extends AbstractContentElementController
{
public function __construct(
readonly RequestStack $requestStack,
readonly ScopeMatcher $scopeMatcher,
) {
}

protected function getResponse(Template $template, ContentModel $model, Request $request): Response
{

if ($this->scopeMatcher->isBackendRequest()) {
return new Response('');
}

$template->gridClasses = '';

$parentKey = ($model->ptable ?: 'tl_article') . '__' . $model->pid;
Expand Down
11 changes: 11 additions & 0 deletions src/Element/GridStop.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Contao\ContentModel;
use Contao\CoreBundle\Controller\ContentElement\AbstractContentElementController;
use Contao\CoreBundle\DependencyInjection\Attribute\AsContentElement;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

use const E_USER_WARNING;
Expand All @@ -33,8 +35,17 @@
#[AsContentElement(type: 'cp_grid_stop', category: 'cp_grid', template: 'ce_grid_stop')]
class GridStop extends AbstractContentElementController
{
public function __construct(
readonly RequestStack $requestStack,
readonly ScopeMatcher $scopeMatcher,
) {
}

protected function getResponse(Template $template, ContentModel $model, Request $request): Response
{
if ($this->scopeMatcher->isBackendRequest()) {
return new Response('');
}

$parentKey = ($model->ptable ?: 'tl_article') . '__' . $model->pid;

Expand Down

0 comments on commit e3b6320

Please sign in to comment.