webman
webman

Reputation: 1203

TYPO3 typoscript set includeCSS trough userFunc

I have a site that has a dark theme and light theme set in function of the daylight ... in my constants i define sunset and sunrise and I have a Class that I reach through userFunc, my class:

<?php
namespace Vendor\Extension\Utility;

use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
 * will return theme class in various instances of the page
 */

class ThemeClass extends ContentObjectRenderer
{
    /**
     * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
     */
    public $cObj;

    /**
     * Returns theme class according to daylight
     *
     * @var string $content
     * @var array $config
     * @return string
     **/
    protected function class($content, array $config = null)
    {
        // init
        $sunRise = $this->cObj->stdWrapValue('sunRise', $config, null);
        $sunSet = $this->cObj->stdWrapValue('sunSet', $config, null);
        $format = 'h:i a';
    
        $now = date_create_from_format($format, Date($format));
        $open = date_create_from_format($format, $sunRise);
        $close = date_create_from_format($format, $sunSet);
    
        return $theme = ($now > $open && $now < $close) ? 'light' : 'dark' ;
    }

    /**
     * Returns body tag according to theme
     *
     * @var string $content
     * @var array $config
     * @return string
     **/
    public function bodyTag($content, array $config = null)
    {
        $theme = $this->class($content, $config);

        return '<body class="'.$theme.'">';
    }

    /**
     * Returns css according to theme
     *
     * @var string $content
     * @var array $config
     * @return string
     **/
    public function themeCss($content, array $config = null)
    {
        $theme = $this->class($content, $config);

        return 'EXT:extension/Resources/Public/Css/_'.$theme.'.css">';
    }

}

I use the theme overrideing the bodytag like this:

page.bodyTag >
page.bodyTagCObject = USER
page.bodyTagCObject {
    userFunc = Vendor\Extension\Utility\ThemeClass->bodyTag
    sunRise = {$extension.config.daylight.sunrise}
    sunSet = {$extension.config.daylight.sunset}
}

I expected that something similar would work here :

page.includeCSS {
    theme.cObject = USER
    theme.cObject {
        userFunc = Vendor\Extension\Utility\ThemeClass->themeCss
        sunRise = {$extension.config.daylight.sunrise}
        sunSet = {$extension.config.daylight.sunset}
    }
}

but it doesn't work within includeCSS ... I tried a few 'side-steps' of this but cannot get anything to work ...

Upvotes: 0

Views: 123

Answers (1)

Daniel
Daniel

Reputation: 7016

You could register a USER or USER_INT somewhere in the page object and in you user function get the AssetCollector to register CSS.

Upvotes: 0

Related Questions