This is a lightweight mobile detection based on the Mobile_Detect.php library, which can be obtained from the GitHub repository.

This module is intended to aid developers utilizing mobile-first and responsive design techniques who also have a need for slight changes for mobile and tablet users. An example would be showing (or hiding) a block display for a particular device.

This module is not intended (and never will be enhanced) to provide theme switching or redirection; other modules already provide this functionality.


Ready for Drupal 8, 9 and 10

  • It extends the Twig Extensions.
  • Also creates conditions for any block entity thanks to @super_romeo
  • Provides PHP conditions to improve your code.
  • Uses composer to get the MobileDetect library thanks to @sarathkm
  • Cache contexts contributed by @super_romeo

Installation

Use composer to install the module dependencies.
composer require 'drupal/mobile_detect:^3.0'

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() %}

About the cache contexts:

By design, the "Internal Page Cache" core module assumes that all pages served to anonymous users will be identical, regardless of the implementation of cache contexts.

If you want to use the mobile_detect cache contexts to vary the content served to anonymous users, "Internal Page Cache" must be disabled, and the performance impact that entails incurred.

More info: https://www.drupal.org/docs/drupal-apis/cache-api/cache-contexts

Project information

Releases