FluidTYPO3\Vhs\Utility\FrontendSimulationUtility::simulateFrontendEnvironment PHP Method

simulateFrontendEnvironment() public static method

Sets the global variables $GLOBALS['TSFE']->csConvObj and $GLOBALS['TSFE']->renderCharset in Backend mode This somewhat hacky work around is currently needed because the conv_case() and convCaseFirst() functions of tslib_cObj rely on those variables to be set
public static simulateFrontendEnvironment ( ) : mixed
return mixed
    public static function simulateFrontendEnvironment()
    {
        if ('BE' !== TYPO3_MODE) {
            return;
        }
        $tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : null;
        $GLOBALS['TSFE'] = new \stdClass();
        // preparing csConvObj
        if (false === is_object($GLOBALS['TSFE']->csConvObj)) {
            if (true === is_object($GLOBALS['LANG'])) {
                $GLOBALS['TSFE']->csConvObj = $GLOBALS['LANG']->csConvObj;
            } else {
                $GLOBALS['TSFE']->csConvObj = GeneralUtility::makeInstance(CharsetConverter::class);
            }
        }
        // preparing renderCharset
        if (false === is_object($GLOBALS['TSFE']->renderCharset)) {
            if (true === is_object($GLOBALS['LANG'])) {
                $GLOBALS['TSFE']->renderCharset = $GLOBALS['LANG']->charSet;
            } else {
                $GLOBALS['TSFE']->renderCharset = 'utf-8';
            }
        }
        return $tsfeBackup;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * used to attach srcset variants of a given image to the specified tag
  *
  * @param \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder $tag the tag to add the srcset as argument
  * @param string $src image path to render srcsets for
  * @return array
  */
 public function addSourceSet($tag, $src)
 {
     $srcsets = $this->getSourceSetWidths();
     if ('BE' === TYPO3_MODE) {
         FrontendSimulationUtility::simulateFrontendEnvironment();
     }
     $format = $this->arguments['format'];
     $quality = $this->arguments['quality'];
     $treatIdAsReference = (bool) $this->arguments['treatIdAsReference'];
     if (true === $treatIdAsReference) {
         $src = $this->arguments['src'];
     }
     $imageSources = [];
     $srcsetVariants = [];
     foreach ($srcsets as $key => $width) {
         $srcsetVariant = $this->getImgResource($src, $width, $format, $quality, $treatIdAsReference);
         $srcsetVariantSrc = rawurldecode($srcsetVariant[3]);
         $srcsetVariantSrc = $this->preprocessSourceUri(GeneralUtility::rawUrlEncodeFP($srcsetVariantSrc));
         $imageSources[$srcsetVariant[0]] = ['src' => $srcsetVariantSrc, 'width' => $srcsetVariant[0], 'height' => $srcsetVariant[1]];
         $srcsetVariants[$srcsetVariant[0]] = $srcsetVariantSrc . ' ' . $srcsetVariant[0] . 'w';
     }
     $tag->addAttribute('srcset', implode(',', $srcsetVariants));
     if ('BE' === TYPO3_MODE) {
         FrontendSimulationUtility::resetFrontendEnvironment();
     }
     return $imageSources;
 }
All Usage Examples Of FluidTYPO3\Vhs\Utility\FrontendSimulationUtility::simulateFrontendEnvironment