如何在 TYPO3 的 CommandController 中将纯文本呈现为 HTML

How to render plain text to HTML in CommandController in TYPO3

我想要的是将纯文本转换为 HTML 代码,例如 some plain text<p>some plain text</p>

我试过 ContentObjectRenderer 如下

/** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObject */
$contentObject = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);

return $contentObject->parseFunc($bodytext, array(), '< lib.parseFunc_RTE');

然而,它根本不起作用。我必须初始化 $GLOBALS['TSFE'] 还是什么?

请查看HtmlViewHelper.php。您会看到它们使用 simulateFrontendEnvironment() 方法构建 $GLOBALS['TSFE'] 并在 cObj 处理后将其删除。

/**
 * Copies the specified parseFunc configuration to $GLOBALS['TSFE']->tmpl->setup in Backend mode
 * This somewhat hacky work around is currently needed because the parseFunc() function of \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer relies on those variables to be set
 */
protected static function simulateFrontendEnvironment()
{
    self::$tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : null;
    $GLOBALS['TSFE'] = new \stdClass();
    $GLOBALS['TSFE']->tmpl = new \stdClass();
    $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
    $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
    $GLOBALS['TSFE']->tmpl->setup = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
}

将您需要的所有内容复制到您的扩展中,它应该可以工作