Fluidtypo3:使用自定义字段作为 FCE 的标题
Fluidtypo3: Use custom field as title of FCE
我不想在我的 FCE 中使用默认值 header,而只想使用自定义通量字段。在后端列表视图中,我的 FCE 显示为“[无标题]”,因为默认 header 未填充。这给编辑带来了很多困惑。
如何定义我的自定义通量字段之一以用作 TYPO3 后端列表视图等中 FCE 的标题?
您不能只使用 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']);
}
}
}
但这会产生维护问题:每次添加或更改内容元素时,都必须更新此文件。
我不想在我的 FCE 中使用默认值 header,而只想使用自定义通量字段。在后端列表视图中,我的 FCE 显示为“[无标题]”,因为默认 header 未填充。这给编辑带来了很多困惑。
如何定义我的自定义通量字段之一以用作 TYPO3 后端列表视图等中 FCE 的标题?
您不能只使用 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']);
}
}
}
但这会产生维护问题:每次添加或更改内容元素时,都必须更新此文件。