<?php

/**
 * @file
 * Code for the Views Mobile module.
 */


/**
 * Implementation of hook_views_pre_view().
 */
function views_mobile_views_pre_view(&$view, &$display_id, &$args) {
  // Change the display if in mobile device.
  $display_id_mobile = $display_id . '_mobile';
  $display_id_tablet = $display_id . '_tablet';

  $has_mobile_mobile = in_array($display_id_mobile, array_keys($view->display));
  $has_mobile_tablet = in_array($display_id_tablet, array_keys($view->display));

  $detect = mobile_detect_get_object();

  if ($detect->isTablet() && $has_mobile_tablet) {
    $view->set_display($display_id_tablet);
  }
  elseif ($detect->isMobile() && $has_mobile_mobile) {
    $view->set_display($display_id_mobile);
  }

  return;
}
