|
| 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 | +} |
0 commit comments