Created
September 3, 2015 11:58
-
-
Save acabouet/bca88bc9f0cd0984fa29 to your computer and use it in GitHub Desktop.
Example of a custom module - this one extends the Domain contrib module to create a custom block with a region select.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function update_domain_enable() { | |
domain_bootstrap_register(); | |
} | |
function update_domain_domain_bootstrap_full($domain) { | |
if (function_exists('drush_verify_cli') || function_exists('update_prepare_d7_bootstrap')) return; | |
module_invoke('update_domain', 'initialize', 'domain'); | |
} | |
function update_domain_init() { | |
$currentkey = $GLOBALS['_domain']['domain_id']; | |
$current = domain_get_domain(); | |
$key = $_COOKIE['labnetRegion']; | |
$preferred = domain_lookup($key); | |
if(isset($preferred) && isset($key)) { | |
if($key != $currentkey && ($preferred['valid'] || user_access('access inactive domains'))) { | |
if(!drupal_is_front_page()) { | |
if ($key != 0 && $key != -1) { | |
setdomaincookie($key); | |
domain_goto($preferred); | |
domain_set_domain($key, FALSE); | |
cache_clear_all(); | |
} elseif ($key == 0) { | |
setdomaincookie($key); | |
$update = domain_default(); | |
domain_goto($update); | |
domain_set_domain($key, FALSE); | |
cache_clear_all(); | |
} elseif ($key == -1) { | |
$key = 0; | |
setdomaincookie($key); | |
$update = domain_default(); | |
domain_goto($update); | |
domain_set_domain($key, FALSE); | |
cache_clear_all(); | |
} | |
} else { | |
setdomaincookie($key); | |
domain_goto($preferred); | |
domain_set_domain($key, FALSE); | |
cache_clear_all(); | |
} | |
} elseif ($key == $currentkey) { | |
if($key == 0 || $key == -1) { | |
if($key == -1) { | |
$key == 0; | |
} | |
setdomaincookie($key); | |
$update = domain_default(); | |
domain_goto($update); | |
cache_clear_all(); | |
} else { | |
setdomaincookie($key); | |
domain_goto($current); | |
domain_set_domain($currentkey, FALSE); | |
cache_clear_all(); | |
} | |
} | |
if($key == 0) { | |
drupal_set_message(t('Does this page seem to be missing content? Please choose your region by selecting from the options below.')); | |
} | |
} | |
} | |
function setdomaincookie($newkey) { | |
$cookie = $_COOKIE['labnetRegion']; | |
$params = session_get_cookie_params(); | |
$cookie_domain = 'labnetinternational.com'; | |
if(isset($cookie)) { | |
if($cookie != $newkey) { | |
setcookie('labnetRegion', $newkey, 0, $params['path'], $cookie_domain, $params['secure'], $params['httponly']); | |
} | |
} else { | |
setcookie('labnetRegion', $newkey, 0, $params['path'], $cookie_domain, $params['secure'], $params['httponly']); | |
} | |
} | |
function update_domain_select_region_form($form, &$form_state) { | |
$options = array(); | |
$company = 'Labnet International, Inc.'; | |
foreach (domain_domains() as $data) { | |
($data['domain_id'] == 0) ? $key = -1 : $key = $data['domain_id']; | |
if ($data['valid'] || user_access('access inactive domains')) { | |
if($data['domain_id'] != 4) { | |
$cleandomain = rtrim(str_replace($company, '', $data['sitename'])); | |
$options[$key] = $cleandomain; | |
} | |
} | |
} | |
$default = ($GLOBALS['_domain']['domain_id'] == 0) ? -1 : $GLOBALS['_domain']['domain_id']; | |
$form['region_help_text'] = array( | |
'#type' => 'markup', | |
'#prefix' => '<div id="region-help-box">', | |
'#suffix' => '</div>', | |
'#markup' => '<h3>Choose Your Region</h3>', | |
); | |
$form['select_region'] = array( | |
'#type' => 'select', | |
'#options' => $options, | |
'#default_value' => $default, | |
'#attributes' => array( | |
'onChange' => 'document.getElementById("update-domain-select-region-form").submit();' | |
), | |
); | |
$form['submit'] = array( | |
'#name' => 'region_submit', | |
'#type' => 'submit', | |
'#value' => t('Go'), | |
'#attributes' => array( | |
'style' => 'display: none', | |
), | |
); | |
return $form; | |
} | |
function update_domain_select_region_form_submit($form, &$form_state) { | |
$key = $form_state['values']['select_region']; | |
if ($key == -1) { | |
$key = 0; | |
} | |
$newdomain = domain_lookup($key); | |
setdomaincookie($key); | |
domain_goto($newdomain); | |
domain_set_domain($key, FALSE); | |
cache_clear_all(); | |
} | |
function update_domain_block_info() { | |
$blocks = array(); | |
$blocks['select_region'] = array( | |
'info' => t('Region Select'), | |
); | |
return $blocks; | |
} | |
function update_domain_block_view($delta = '') { | |
$block = array(); | |
$block_content = array( | |
'html' => array( | |
'#type' => 'markup', | |
'#markup' => '', | |
), | |
'form' => array(), | |
); | |
$markup = ''; | |
$regionform = drupal_get_form('update_domain_select_region_form'); | |
switch($delta) { | |
case 'select_region': | |
$markup = drupal_render($regionform); | |
break; | |
} | |
$block_content['html']['#markup'] = $markup; | |
$block['content'] = $block_content; | |
return $block; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment