Skip to content

Instantly share code, notes, and snippets.

@liayn
Created June 8, 2015 17:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liayn/b09fa61878244f273493 to your computer and use it in GitHub Desktop.
Save liayn/b09fa61878244f273493 to your computer and use it in GitHub Desktop.
Create Fake FE in TYPO3 Backend for proper FE link generation from BE
<?php
class Foo {
protected function buildFakeFE($pageUid) {
// clear session key to avoid FE cookies
$oldGETSessionKey = '';
if (isset($GLOBALS['_GET']['FE_SESSION_KEY'])) {
$oldGETSessionKey = $GLOBALS['_GET']['FE_SESSION_KEY'];
}
$oldPOSTSessionKey = '';
if (isset($GLOBALS['_POST']['FE_SESSION_KEY'])) {
$oldPOSTSessionKey = $GLOBALS['_POST']['FE_SESSION_KEY'];
}
$GLOBALS['_POST']['FE_SESSION_KEY'] = '';
$GLOBALS['_GET']['FE_SESSION_KEY'] = '';
// simulate FE session to get TypoScript config from given PID
$GLOBALS['TT'] = GeneralUtility::makeInstance(NullTimeTracker::class);
// simulate a normal FE without any logged-in FE or BE user
/** @var $fe TypoScriptFrontendController */
$this->tsfe = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], $pageUid, 0, 0, TRUE);
$GLOBALS['TSFE'] = $this->tsfe;
$this->tsfe->TYPO3_CONF_VARS['FE']['pageNotFound_handling'] = '';
$this->tsfe->beUserLogin = FALSE;
try {
$this->tsfe->initFEuser();
$this->tsfe->clear_preview();
$this->tsfe->determineId();
$this->tsfe->makeCacheHash();
$this->tsfe->initTemplate();
$this->tsfe->getFromCache();
$this->tsfe->getConfigArray();
} catch (\Exception $e) {
return FALSE;
}
// calculate the absolute path prefix
if (!empty($this->tsfe->config['config']['absRefPrefix'])) {
$absRefPrefix = trim($this->tsfe->config['config']['absRefPrefix']);
if ($absRefPrefix === 'auto') {
$this->tsfe->absRefPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
} else {
$this->tsfe->absRefPrefix = $absRefPrefix;
}
} else {
$this->tsfe->absRefPrefix = '';
}
$this->tsfe->newCObj();
// restore
$GLOBALS['_GET']['FE_SESSION_KEY'] = $oldGETSessionKey;
$GLOBALS['_POST']['FE_SESSION_KEY'] = $oldPOSTSessionKey;
return TRUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment