You are here

function mobile_device_detection_views_post_execute in Mobile Device Detection 8

Same name and namespace in other branches
  1. 8.3 mobile_device_detection.module \mobile_device_detection_views_post_execute()
  2. 8.2 mobile_device_detection.module \mobile_device_detection_views_post_execute()

Implements hook_views_post_execute().

File

./mobile_device_detection.module, line 57

Code

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;
        }
      }
    }
  }
}