typo3typoscripttypo3-7.x

userfunc condition for detecting mobile device


Since TYPO3 7 the condition 'device' and 'useragent' are deprecated. No I'm looking for a userFunc to use as a condition for detecting mobile devices. My aim is to hide or to show some special pages on mobile devices. I used the extension 'contexts_wurfl' for a while but I guess that there should be 'smaller solutions'.

Thanks for any help.


Solution

  • You can accomplish this with TypoScript using the PAGE Object.

    The code below shows you how to execute your own code before executing something else (like the template engine/content rendering etcetera).

    page.01 = USER_INT
    page.01 {
        userFunc = TYPO3\MyExt\Utility\MobileDeviceUtility->detectMobileDevice
    }
    

    And in code:

    <?php
    namespace TYPO3\MyExt\Utility;
    
    class MobileDeviceUtility {
    
        /**
         * Is Mobile Device
         *
         * @return boolean
         */
        static public function isMobileDevice() {
    
            // calculates if the user agent is on a mobile device
    
            return TRUE;
        }
    
        /**
         * Detect Mobile Device
         *
         * @param string $content
         * @param array $conf
         * @return void
         */ 
        static public function detectMobileDevice($content, array $conf = NULL) {
    
            global $TSFE;
    
            if (self::isMobileDevice()
                  && (boolean) $TSFE->page['mycustom_device_checkbox']
            ) {
                // do something 
            }
    
        }
    
    }
    

    OR otherwise you should create your own condition [YourVendor\YourPackage\YourCondition = var1 = value1, var2 != value2, ...].