For coders TYPO3 Tech Corner

Add own PreviewRenderer for CType shortcut in the TYPO3 backend

Add own PreviewRenderer for CType shortcut in the TYPO3 backend

Wouldn't it be good if the editors could already see in the page module which referenced page content is in an element of the type shortcut? We'll show you how you can quickly do it yourself without gridelements.

It doesn't matter whether you like the extension gridelements or not. This brings a lot of useful functions. This also includes a simple preview function for the page content shortcut or reference. When you come back later as an editor, you can already see in the overview of the page module which referenced content is hidden behind this one content element.

However, if you prefer to use the container extension, for example, or perhaps you don't need a grid solution at all, you usually won't be able to enjoy this helpful preview function. We will show you how you can get them started with a few lines of code via your site package.

All code examples are based on TYPO3 11 and assume that you know what a sitepackage (in our case "in2template") is and can extend it.

First we define another PreviewRenderer for the Shortcut type page content. This is very easy to do via TCA in EXT:in2template/Configuration/TCA/Overrides/tt_content.php:

<?php // Use a different preview renderer for CType shortcut $GLOBALS['TCA']['tt_content']['types']['shortcut']['previewRenderer'] = \In2code\In2template\Backend\ShortcutPreviewRenderer::class;

The actual class that assembles the preview for us is then located in EXT:in2template/Classes/Backend/ShortcutPreviewRenderer:

<?php declare(strict_types=1); namespace In2code\In2template\Backend; use Throwable; use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumn; use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\MathUtility; use TYPO3\CMS\Fluid\View\StandaloneView; use TYPO3\CMS\Frontend\Preview\TextmediaPreviewRenderer; use UnexpectedValueException; class ShortcutPreviewRenderer extends TextmediaPreviewRenderer { const TABLE_NAME = 'tt_content'; protected string $template = 'EXT:in2template/Resources/Private/Templates/PreviewRenderer/Shortcut.html'; public function renderPageModulePreviewContent(GridColumnItem $item): string { try { $contentIdentifiers = $this->getContentIdentifiers($item->getRecord()); $elements = ''; foreach ($contentIdentifiers as $identifier) { $elements .= $this->getPreviewOfSingleContentElement($item, $identifier); } } catch (Throwable $exception) { $elements = $exception->getMessage() . ' (' . $exception->getCode() . ')'; } return $elements; } protected function getPreviewOfSingleContentElement(GridColumnItem $item, $identifier): string { $record = BackendUtility::getRecord(self::TABLE_NAME, $identifier); $gridColumn = GeneralUtility::makeInstance(GridColumn::class, $item->getContext(), []); $gridColumnItem = GeneralUtility::makeInstance(GridColumnItem::class, $item->getContext(), $gridColumn, $record); $standaloneView = GeneralUtility::makeInstance(StandaloneView::class); $standaloneView->setTemplatePathAndFilename($this->template); $standaloneView->assignMultiple([ 'preview' => $gridColumnItem->getPreview(), 'identifier' => $identifier, 'data' => $record, 'iconIdentifier' => $this->getIconIdentifier($record['CType']), ]); return $standaloneView->render(); } protected function getContentIdentifiers(array $data): array { if (isset($data['records']) === false) { throw new UnexpectedValueException('Key records not available', 1676669367); } $identifiers = []; $mixedIdentifiers = explode(',', $data['records']); foreach ($mixedIdentifiers as $identifier) { $cleanedIdentifier = str_replace(self::TABLE_NAME . '_', '', $identifier); if (MathUtility::canBeInterpretedAsInteger($cleanedIdentifier)) { $identifiers[] = (int)$cleanedIdentifier; } else { throw new UnexpectedValueException($identifier . ' is not a supported identifier', 1676669672); } } return $identifiers; } /** * @param string $cType * @return string * @SuppressWarnings(PHPMD.Superglobals) */ protected function getIconIdentifier(string $cType): string { $typeiconClasses = $GLOBALS['TCA'][self::TABLE_NAME]['ctrl']['typeicon_classes']; if (array_key_exists($cType, $typeiconClasses)) { return $typeiconClasses[$cType]; } return 'mimetypes-x-content-text-media'; } }

Because we also have some HTML and CSS, let's use a Fluid Template for this EXT:in2template/Resources/Private/Templates/PreviewRenderer/Shortcut.html:

<f:asset.css identifier="ce-preview-shortcut-container"> .reference { position: relative; margin-left: 0; margin-right: 0; } .reference .t3-page-ce { margin: 0; } .t3-page-ce:hover .reference .t3-page-ce-header { border: 1px solid #ccc; border-bottom: 0; background: #eaeaea; } .t3-page-ce:hover .reference .t3-page-ce-body { border: 1px solid #ccc; border-top: 0; box-shadow: none; } .reference-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: #427BAB; opacity: 0.15; filter: alpha(opacity=15); } </f:asset.css> <div class="t3-page-ce t3js-page-ce reference" id="element-tt_content-{identifier}"> <div class="t3-page-ce-header row m-0 g-0"> <div class="col t3-page-ce-header-icons-left"> <core:icon identifier="{iconIdentifier}" size="small" /> </div> </div> <div class="t3-page-ce-body"> <div class="t3-page-ce-body-inner"> <span class="exampleContent"> {preview -> f:format.raw()} </span> </div> </div> <div class="reference-overlay"></div> </div>

You can also store the CSS in Fluid in a file and refine it a bit if we have forgotten something. The sample function also only supports referenced tables of type tt_content. You would have to add referencing to pages, tx_news_domain_model_news or others yourself.

Happy Coding!

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news

Null-Safe Operator in the TYPO3 area

With the introduction of PHP8, problems with undefined arrays or variables in general can arise in many places. Here are a few examples and simple solutions.

Go to news

Delete the first/last lines of a (SQL) file

There isn't much to say about the following commands. Sometimes it can be useful to delete the first (or last) X lines from a file. And if the file is too large to open with a conventional program, a...

Go to news

b13/container: Add and modify child elements in edit view

Unlike gridelements, you cannot manage the child elements in the B13 Container extension when you open the container in the editing view. I would be happy to show you how you can quickly install this...

Go to news

Menu comparison: Numbers, numbers, numbers

Go to news