【问题标题】:Fluidtypo3: Use custom field as title of FCEFluidtypo3:使用自定义字段作为 FCE 的标题
【发布时间】:2016-02-28 06:42:56
【问题描述】:

我不想在我的 FCE 中使用默认标题,而只想使用自定义通量字段。在后端列表视图中,我的 FCE 显示为“[no title]”,因为未填充默认标题。这会给编辑带来很大的困惑。

如何定义我的自定义通量字段之一以用作 TYPO3 后端列表视图等中 FCE 的标题?

【问题讨论】:

    标签: typo3 fluid flux


    【解决方案1】:

    您不能只使用 flexform 中的字段,因为 FCE 中的所有字段都存储在数据库的同一字段中 (pi_flexform)。

    您可以做的是使用用户函数呈现内容元素标题。它在 TCA 配置中用这样的一行注册:

    $GLOBALS['TCA']['tt_content']['ctrl']['label_userFunc'] = 'Vendor\\Extkey\\Utility\\ContentElementLabelRenderer->getContentElementTitle';
    

    用户函数本身可能如下所示:

    <?php
    namespace Vendor\Extkey\Utility;
    
    /**
     * This class renders a human readable title for FCEs,
     * so one is able to find a content element by its headline.
     */
    class ContentElementLabelRenderer implements \TYPO3\CMS\Core\SingletonInterface {
    
        /**
         * @var \TYPO3\CMS\Extbase\Service\FlexFormService
         * @inject
         */
        protected $flexFormService = null;
    
        /**
         * Returns the content element title for a given content element
         */
        public function getContentElementTitle(&$params) {
    
            if (null === $this->flexFormService) {
                $this->flexFormService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Service\\FlexFormService');
            }
    
            if (ctype_digit($params['row']['uid']) && 'fluidcontent_content' === $params['row']['CType']) {
    
                // If this is a FCE, parse the flexform and template name and generate the 
                // title in a template specific way.
                $row = $params['row'];
                $additionalRowData = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('pi_flexform, tx_fed_fcefile', 'tt_content', 'uid = ' . $row['uid']);
                $flexFormContent = $this->flexFormService->convertFlexFormContentToArray($additionalRowData['pi_flexform']);
    
                $lastColonPosition = strrpos($additionalRowData['tx_fed_fcefile'], ':');
                $contentElementType = (FALSE === $lastColonPosition) ? 'invalidtype' : substr($additionalRowData['tx_fed_fcefile'], $lastColonPosition + 1);
                switch ($contentElementType) {
    
                    case 'Image.html':
                        $params['title'] = 'Image: "' . ($flexFormContent['title'] ?: $flexFormContent['subtitle']) . '"';
                        break;
                    default:
                        $params['title'] = 'Unknown content element type';
                        break;
                 }
            }
            else {
                // If this is not a FCEm, print out "normal"    
                // title. Not the real thing, but comes pretty close, hopefully.
                $params['title'] = $params['row']['header'] ?: ($params['row']['subheader'] ?: $params['row']['bodytext']);
            }
        }
    }
    

    但这会产生维护问题:每次添加或更改内容元素时,都必须更新此文件。

    【讨论】:

    • 我终于测试了你的解决方案,它就像一个魅力!非常感谢!
    • 这对我来说适用于 TYPO3 6.2,但不适用于 TYPO3 7.6。在我的情况下,虽然命名空间正确,但找不到类 ContentElementLabelRenderer。我必须添加 require_once \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath( 'myext' ) . 'Classes/Utility/ContentElementLabelRenderer.php'; 才能使其正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多