Added utility to detect mobile/table user agents server side

This commit is contained in:
2024-11-12 17:07:54 +00:00
parent 75c31f0506
commit 3afcc69531
3 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
<?php
/*
* AVSDev UF Tweaks (https://avsdev.uk)
*
* @link https://git.avsdev.uk/avsdev/sprinkle-uf-tweaks
* @license https://git.avsdev.uk/avsdev/sprinkle-uf-tweaks/blob/master/LICENSE.md (LGPL-3.0 License)
*/
namespace UserFrosting\Sprinkle\UFTweaks\Twig;
use Detection\MobileDetect;
use Psr\Container\ContainerInterface;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use Twig\TwigFunction;
/**
* Extends Twig functionality for mobile detection.
*
* @author Craig Williams (https://avsdev.uk)
*/
class MobileDetectExtension extends AbstractExtension implements GlobalsInterface
{
/**
* @param ContainerInterface $services
*/
public function __construct(ContainerInterface $services)
{
}
public function getName()
{
return 'avsdev/uf-tweaks-mobileDetect';
}
public function getFunctions()
{
return [
// Add Twig function for checking permissions during dynamic menu rendering
new TwigFunction('isMobile', function () {
$detect = new MobileDetect();
return $detect->isMobile();
}),
new TwigFunction('isTablet', function () {
$detect = new MobileDetect();
return $detect->isTablet();
}),
new TwigFunction('isDesktop', function () {
$detect = new MobileDetect();
return !$detect->isTablet() && !$detect->isMobile();
}),
];
}
public function getGlobals()
{
return [];
}
}