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

时间:2015-05-20 20:14:36

标签: typo3 typoscript typo3-7.x

由于TYPO3 7条件'设备'和' useragent'不推荐使用。不,我正在寻找userFunc作为检测移动设备的条件。我的目的是隐藏或在移动设备上显示一些特殊页面。 我使用了扩展名' contexts_wurfl'一段时间但我想应该有更小的解决方案'。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

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

下面的代码向您展示了在执行其他操作之前如何执行自己的代码(例如模板引擎/内容呈​​现等)。

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, ...]

答案 1 :(得分:0)

如果您想避免编写自定义用户功能,则在更高版本的Typo3中仍可以使用Typo3的globalString函数来访问用户代理和其他类似信息:

[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

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

要为移动设备和固定设备执行不同的印刷,我发现以下代码段适用于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]