You are here

mobile_device_detection.module in Mobile Device Detection 8

Same filename and directory in other branches
  1. 8.3 mobile_device_detection.module
  2. 8.2 mobile_device_detection.module

File

mobile_device_detection.module
View source
<?php

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\ViewExecutable;

/**
 * Implements hook_help().
 */
function mobile_device_detection_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.mobile_device_detection':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('"Mobile device detection" module can detect any mobile device. You can use it via service or "Views". This module integrate with "Views" and you can easily to switch "Views display" for different devices.') . '</p>';
      $output .= '<p>' . t('For more information, see the <a href="https://www.drupal.org/project/mobile_device_detection">Mobile device detection</a>.') . '</p>';
      $output .= '<h3>' . t('Usage') . '</h3>';
      $output .= '<p>' . t('Initialization:') . '</p>';
      $output .= '<p>' . t('$entity = \\Drupal::service("object.mdd");') . '</p>';
      $output .= '<hr>';
      $output .= '<p>' . t('Implementation:') . '</p>';
      $output .= '<p>' . t('if($entity->isMobile()) { $entity->getObject(); }') . '</p>';
      $output .= '<p>' . t('if($entity->isTablet()) { $entity->getObject(); }') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_install().
 */
function mobile_device_detection_install() {

  // Enable mobile_device_detection plugin.
  $config = \Drupal::service('config.factory')
    ->getEditable('views.settings');
  $display_extenders = $config
    ->get('display_extenders') ?: array();
  $display_extenders[] = 'mobile_device_detection';
  $config
    ->set('display_extenders', $display_extenders);
  $config
    ->save();
}

/**
 * Implements hook_uninstall().
 */
function mobile_device_detection_uninstall() {

  // Disable mobile_device_detection plugin.
  $config = \Drupal::service('config.factory')
    ->getEditable('views.settings');
  $display_extenders = $config
    ->get('display_extenders') ?: array();
  $key = array_search('mobile_device_detection', $display_extenders);
  if ($key !== FALSE) {
    unset($display_extenders[$key]);
    $config
      ->set('display_extenders', $display_extenders);
    $config
      ->save();
  }
}

/**
 * Implements hook_views_post_execute().
 */
function mobile_device_detection_views_post_execute(ViewExecutable $view) {
  $display = $view
    ->getDisplay();
  $extenders = $display
    ->getExtenders();
  if (!isset($extenders['mobile_device_detection'])) {
    return;
  }
  $devices = array_filter($extenders['mobile_device_detection']
    ->getDevices());
  if (!empty($devices)) {
    \Drupal::service('page_cache_kill_switch')
      ->trigger();
    $view->element['#cache']['contexts'] = [
      'cache_context.session',
    ];
    $view->element['#cache']['max-age'] = 0;
    $entity = \Drupal::service('object.mdd');
    $view->build_info['fail'] = true;
    foreach ($devices as $key => $value) {
      if ($key != 'desktop') {
        $func = 'is' . ucfirst($value);
        if (is_callable(array(
          $entity,
          $func,
        )) && $entity
          ->{$func}()) {
          $view->build_info['fail'] = false;
        }
      }
      else {
        if (!$entity
          ->isMobile() && !$entity
          ->isTablet()) {
          $view->build_info['fail'] = false;
        }
      }
    }
  }
}