Reputation: 11
I have a TYPO3 v12 project, where the admin can create a content-element (text+image) in the backend. The content-element should be rendered using fluid_styled_content and sent in an email. This works fine in the frontend context. But when trying to render the element in a scheduler job or the CLI context, rendering fails. Does somebody have a working example how to render a content-element in this context under TYPO3 12?
I have tried various solutions that existed for previous versions of TYPO3, including manually initializing the frontend and TypoScript Array.
Upvotes: 1
Views: 443
Reputation: 101
What worked for me is to create a ContentObjectRenderer
instance and a ServerRequest
(if not available via $GLOBALS['TYPO3_REQUEST']
). E.g. like this:
if (!isset($GLOBALS['TYPO3_REQUEST'])) {
$siteFinder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Site\SiteFinder::class);
$site = $siteFinder->getSiteByIdentifier('my-site');
$GLOBALS['TYPO3_REQUEST'] = (new \TYPO3\CMS\Core\Http\ServerRequest($site->getBase()))
->withAttribute('applicationType', \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::REQUESTTYPE_BE);
GeneralUtility::setIndpEnv('TYPO3_REQUEST_DIR', (string)$site->getBase());
}
$cObjRenderer = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
$cObjRenderer->setRequest($GLOBALS['TYPO3_REQUEST']);
Note that the site identifier 'my-site'
in line 3 has to be replaced by your site's identifier. Otherwise you also can get your site by $siteFinder->getSiteByPageId($pageUid)
, $siteFinder->getSiteByRootPageId($pageUid)
- or all sites via $siteFinder->getAllSites()
and choose the one you want.
Upvotes: 0
Reputation: 159
I know it's discouraged to initialize your own TSFE, but there are real world applications out there that need this, since there are currently no good alternatives for many use cases (see this answer by core member Christian Kuhn).
I have a legacy extension with a similar scenario (queueing mails with content generated in a FE context for later sending by scheduler task or console command). But in my case I need to render the output of a plugin controller based on (the now deprecated...) AbstractPlugin
while not in FE context. The main problem is, how to initialize TSFE and get all the required TypoScript configuration?
You can use the extension nnhelpers and in particular their Tsfe class. The extension is available for v12. I haven't used the extension as such for myself, but I have consulted its source code for inspiration (mainly for v10 and v11). It offers a lot of other useful helper classes as well.
Another great source of information are the two middlerwares typo3/cms-frontend/tsfe
and typo3/cms-frontend/prepare-tsfe-rendering
in the core, which basically do just what you want (initializing TSFE and typoscript). I consulted those to update my implementation for v12.
Based on that, you can then start to render your content elements. A specific implemention obviously depends on a number of factors (do you have a page with content? do have your own TS config? do you have a PSR-7 request object? etc.), but I hope this can get you started.
Upvotes: 0
Reputation: 7939
This won't really work. I suggest a different proposal:
Prepate a page in the frontend, reduce the page template or use TypoScript only with something like
page >
page = PAGE
page {
20 = RECORDS
20 {
source.data = GP:recordId
dontCheckPid = 1
tables = tt_content
}
}
and fetch in CLI the content with cUrl or GeneralUtility::getUrl($url)
You should also restrict the request (if this is relevant for you), otherwise anyone can read any content element by testing all IDs
Upvotes: 0