从 TYPO3 7 开始,条件 'device' 和 'useragent' 已被弃用。不,我正在寻找一个 userFunc 用作检测移动设备的条件。我的目标是在移动设备上隐藏或显示一些特殊页面。我使用了扩展名“contexts_wurfl”一段时间,但我想应该有“更小的解决方案”。
谢谢你的帮助。
从 TYPO3 7 开始,条件 'device' 和 'useragent' 已被弃用。不,我正在寻找一个 userFunc 用作检测移动设备的条件。我的目标是在移动设备上隐藏或显示一些特殊页面。我使用了扩展名“contexts_wurfl”一段时间,但我想应该有“更小的解决方案”。
谢谢你的帮助。
您可以使用 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, ...]
。
如果您想避免编写自定义用户函数globalString
,Typo3 的函数仍然可以在 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
可以在此处找到使用条件的详细文档。
可以在此处找到可与该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]