Skip to content

Commit 749ccf7

Browse files
committed
[WIP] backend template
1 parent 9d29e35 commit 749ccf7

File tree

3 files changed

+139
-109
lines changed

3 files changed

+139
-109
lines changed

Classes/Backend/Preview/ContainerPreviewRenderer.php

+6-106
Original file line numberDiff line numberDiff line change
@@ -12,122 +12,22 @@
1212
* of the License, or any later version.
1313
*/
1414

15-
use B13\Container\Backend\Grid\ContainerGridColumn;
16-
use B13\Container\Backend\Grid\ContainerGridColumnItem;
17-
use B13\Container\Backend\Service\NewContentUrlBuilder;
18-
use B13\Container\Domain\Factory\Exception;
19-
use B13\Container\Domain\Factory\PageView\Backend\ContainerFactory;
20-
use B13\Container\Events\BeforeContainerPreviewIsRenderedEvent;
21-
use B13\Container\Tca\Registry;
22-
use Psr\EventDispatcher\EventDispatcherInterface;
15+
2316
use TYPO3\CMS\Backend\Preview\StandardContentPreviewRenderer;
24-
use TYPO3\CMS\Backend\Utility\BackendUtility;
25-
use TYPO3\CMS\Backend\View\BackendLayout\Grid\Grid;
2617
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem;
27-
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridRow;
28-
use TYPO3\CMS\Core\Utility\GeneralUtility;
29-
use TYPO3\CMS\Fluid\View\StandaloneView;
3018

3119
class ContainerPreviewRenderer extends StandardContentPreviewRenderer
3220
{
33-
/**
34-
* @var Registry
35-
*/
36-
protected $tcaRegistry;
37-
38-
/**
39-
* @var ContainerFactory
40-
*/
41-
protected $containerFactory;
42-
43-
protected NewContentUrlBuilder $newContentUrlBuilder;
21+
protected GridRenderer $gridRenderer;
4422

45-
/**
46-
* @var EventDispatcherInterface
47-
*/
48-
protected $eventDispatcher;
49-
50-
public function __construct(
51-
Registry $tcaRegistry,
52-
ContainerFactory $containerFactory,
53-
NewContentUrlBuilder $newContentUrlBuilder,
54-
EventDispatcherInterface $eventDispatcher
55-
) {
56-
$this->eventDispatcher = $eventDispatcher;
57-
$this->tcaRegistry = $tcaRegistry;
58-
$this->containerFactory = $containerFactory;
59-
$this->newContentUrlBuilder = $newContentUrlBuilder;
23+
public function __construct(GridRenderer $gridRenderer) {
24+
$this->gridRenderer = $gridRenderer;
6025
}
6126

6227
public function renderPageModulePreviewContent(GridColumnItem $item): string
6328
{
6429
$content = parent::renderPageModulePreviewContent($item);
65-
$context = $item->getContext();
66-
$record = $item->getRecord();
67-
$grid = GeneralUtility::makeInstance(Grid::class, $context);
68-
try {
69-
$container = $this->containerFactory->buildContainer((int)$record['uid']);
70-
} catch (Exception $e) {
71-
// not a container
72-
return $content;
73-
}
74-
$containerGrid = $this->tcaRegistry->getGrid($record['CType']);
75-
foreach ($containerGrid as $cols) {
76-
$rowObject = GeneralUtility::makeInstance(GridRow::class, $context);
77-
foreach ($cols as $col) {
78-
$defVals = $this->getDefValsForContentDefenderAllowsOnlyOneSpecificContentType($record['CType'], (int)$col['colPos']);
79-
$url = $this->newContentUrlBuilder->getNewContentUrlAtTopOfColumn($context, $container, (int)$col['colPos'], $defVals);
80-
$columnObject = GeneralUtility::makeInstance(ContainerGridColumn::class, $context, $col, $container, $url, $defVals !== null);
81-
$rowObject->addColumn($columnObject);
82-
if (isset($col['colPos'])) {
83-
$records = $container->getChildrenByColPos($col['colPos']);
84-
foreach ($records as $contentRecord) {
85-
$url = $this->newContentUrlBuilder->getNewContentUrlAfterChild($context, $container, (int)$col['colPos'], (int)$contentRecord['uid'], $defVals);
86-
$columnItem = GeneralUtility::makeInstance(ContainerGridColumnItem::class, $context, $columnObject, $contentRecord, $container, $url);
87-
$columnObject->addItem($columnItem);
88-
}
89-
}
90-
}
91-
$grid->addRow($rowObject);
92-
}
93-
94-
$gridTemplate = $this->tcaRegistry->getGridTemplate($record['CType']);
95-
$partialRootPaths = $this->tcaRegistry->getGridPartialPaths($record['CType']);
96-
$layoutRootPaths = $this->tcaRegistry->getGridLayoutPaths($record['CType']);
97-
$view = GeneralUtility::makeInstance(StandaloneView::class);
98-
$view->setPartialRootPaths($partialRootPaths);
99-
$view->setLayoutRootPaths($layoutRootPaths);
100-
$view->setTemplatePathAndFilename($gridTemplate);
101-
102-
$view->assign('hideRestrictedColumns', (bool)(BackendUtility::getPagesTSconfig($context->getPageId())['mod.']['web_layout.']['hideRestrictedCols'] ?? false));
103-
$view->assign('newContentTitle', $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newContentElement'));
104-
$view->assign('newContentTitleShort', $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:content'));
105-
$view->assign('allowEditContent', $this->getBackendUser()->check('tables_modify', 'tt_content'));
106-
// keep compatibility
107-
$view->assign('containerGrid', $grid);
108-
$view->assign('grid', $grid);
109-
$view->assign('containerRecord', $record);
110-
$view->assign('context', $context);
111-
$beforeContainerPreviewIsRendered = new BeforeContainerPreviewIsRenderedEvent($container, $view, $grid, $item);
112-
$this->eventDispatcher->dispatch($beforeContainerPreviewIsRendered);
113-
$rendered = $view->render();
114-
115-
return $content . $rendered;
116-
}
117-
118-
protected function getDefValsForContentDefenderAllowsOnlyOneSpecificContentType(string $cType, int $colPos): ?array
119-
{
120-
$contentDefefenderConfiguration = $this->tcaRegistry->getContentDefenderConfiguration($cType, $colPos);
121-
$allowedCTypes = GeneralUtility::trimExplode(',', $contentDefefenderConfiguration['allowed.']['CType'] ?? '', true);
122-
$allowedListTypes = GeneralUtility::trimExplode(',', $contentDefefenderConfiguration['allowed.']['list_type'] ?? '', true);
123-
if (count($allowedCTypes) === 1) {
124-
if ($allowedCTypes[0] !== 'list') {
125-
return ['CType' => $allowedCTypes[0]];
126-
}
127-
if (count($allowedListTypes) === 1) {
128-
return ['CType' => 'list', 'list_type' => $allowedListTypes[0]];
129-
}
130-
}
131-
return null;
30+
$grid = $this->gridRenderer->renderGrid($item->getRecord(), $item->getContext(), $item);
31+
return $content . $grid;
13232
}
13333
}
+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace B13\Container\Backend\Preview;
6+
7+
/*
8+
* This file is part of TYPO3 CMS-based extension "container" by b13.
9+
*
10+
* It is free software; you can redistribute it and/or modify it under
11+
* the terms of the GNU General Public License, either version 2
12+
* of the License, or any later version.
13+
*/
14+
15+
use B13\Container\Backend\Grid\ContainerGridColumn;
16+
use B13\Container\Backend\Grid\ContainerGridColumnItem;
17+
use B13\Container\Backend\Service\NewContentUrlBuilder;
18+
use B13\Container\Domain\Factory\Exception;
19+
use B13\Container\Domain\Factory\PageView\Backend\ContainerFactory;
20+
use B13\Container\Events\BeforeContainerPreviewIsRenderedEvent;
21+
use B13\Container\Tca\Registry;
22+
use Psr\EventDispatcher\EventDispatcherInterface;
23+
use TYPO3\CMS\Backend\Utility\BackendUtility;
24+
use TYPO3\CMS\Backend\View\BackendLayout\Grid\Grid;
25+
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem;
26+
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridRow;
27+
use TYPO3\CMS\Backend\View\PageLayoutContext;
28+
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
29+
use TYPO3\CMS\Core\Localization\LanguageService;
30+
use TYPO3\CMS\Core\Utility\GeneralUtility;
31+
use TYPO3\CMS\Fluid\View\StandaloneView;
32+
33+
class GridRenderer
34+
{
35+
36+
protected Registry $tcaRegistry;
37+
protected ContainerFactory $containerFactory;
38+
protected NewContentUrlBuilder $newContentUrlBuilder;
39+
protected EventDispatcherInterface $eventDispatcher;
40+
41+
public function __construct(
42+
Registry $tcaRegistry,
43+
ContainerFactory $containerFactory,
44+
NewContentUrlBuilder $newContentUrlBuilder,
45+
EventDispatcherInterface $eventDispatcher
46+
) {
47+
$this->eventDispatcher = $eventDispatcher;
48+
$this->tcaRegistry = $tcaRegistry;
49+
$this->containerFactory = $containerFactory;
50+
$this->newContentUrlBuilder = $newContentUrlBuilder;
51+
}
52+
53+
public function renderGrid(array $record, PageLayoutContext $context, ?GridColumnItem $parentGridColumnItem = null): string
54+
{
55+
$grid = GeneralUtility::makeInstance(Grid::class, $context);
56+
try {
57+
$container = $this->containerFactory->buildContainer((int)$record['uid']);
58+
} catch (Exception $e) {
59+
// not a container
60+
return '';
61+
}
62+
$containerGrid = $this->tcaRegistry->getGrid($record['CType']);
63+
foreach ($containerGrid as $cols) {
64+
$rowObject = GeneralUtility::makeInstance(GridRow::class, $context);
65+
foreach ($cols as $col) {
66+
$defVals = $this->getDefValsForContentDefenderAllowsOnlyOneSpecificContentType($record['CType'], (int)$col['colPos']);
67+
$url = $this->newContentUrlBuilder->getNewContentUrlAtTopOfColumn($context, $container, (int)$col['colPos'], $defVals);
68+
$columnObject = GeneralUtility::makeInstance(ContainerGridColumn::class, $context, $col, $container, $url, $defVals !== null);
69+
$rowObject->addColumn($columnObject);
70+
if (isset($col['colPos'])) {
71+
$records = $container->getChildrenByColPos($col['colPos']);
72+
foreach ($records as $contentRecord) {
73+
$url = $this->newContentUrlBuilder->getNewContentUrlAfterChild($context, $container, (int)$col['colPos'], (int)$contentRecord['uid'], $defVals);
74+
$columnItem = GeneralUtility::makeInstance(ContainerGridColumnItem::class, $context, $columnObject, $contentRecord, $container, $url);
75+
$columnObject->addItem($columnItem);
76+
}
77+
}
78+
}
79+
$grid->addRow($rowObject);
80+
}
81+
82+
$gridTemplate = $this->tcaRegistry->getGridTemplate($record['CType']);
83+
$partialRootPaths = $this->tcaRegistry->getGridPartialPaths($record['CType']);
84+
$layoutRootPaths = $this->tcaRegistry->getGridLayoutPaths($record['CType']);
85+
$view = GeneralUtility::makeInstance(StandaloneView::class);
86+
$view->setPartialRootPaths($partialRootPaths);
87+
$view->setLayoutRootPaths($layoutRootPaths);
88+
$view->setTemplatePathAndFilename($gridTemplate);
89+
90+
$view->assign('hideRestrictedColumns', (bool)(BackendUtility::getPagesTSconfig($context->getPageId())['mod.']['web_layout.']['hideRestrictedCols'] ?? false));
91+
$view->assign('newContentTitle', $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newContentElement'));
92+
$view->assign('newContentTitleShort', $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:content'));
93+
$view->assign('allowEditContent', $this->getBackendUser()->check('tables_modify', 'tt_content'));
94+
// keep compatibility
95+
$view->assign('containerGrid', $grid);
96+
$view->assign('grid', $grid);
97+
$view->assign('containerRecord', $record);
98+
$view->assign('context', $context);
99+
$beforeContainerPreviewIsRendered = new BeforeContainerPreviewIsRenderedEvent($container, $view, $grid, $parentGridColumnItem);
100+
$this->eventDispatcher->dispatch($beforeContainerPreviewIsRendered);
101+
$rendered = $view->render();
102+
return $rendered;
103+
}
104+
105+
protected function getDefValsForContentDefenderAllowsOnlyOneSpecificContentType(string $cType, int $colPos): ?array
106+
{
107+
$contentDefefenderConfiguration = $this->tcaRegistry->getContentDefenderConfiguration($cType, $colPos);
108+
$allowedCTypes = GeneralUtility::trimExplode(',', $contentDefefenderConfiguration['allowed.']['CType'] ?? '', true);
109+
$allowedListTypes = GeneralUtility::trimExplode(',', $contentDefefenderConfiguration['allowed.']['list_type'] ?? '', true);
110+
if (count($allowedCTypes) === 1) {
111+
if ($allowedCTypes[0] !== 'list') {
112+
return ['CType' => $allowedCTypes[0]];
113+
}
114+
if (count($allowedListTypes) === 1) {
115+
return ['CType' => 'list', 'list_type' => $allowedListTypes[0]];
116+
}
117+
}
118+
return null;
119+
}
120+
121+
protected function getBackendUser(): BackendUserAuthentication
122+
{
123+
return $GLOBALS['BE_USER'];
124+
}
125+
126+
protected function getLanguageService(): LanguageService
127+
{
128+
return $GLOBALS['LANG'];
129+
}
130+
}

Classes/Events/BeforeContainerPreviewIsRenderedEvent.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ final class BeforeContainerPreviewIsRenderedEvent
2525

2626
protected Grid $grid;
2727

28-
protected GridColumnItem $item;
28+
protected ?GridColumnItem $item;
2929

30-
public function __construct(Container $container, StandaloneView $view, Grid $grid, GridColumnItem $item)
30+
public function __construct(Container $container, StandaloneView $view, Grid $grid, ?GridColumnItem $item)
3131
{
3232
$this->container = $container;
3333
$this->view = $view;
@@ -50,7 +50,7 @@ public function getGrid(): Grid
5050
return $this->grid;
5151
}
5252

53-
public function getItem(): GridColumnItem
53+
public function getItem(): ?GridColumnItem
5454
{
5555
return $this->item;
5656
}

0 commit comments

Comments
 (0)