How to link to a file in a FE plugin of TYPO3?
This short snippet demonstrates the essentials of how to create links to files in FE plugins. It reveals the power of cObjects and TypoScript. This technique is typical for TYPO3 and it shows you why TYPO3 can be so amazing.
This posting is not meant as a tutorial shipping complete sources, but a snippet to give you an impression of the essentials. So don't be disappointed if you won't find some ready-to-paste snippets.
The idea is to create a link to a file, which is set in the media field of the page. You can find this field in the page properties below the ressources tab and its label in the backend is "Files".
Once you have chosen the file in the backend, it gets copied to the uploads/media/ directory. In the plugin, we need to get the filename and then simply build a link around it. "That's easy", you might think, "just get the filename from TSFE and put some a href tag around it and it's finished". But as this snippet will show, you don't need to think about HTML at all. The magic lies inside the filelink function of TypoScript.
// path to the file
$filePath = 'uploads/media/'.$GLOBALS['TSFE']->page['media'];
// use the filelink function to create a rich feature link
$content = $this->cObj->filelink(
$filePath,
$this->conf['fileLink.']
);
This is only a few lines of PHP code! Next we need to configure the filelink with in the TypoScript setup of the plugin:
plugin.tx_myplugin_pi1 {
fileLink {
file.noTrimWrap = | | |
labelStdWrap.data = page:media
icon = 1
icon_link = 1
size = 1
size.wrap = (|)
size.bytes = 1
size.bytes.labels = " B| KB| MB| GB"
}
}
The result is a full featured link with an icon corresponding to the content type and the size of the file: