Problem / Motivation

We Create a service that loads the Mobile_Detect library.

Solution

Create a service to abstract the Mobile_Detect library, probably as a thin class w/ an interface. However, I am not sure how/if you can avoid using a singleton here, which would make unit testing the service harder.

CommentFileSizeAuthor
#3 2627514-mobile-detect-service.patch600 bytesdarol100

Comments

darol100 created an issue. See original summary.

darol100’s picture

Assigned: Unassigned » darol100

I will give this a shot sometime this week. Assigned to myself.

darol100’s picture

Title: Create a service to abstract the Mobile_Detect library » Create a service to load the Mobile_Detect library
Assigned: darol100 » Unassigned
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new600 bytes

Changing the description a little bit. I do not think (IMO), that we do not need a necessary to have an interface for this module since this module is specific to Mobile_detect.php library.

Adding the patch that allows having the Mobile_detect library as service.

If you wish to test this patch here is the usage of the service.

   $detect = \Drupal::service('mobile_detect.manager');
    $is_mobile = $detect->isMobile();
    $is_tablet = $detect->isTablet();

    if($is_mobile){
        echo 'Mobile';
    }
    if($is_tablet){
        echo 'Table';
    }
mpdonadio’s picture

My only thought here is that if we do this as a thin wrapper to \Mobile_Detect, then we can add methods to handle the quirks like having to check for both isMobile() and isTable() when we explicitly want just handhelds.

darol100’s picture

Assigned: Unassigned » darol100
darol100’s picture

Status: Needs review » Needs work
nonom’s picture

It's done in 8.x-2.x branch

services:
  mobile_detect:
    class: Detection\MobileDetect
  mobile_detect.twig.extension:
    class: Drupal\mobile_detect\Twig\MobileDetectTwig
    arguments: ["@mobile_detect"]
    tags:
      - { name: twig.extension }

So you can use:

$md = \Drupal::service('mobile_detect');
$md->isMobile();
$md->isTablet()
nonom’s picture

Status: Needs work » Fixed
nonom’s picture

Status: Fixed » Closed (fixed)