Last active
August 29, 2015 14:07
-
-
Save AugustMiller/444eabf7842a441af086 to your computer and use it in GitHub Desktop.
Kirbytext extension enabling linking of files belonging to other pages
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
<? | |
class kirbytextExtended extends kirbytext { | |
function __construct($text, $markdown=true) { | |
parent::__construct($text, $markdown); | |
# A "Resource" as we're calling it needs a filename, scope and link text: | |
$this->addTags('resource'); | |
$this->addAttributes('page'); | |
$this->addAttributes('text'); | |
} | |
function resource($params) { | |
# We need to traverse the content tree, so we reference the global $site object | |
global $site; | |
# Defaults, in case one or more params are not specified | |
$defaults = array( | |
'resource' => '', | |
'page' => '', | |
'text' => '' | |
); | |
# Make our calls failsafe, through the $options associative array | |
$options = array_merge($defaults, $params); | |
# Make sure that both the page and filename resolve cleanly with the Kirby API: | |
if ( is_object( $page = $site->pages()->find($options['page']) ) && is_object($file = $page->files()->find($options['resource']) ) ) { | |
# If we're good to go, return (not print) the HTML for a link. | |
return '<a class="file" href="' . $file->url() . '">' . html($options['text']) . '</a>'; | |
} else { | |
# Otherwise, output just the text that was provided in the short-tag | |
return $options['text']; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment