【问题标题】:TYPO3 8 show layout selection in backend preview for textmediaTYPO3 8 在 textmedia 的后端预览中显示布局选择
【发布时间】:2026-01-08 00:50:01
【问题描述】:

我尽量使用和自定义fluid_styled_content的CTypes。因此,选择字段“布局”非常有用,可以选择一些不同的样式(如红色框、阴影或图像内容)。但是,如果您有一些选择的可能性,它不会显示在后端预览中,每个元素看起来都是一样的。

有没有办法在 textmedia 的后端预览中显示布局字段的选定值?

【问题讨论】:

    标签: typo3 backend typo3-8.x fluid-styled-content


    【解决方案1】:

    要完成此操作,请像这样注册一个钩子(路径:yourextension/Classes/Hooks/PageLayoutView/TextMediaCustomPreviewRenderer.php):

        <?php
    namespace Vendor\Yourextension\Hooks\PageLayoutView;
    
    /*
     * This file is part of the TYPO3 CMS project.
     *
     * It is free software; you can redistribute it and/or modify it under
     * the terms of the GNU General Public License, either version 2
     * of the License, or any later version.
     *
     * For the full copyright and license information, please read the
     * LICENSE.txt file that was distributed with this source code.
     *
     * The TYPO3 project - inspiring people to share!
     */
    
    use \TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
    use \TYPO3\CMS\Backend\View\PageLayoutView;
    
    /**
     * Contains a preview rendering for the page module of CType="textmedia"
     */
    class TextMediaCustomPreviewRenderer implements PageLayoutViewDrawItemHookInterface
    {
    
       /**
        * Preprocesses the preview rendering of a content element of type "Text Media"
        *
        * @param \TYPO3\CMS\Backend\View\PageLayoutView $parentObject Calling parent object
        * @param bool $drawItem Whether to draw the item using the default functionality
        * @param string $headerContent Header content
        * @param string $itemContent Item content
        * @param array $row Record row of tt_content
        *
        * @return void
        */
       public function preProcess(
          PageLayoutView &$parentObject,
          &$drawItem,
          &$headerContent,
          &$itemContent,
          array &$row
       )
       {
          $pageTs = \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($row['pid']);
          if ($row['CType'] === 'textmedia') {
             $itemContent .= '<p>Layoutversion: ' . $pageTs['TCEFORM.']['tt_content.']['layout.']['types.']['textmedia.']['addItems.'][$row['layout']] . '</p>';
    
             if ($row['bodytext']) {
                 $itemContent .= $parentObject->linkEditContent(
                         $parentObject->renderText($row['bodytext']),
                         $row
                     ) . '<br />';
             }
             if ($row['assets']) {
                 $itemContent .= $parentObject->thumbCode($row, 'tt_content', 'assets') . '<br />';
             }
             $drawItem = false;
          }
       }
    }
    

    在你的 ext_localconf.php 中你这样写:

        // Register for hook to show preview of tt_content element of CType="textmedia" in page module
    $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem']['textmedia'] = \Vendor\Yourextension\Hooks\PageLayoutView\TextMediaCustomPreviewRenderer::class;
    

    就我而言,我在 pageTsconfig 中提供了不同的选择选项:

        TCEFORM.tt_content.layout.types.textmedia.addItems {
        50 = Textbox grau, Bildergalerie oben
        60 = Textbox grau, Bildergalerie unten
        110 = Blau, rechtsbündig
        210 = Hellblau, linksbündig
        220 = Rot, linksbündig
        310 = Akkordeon
    }
    

    这是使用 locallang.xlf 正确语言处理的更好方法。如果你这样做,也许你必须稍微改变一下代码示例......

    这是 Facebook 上“TYPO3 Fragen, Antworten, inoffizielle Gruppe”主题的结果。非常感谢 Wolfgang Klinger 的每一位帮助 :-)

    【讨论】: