You are here

class MobileDetectTwig in Mobile Detect 8.2

MobileDetectTwig class.

Hierarchy

  • class \Drupal\mobile_detect\Twig\MobileDetectTwig extends \Twig\Extension\AbstractExtension

Expanded class hierarchy of MobileDetectTwig

1 string reference to 'MobileDetectTwig'
mobile_detect.services.yml in ./mobile_detect.services.yml
mobile_detect.services.yml
1 service uses MobileDetectTwig
mobile_detect.twig.extension in ./mobile_detect.services.yml
Drupal\mobile_detect\Twig\MobileDetectTwig

File

src/Twig/MobileDetectTwig.php, line 13

Namespace

Drupal\mobile_detect\Twig
View source
class MobileDetectTwig extends AbstractExtension {

  /**
   * Mobile detect service.
   *
   * @var \Detection\MobileDetect
   */
  protected $mobileDetector;

  /**
   * Constructor
   * @param MobileDetect $mobileDetector
   */
  public function __construct(MobileDetect $mobileDetector) {
    $this->mobileDetector = $mobileDetector;
  }

  /**
   * Get extension twig function
   * @return array
   */
  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',
      ]),
    ];
  }

  /**
   * Is mobile
   * @return boolean
   */
  public function isMobile() {
    return $this->mobileDetector
      ->isMobile();
  }

  /**
   * Is tablet
   * @return boolean
   */
  public function isTablet() {
    return $this->mobileDetector
      ->isTablet();
  }

  /**
   * Is device
   * @param string $deviceName is[iPhone|BlackBerry|HTC|Nexus|Dell|Motorola|Samsung|Sony|Asus|Palm|Vertu|...]
   * @return boolean
   */
  public function isDevice($deviceName) {
    $methodName = 'is' . strtolower((string) $deviceName);
    return $this->mobileDetector
      ->{$methodName}();
  }

  /**
   * Is iOS
   * @return boolean
   */
  public function isIOS() {
    return $this->mobileDetector
      ->isIOS();
  }

  /**
   * Is Android OS
   * @return boolean
   */
  public function isAndroidOS() {
    return $this->mobileDetector
      ->isAndroidOS();
  }

  /**
   * Extension name
   * @return string
   */
  public function getName() {
    return 'mobile_detect.twig.extension';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MobileDetectTwig::$mobileDetector protected property Mobile detect service.
MobileDetectTwig::getFunctions public function * Get extension twig function *
MobileDetectTwig::getName public function * Extension name *
MobileDetectTwig::isAndroidOS public function * Is Android OS *
MobileDetectTwig::isDevice public function * Is device *
MobileDetectTwig::isIOS public function * Is iOS *
MobileDetectTwig::isMobile public function * Is mobile *
MobileDetectTwig::isTablet public function * Is tablet *
MobileDetectTwig::__construct public function * Constructor *