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

问题描述 投票:0回答:2

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

谢谢你的帮助。

typo3 typoscript typo3-7.x
2个回答
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, ...]


0
投票

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

[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]

有关使用globalStringcan的条件的详细文档,请参阅here

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

为移动和固定设备执行不同的typoscript,我发现以下代码段适用于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]
© www.soinside.com 2019 - 2024. All rights reserved.