Added utility to detect mobile/table user agents server side
This commit is contained in:
60
src/Twig/MobileDetectExtension.php
Normal file
60
src/Twig/MobileDetectExtension.php
Normal 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 [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user