Guest User

Untitled

a guest
Oct 16th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. <?php
  2.  
  3. class Def_MailTools extends ContentController
  4. {
  5.  
  6.  
  7. /**
  8. * Replace a "[sitetree_link id=n]" shortcode with a link to the page with the corresponding ID.
  9. *
  10. * @return string
  11. */
  12. public static function absolute_link_shortcode_handler($arguments, $content = null, $parser = null) {
  13. if(!isset($arguments['id']) || !is_numeric($arguments['id'])) return;
  14.  
  15. if (
  16. !($page = DataObject::get_by_id('SiteTree', $arguments['id'])) // Get the current page by ID.
  17. && !($page = Versioned::get_latest_version('SiteTree', $arguments['id'])) // Attempt link to old version.
  18. && !($page = DataObject::get_one('ErrorPage', '"ErrorCode" = \'404\'')) // Link to 404 page directly.
  19. ) {
  20. return; // There were no suitable matches at all.
  21. }
  22.  
  23. if($content) {
  24. return sprintf('<a href="%s">%s</a>', $page->AbsoluteLink(), $parser->parse($content));
  25. } else {
  26. return $page->AbsoluteLink();
  27. }
  28. }
  29.  
  30. function Build($content)
  31. {
  32.  
  33. // Change internal links ([sitetree_link id=2]) to proper absolute links
  34. $parser = new ShortcodeParser();
  35. $parser->register('absolute_link_shortcode_handler', array('Def_MailTools', 'absolute_link_shortcode_handler'));
  36. $content = $parser->parse($content);
  37.  
  38. return $content;
  39. }
  40.  
  41.  
  42. }
Add Comment
Please, Sign In to add comment