Skip to content

Instantly share code, notes, and snippets.

Created May 16, 2014 00:25
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 anonymous/96cfcbae0364411ea806 to your computer and use it in GitHub Desktop.
Save anonymous/96cfcbae0364411ea806 to your computer and use it in GitHub Desktop.
static public function link_shortcode_handler($arguments, $content = null, $parser = null) {
if (!isset($arguments['id']) || !is_numeric($arguments['id']))
return;
if (
!($page = DataObject::get_by_id('SiteTree', $arguments['id'])) // Get the current page by ID.
&& !($page = Versioned::get_latest_version('SiteTree', $arguments['id'])) // Attempt link to old version.
&& !($page = DataObject::get_one('ErrorPage', '"ErrorPage"."ErrorCode" = \'404\'')) // Link to 404 page.
) {
return; // There were no suitable matches at all.
}
$link = Convert::raw2att($page->Link());
if ($content) {
return sprintf('<a href="%s">%s</a>', $link, $parser->parse($content));
} else {
return $link;
}
}
/* _config.php */
ShortcodeParser::get('default')->register('sitetree_link', array('Block_HTMLArea', 'link_shortcode_handler'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment