Skip to content

Commit

Permalink
[BUGFIX] Simulate TSFE while processing variables
Browse files Browse the repository at this point in the history
As mail will be sent by scheduler (BE context), we
do not have a TSFE. If working with TS Variables these
require a working TSFE. Adding a temporary TSFE
like in f:cObject VH solves that problem.

Resolves: in2code-de#45
  • Loading branch information
froemken committed Sep 21, 2020
1 parent a859177 commit 7ed6923
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Classes/Domain/Service/ParseNewsletterUrlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Class ParseNewsletterUrlService to fill a container html with a content from a http(s) page.
Expand All @@ -42,6 +43,13 @@ class ParseNewsletterUrlService
*/
protected $parseVariables = true;

/**
* Contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
*
* @var TypoScriptFrontendController
*/
protected static $tsfeBackup;

/**
* ParseNewsletterUrlService constructor.
* @param string $origin can be a page uid or a complete url
Expand Down Expand Up @@ -144,6 +152,9 @@ protected function getNewsletterContainerAndContent(string $content, User $user)
*/
protected function getContentObjectVariables(array $configuration): array
{
if (TYPO3_MODE === 'BE') {
self::simulateFrontendEnvironment();
}
$tsService = ObjectUtility::getObjectManager()->get(TypoScriptService::class);
$tsConfiguration = $tsService->convertPlainArrayToTypoScriptArray($configuration);

Expand All @@ -161,6 +172,10 @@ protected function getContentObjectVariables(array $configuration): array
);
}

if (TYPO3_MODE === 'BE') {
self::resetFrontendEnvironment();
}

return $variables;
}

Expand Down Expand Up @@ -219,6 +234,28 @@ protected function getBodyFromHtml(string $string): string
return $string;
}

/**
* Sets the $TSFE->cObjectDepthCounter in Backend mode
* This somewhat hacky work around is currently needed because the cObjGetSingle() function of \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer relies on this setting
*/
protected static function simulateFrontendEnvironment()
{
static::$tsfeBackup = $GLOBALS['TSFE'] ?? null;
$GLOBALS['TSFE'] = new \stdClass();
$GLOBALS['TSFE']->cObj = ObjectUtility::getContentObject();
$GLOBALS['TSFE']->cObjectDepthCounter = 100;
}

/**
* Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
*
* @see simulateFrontendEnvironment()
*/
protected static function resetFrontendEnvironment()
{
$GLOBALS['TSFE'] = static::$tsfeBackup;
}

/**
* @return bool
*/
Expand Down

0 comments on commit 7ed6923

Please sign in to comment.