Attention
TYPO3 v11 has reached end-of-life as of October 31th 2024 and is no longer being maintained. Use the version switcher on the top left of this page to select documentation for a supported version of TYPO3.
Need more time before upgrading? You can purchase Extended Long Term Support (ELTS) for TYPO3 v11 here: TYPO3 ELTS.
Configure custom backend preview for content element
To allow editors a smoother experience, all custom content elements and plugins should be configured with a corresponding backend preview that shows an approximation of the element's appearance in the TYPO3 page module. The following sections describe how to achieve that.
A preview renderer is used to facilitate (record) previews in TYPO3. This class is responsible for generating the preview and the wrapping.
The default preview renderer is \TYPO3\
and handles the Core's built-in content types (field CType
in table tt_
).
Extend the default preview renderer
There are two ways to provide previews for your custom content types: via page TSconfig or hook.
Page TSconfig
This is the "integrator" way, no PHP coding is required. Just some page TSconfig and a Fluid template.
mod.web_layout {
tt_content {
preview {
# Your CType
example_ctype = EXT:my_extension/Resources/Private/Templates/Preview/ExampleCType.html
}
}
}
For more details see the TSconfig Reference.
Hook
This requires at least some PHP coding, but allows more flexibility in accessing and processing the content elements properties.
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'][]
= MyVendor\MyExtension\Backend\DrawItem::class;
Changed in version 12.0
In version 12.0 this hook will been removed and replaced by the event
Page
Writing a PreviewRenderer
A custom preview renderer must implement the interface
\TYPO3\
which contains
the following API methods:
- interface PreviewRendererInterface
-
- Fully qualified name
-
\TYPO3\
CMS\ Backend\ Preview\ Preview Renderer Interface
Interface PreviewRendererInterface
Contract for classes capable of rendering previews of a given record from a table. Responsible for rendering preview header, preview content and wrapping of those two values.
Responsibilities are segmented into three methods, one for each responsibility, which is done in order to allow overriding classes to change those parts individually without having to replace other parts. Rather than relying on implementations to be friendly and divide code into smaller pieces and give them (at least) protected visibility, the key methods are instead required on the interface directly.
Callers are then responsible for calling each method and combining/wrapping the output appropriately.
- renderPageModulePreviewHeader ( TYPO3\\CMS\\Backend\\View\\BackendLayout\\Grid\\GridColumnItem $item)
-
Dedicated method for rendering preview header HTML for the page module only. Receives the the GridColumnItem that contains the record for which a preview header should be rendered and returned.
- param TYPO3\\CMS\\Backend\\View\\BackendLayout\\Grid\\GridColumnItem $item
-
the item
- returntype
-
string
- renderPageModulePreviewContent ( TYPO3\\CMS\\Backend\\View\\BackendLayout\\Grid\\GridColumnItem $item)
-
Dedicated method for rendering preview body HTML for the page module only. Receives the the GridColumnItem that contains the record for which a preview should be rendered and returned.
- param TYPO3\\CMS\\Backend\\View\\BackendLayout\\Grid\\GridColumnItem $item
-
the item
- returntype
-
string
-
Render a footer for the record to display in page module below the body of the item's preview.
- param TYPO3\\CMS\\Backend\\View\\BackendLayout\\Grid\\GridColumnItem $item
-
the item
- returntype
-
string
- wrapPageModulePreview ( string $previewHeader, string $previewContent, TYPO3\\CMS\\Backend\\View\\BackendLayout\\Grid\\GridColumnItem $item)
-
Dedicated method for wrapping a preview header and body HTML. Receives $item, an instance of GridColumnItem holding among other things the record, which can be used to determine appropriate wrapping.
- param string $previewHeader
-
the previewHeader
- param string $previewContent
-
the previewContent
- param TYPO3\\CMS\\Backend\\View\\BackendLayout\\Grid\\GridColumnItem $item
-
the item
- returntype
-
string
Implementing these methods allows you to control the exact composition of the preview.
This means assuming your preview renderer returns <h4>Header</
from the header render method and <p>Body</
from the preview content
rendering method and your wrapping method does
return '<div>' . $preview
then the
entire output becomes <div><h4>Header</
when
combined.
Should you wish to reuse parts of the default preview rendering and only change,
for example, the method that renders the preview body content, you can extend
\TYPO3\
in your custom
preview renderer class - and selectively override the methods from the API
displayed above.
Configuring the implementation
Individual preview renderers can be defined by using one of the following approaches:
-
Any record
$GLOBALS['TCA'][$table]['ctrl']['previewRenderer'] = MyVendor\MyExtension\Preview\MyPreviewRenderer::class;
Copied!This specifies the preview renderer to be used for any record in
$table
. -
Table has a type field/attribute
$GLOBALS['TCA'][$table]['types'][$type]['previewRenderer'] = MyVendor\MyExtension\Preview\MyPreviewRenderer::class;
Copied!This specifies the preview renderer only for records of type
$type
as determined by the type field of your table. -
Table and field have a
subtype_
TCA settingvalue_ field If your table and field have a
subtype_
TCA setting (likevalue_ field tt_
for example) and you want to register a preview renderer that applies only when that value is selected (assume, when a certain plugin type is selected and you can't match it with the "type" of the record alone):content. list_ type $GLOBALS['TCA'][$table]['types'][$type]['previewRenderer'][$subType] = MyVendor\MyExtension\Preview\MyPreviewRenderer::class;
Copied!Where
$type
is, for example,list
(indicating a plugin) and$sub
is the value of theType list_
field when the type of plugin you want to target is selected as plugin type.type
Note
The recommended location is in the
ctrl
array in your extension's Configuration/
or Configuration/
file. The former is used
when your extension is the one that creates the table, the latter is used
when you need to override TCA properties of tables added by the Core or
other extensions.
Note
The content elements text
, textpic
, textmedia
and
image
have their own Preview
. Therefore it's not
sufficient to overwrite the Standard
but
you need to use the second approach from above for every single of
these content elements.