Nov. 24., 2008

How to link to a file in a FE plugin of TYPO3?

By Steffen Müller. Licensed under the Creative Commons License

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:

--> Back to the list of articles

License

Licensed under creative commonsThis article is licensed under the Creative Commons License CC BY-SA 3.0. You are free to share (copy, distribute and transmit) and to remix (to adapt) the work under the following conditions:

  • You must attribute the work by mentioning the name of the author (Steffen Müller) and setting a link back to the original article using its URL.
  • If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

Comments


Leave a comment:

This page uses static caches. Make sure you reload the page in your browser after posting a comment.

(will not be published)

CAPTCHA image for SPAM prevention Click here for audio version of the word to enter.

If you can't read the captcha word, please click to load a new image.
(You need Javascript turned on. Otherwise press the submit button and wait until the page has reloaded.)