1

I am using Templavoilà Plus and I have one field which the user complete with a title called field_title. I use this field to create an URL/HTML for other fields of my FCE, using Typoscript Object Path.

Constants:

file = fileadmin/datasheets/|.pdf

Setup:

lib.field_datasheet = TEXT
lib.field_datasheet {
 field = field_title
 wrap = <a href="{$file}"></a>
}

The problem is that I want to do that only if the file/URL exists. First, I thought of checking if the URL I create didn't link to a 404 page. Then I thought it would be easier to check if the file size wasn't 0. After working on that for two days, and after looking everywhere, I realized neither of these solutions were easy…

I think I have bits of answers, using stdWrap.rawUrlEncode, file:current:size, if.isTrue.data, FILE, etc… but obviously, Typoscript isn't made for beginners, and I can't seem to find how to put everything in the right order, and how to use all this properly.

I think I could manage the condition using something like :

if.isTrue < .10 #.10 containing the size of the file
if.value = 0

But the part which really is difficult is getting the file size from a URL I created using Typoscript. I'm pretty there must be a way to do that only with Typoscript since I know one can get the size of a file from its uid…

Or maybe I should just stick to checking if that URL don't lead to a 404 page. Or maybe there is a simpler solution I didn't think about!

Any help would be greatly appreciated, sorry if that problem is too easy to solve, and thank you very much already for reading this post ! =)

EDIT : I'm using Typo3 7.6

CCR
  • 154
  • 4
  • 18

3 Answers3

2

You can probably use filelink for that. Without testing it, it should be something like:

lib.field_datasheet = TEXT
lib.field_datasheet {
  field = field_title
  wrap = |.pdf
  filelink {
    path = fileadmin/datasheets/
    file.field = field_title
  }
}

Not sure what that does if the file isn't there though. It probably shows nothing, in which case you can use ifEmpty.field = field_title to just show the text.

More on filelink: https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Filelink.html

CCR
  • 154
  • 4
  • 18
Rudy Gnodde
  • 4,286
  • 2
  • 12
  • 34
  • Thank you very much! Using filelink was the solution! I'll add the code I've used in comment. I often find that Typo3 documentation lack concrete exemples, so that might be useful to some =) – CCR Jul 12 '18 at 10:30
  • Would you know how to make it **not** case sensitive? So that it could either be .pdf or .PDF. Same for the `field_title`. – CCR Oct 17 '18 at 13:32
1

Why don't you let TYPO3 decide whether the file exist and it's worth to build a link?

Your problem could be that you have nothing to become linked. (empty A-tag)

I would try something like:

Constants:

filePath = fileadmin/datasheets/|.pdf

Setup:

lib.field_datasheet = TEXT
lib.field_datasheet {
   // use the 'filename' as text to be linked
   field = field_title
   // now build the link:
   typolink.parameter {
       field = field_title
       wrap = {$filePath}|
   }
}
Bernd Wilke πφ
  • 10,390
  • 1
  • 19
  • 38
  • Thank you very much for your answer. I have tried your answer, it works and it's better than wrapping into |. However, a link is still being created even if the file doesn't exists… – CCR Jul 06 '18 at 09:32
  • The default behaviour of TYPO3 is: if the target of a link is unavailable don't generate the link (just show the text to be linked). So I would consider an empty link as a bug and you should open a bug ticket on https://forge.typo3.org – Bernd Wilke πφ Jul 09 '18 at 06:02
  • Sorry, maybe it wasn't clear, the link just links to a 404 Not Found. I'm trying to find where the default behaviour is explained, I can't find anything about unavailable links here https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Typolink/Index.html – CCR Jul 10 '18 at 13:16
  • Is not only when you link to an uid that the link is not generated if the uid doesn't exist ? I feel like if you write the full URL, it doesn't work the same. The question then might be, is it possible/relevant to make a uid out of a URL? – CCR Jul 10 '18 at 13:28
  • I don't know whether the behaviour for unavailable links is documented at all. you probably need to inspect the PHP sources of the link generation as it is not only about typoscript links. it's the same for fluid or any other link generated by an API call. – Bernd Wilke πφ Jul 11 '18 at 06:14
1

Thanks to Rudy's answer, I've been able to solve my problem. So that it can be useful to others, here is the bit of Typoscript I've used :

lib.field_datasheet = TEXT
lib.field_datasheet {
  value {
    field = field_title
    wrap = |.pdf
    }

  filelink {
    path = /fileadmin/datasheet/
    target = _blank
    altText = PDF icon
    titleText = Download datasheet

    //Personalize the icon, the file must be named pdf.png            
    icon_link = 1
    icon = 1
    icon {
      path = fileadmin/icons/
      ext = png
      }
    }
 }
CCR
  • 154
  • 4
  • 18