用于检测移动设备的 userfunc 条件

userfunc condition for detecting mobile device

自 TYPO3 7 起,条件 'device' 和 'useragent' 已弃用。不,我正在寻找一个 userFunc 用作检测移动设备的条件。我的目标是在移动设备上隐藏或显示一些特殊页面。 我使用扩展名 'contexts_wurfl' 一段时间,但我想应该有 'smaller solutions'.

感谢您的帮助。

您可以使用 PAGE 对象通过 TypoScript 完成此操作。

下面的代码向您展示了如何在执行其他内容(例如模板 engine/content 渲染等)之前执行您自己的代码。

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

在代码中:

<?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 
        }

    }

}

否则您应该创建自己的条件 [YourVendor\YourPackage\YourCondition = var1 = value1, var2 != value2, ...]

如果您想避免编写自定义用户函数,Typo3 的 globalString 函数仍然可以在更高版本的 Typo3 中使用,以访问用户代理和其他信息,如下所示:

[globalString = IENV:HTTP_USER_AGENT = *<User-Agent>*]
  # Statements here will only affect browsers with the useragent matching <User-Agent>
[else]
  # Statements here will only affect browsers with the useragent not matching <User-Agent>
[end]

可以找到使用 globalString 条件的详细文档 here

可以找到可与 globalString 函数一起使用的变量的完整列表 here

为了对移动设备和固定设备执行不同的拼写错误,我发现以下代码片段适用于 Typo3 8.7 LTS 和 9.5 LTS:

[globalString = IENV:HTTP_USER_AGENT = *Android*]||[globalString = IENV:HTTP_USER_AGENT = *iPhone*]||[globalString = IENV:HTTP_USER_AGENT = *Mobile*]||[globalString = IENV:HTTP_USER_AGENT = *Windows Phone*]
  # Statements for mobile devices only
[else]
  # Statements for stationary devices only
[end]