MobileDetectTwig.php in Mobile Detect 8.2
File
src/Twig/MobileDetectTwig.php
View source
<?php
namespace Drupal\mobile_detect\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Detection\MobileDetect;
class MobileDetectTwig extends AbstractExtension {
protected $mobileDetector;
public function __construct(MobileDetect $mobileDetector) {
$this->mobileDetector = $mobileDetector;
}
public function getFunctions() {
return [
new TwigFunction('is_mobile', [
$this,
'isMobile',
]),
new TwigFunction('is_tablet', [
$this,
'isTablet',
]),
new TwigFunction('is_device', [
$this,
'isDevice',
]),
new TwigFunction('is_ios', [
$this,
'isIOS',
]),
new TwigFunction('is_android_os', [
$this,
'isAndroidOS',
]),
];
}
public function isMobile() {
return $this->mobileDetector
->isMobile();
}
public function isTablet() {
return $this->mobileDetector
->isTablet();
}
public function isDevice($deviceName) {
$methodName = 'is' . strtolower((string) $deviceName);
return $this->mobileDetector
->{$methodName}();
}
public function isIOS() {
return $this->mobileDetector
->isIOS();
}
public function isAndroidOS() {
return $this->mobileDetector
->isAndroidOS();
}
public function getName() {
return 'mobile_detect.twig.extension';
}
}