Created
January 27, 2015 15:08
-
-
Save vdchristelle/d404c540fdbd23f40a57 to your computer and use it in GitHub Desktop.
Provides a custom 'Back to overview' button - Block
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 | |
/** | |
* Implementation of hook_block_info(). | |
*/ | |
function the_aim_custom_block_info() { | |
$blocks = array(); | |
$blocks['back-to-overview'] = array( | |
'info' => t('Back to overview'), | |
'cache' => DRUPAL_CACHE_PER_PAGE, | |
); | |
return $blocks; | |
} | |
/** | |
* Implementation of hook_block_view(). | |
*/ | |
function the_aim_custom_block_view($delta = '') { | |
$block = array(); | |
switch ($delta) { | |
// NOTE: back to overview links will only work correctly when the right alias and patterns have been set | |
case 'back-to-overview': | |
// get path alias | |
$path_alias = explode('/', drupal_lookup_path('alias', current_path())); | |
// remove the last url section | |
array_pop($path_alias); | |
// get the original path for the url, if it exists | |
$mainpath = drupal_lookup_path('source', implode('/', array_filter($path_alias))); | |
$content = ''; | |
// Blocks won't show when content is empty, so don't add content when there is no valid back page found. | |
if (!empty($mainpath)) { | |
// make link | |
global $base_root; | |
//check if previous page is from same domain. | |
if(stripos($_SERVER['HTTP_REFERER'], $base_root) !== false) { | |
drupal_add_js(array('referer' => 'internal'), 'setting'); | |
} else { | |
drupal_add_js(array('referer' => 'external'), 'setting'); | |
} | |
$content = '<p>' . l(t('Back to overview'), $mainpath, array('attributes' => array('title' => t('Back to overview')))) . '</p>'; | |
// assign block content | |
$block['content'] = $content; | |
} | |
break; | |
} | |
return $block; | |
} |
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
$(window).load(function() { | |
//go back to last page. cfr. news?page=2 | |
if(Drupal.settings.referer == "internal") { | |
$('.backlink a').click(function(e) { | |
e.preventDefault(); | |
history.go(-1); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment