Skip to content

Instantly share code, notes, and snippets.

@dergachev
Last active November 18, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dergachev/664b3263616a785cb434 to your computer and use it in GitHub Desktop.
Save dergachev/664b3263616a785cb434 to your computer and use it in GitHub Desktop.
name = EW Mobile Switcher
description = Allows switching to desktop version and back on mobile site.
dependencies[] = block
core = 7.x
<?php
/**
* @file ew_mobile_switcher.module
*
* Handles the version switch by adding/removing the class
* "mobile-version-enabled" when called from the block link
*/
/**
* Implements hook_init().
*/
function ew_mobile_switcher_init() {
if (isset($_GET['force_desktop']) && is_numeric($_GET['force_desktop'])) {
$_SESSION['force_desktop'] = $_GET['force_desktop'];
//redirect to the same URL, but without the force_desktop arg
$url = drupal_parse_url(request_uri());
unset($url['query']['force_desktop']);
drupal_goto($url['path'], $url);
}
}
/**
* Helper fuction to check if the mobile view has been suppressed.
*/
function _ew_mobile_switcher_should_force_desktop() {
return $_SESSION['force_desktop'] == 1;
}
/**
* Implements template_preprocess_html().
*/
function ew_mobile_switcher_preprocess_html(&$vars){
// By default, the "mobile-version-enabled" class is set as default
if (!_ew_mobile_switcher_should_force_desktop()) {
$vars['classes_array'][] = 'mobile-version-enabled';
}
}
/**
* Implements hook_block_info().
*/
function ew_mobile_switcher_block_info() {
$blocks = array();
$blocks['switch-version'] = array(
'info' => t('Switch Mobile/Desktop Version Block'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function ew_mobile_switcher_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'switch-version' :
$block['subject'] = '';
$block['content'] = _ew_mobile_switcher_block_content();
break;
default:
break;
}
return $block;
}
/**
* Block view helper.
*/
function _ew_mobile_switcher_block_content(){
// Parse the current URI, get ready to insert extra query arg.
$uri = drupal_parse_url(request_uri());
if(_ew_mobile_switcher_should_force_desktop()) {
$text = t('Switch to Mobile Version');
$uri['query']['force_desktop'] = 0;
} else {
$text = t('Switch to Desktop Version');
$uri['query']['force_desktop'] = 1;
}
return render(l($text, $uri['path'], $uri));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment