mobile_detect
This is a lightweight mobile detection module based on the Mobile_Detect.php library, which can be obtained from the GitHub repository.
This module is designed to assist developers using mobile-first and responsive design techniques who also need to apply slight variations for mobile and tablet users.
An example use case would be showing or hiding a block based on the type of device.
Installation
Use composer to install the module and its dependencies.
composer require 'drupal/mobile_detect:^4.0'
What does it do?
This module allows you to configure block visibility conditions based on device type or platform via the Drupal user interface.
The Mobile Detect Status block is included for testing purposes.
You can also use it programmatically in your PHP code or within Twig templates.
PHP
$md = \Drupal::service('mobile_detect');
$is_mobile = $md->isMobile();
$is_tablet = $md->isTablet();
Twig Extensions
{% if is_mobile() %}
{% if is_tablet() %}
{% if is_device('iPhone') %}
{% if is_ios() %}
{% if is_android_os() %}There are not major upgrades so moving from 3.0.0 to 4.0.0 is safe.