Extbase FAL file download

11,793

Solution 1

You can use the VHS viewhelper for that. Check it out the following code:

<v:resource.file additionalAttributes="{foo: 'bar'}" data="{foo: 'bar'}" identifier="[mixed]" categories="[mixed]" treatIdAsUid="1" treatIdAsReference="1" as="NULL">
    <!-- tag content - may be ignored! -->
</v:resource.file>

Link to the documentation: https://fluidtypo3.org/viewhelpers/vhs/master/Resource/FileViewHelper.html

You have to install the vhs extension and also to add the namespace on your template file.

{namespace v=FluidTYPO3\Vhs\ViewHelpers}

As a last thing you should have the files indexed in typo3. For that create a scheduler task and select the File Abstraction Layer: Extract metadata in storage task.

Solution 2

You can use the publicURL property of the originalResource to create a file link.

Sample Fluid to generate a list of files referenced in your domain model property

<ul class="download-list">
    <f:for each="{myObject.myFALproperty}" as="file" >
        <li><a href="{file.originalResource.publicUrl}">{file.originalResource.title}</a></li>
    </f:for>
</ul>

Just use the {myObject.myFALproperty} to see what you can access.

If you need more properties here are the basics of the Original and the reference in case you want to use the override located in the FlexForm

Original Attributes

filename :{myObject.myFALproperty.originalResource.originalFile.name}
title:{myObject.myFALproperty.originalResource.originalFile.title}
description:{myObject.myFALproperty.originalResource.originalFile.description}
alt:{myObject.myFALproperty.originalResource.originalFile.alternative}
uid:{myObject.myFALproperty.originalResource.originalFile.uid}
path:{myObject.myFALproperty.originalResource.publicUrl}

Reference Attributes

title: {myObject.myFALproperty.originalResource.title}
description {myObject.myFALproperty.originalResource.description}

Hope that helps.

Just for reference this is what the according TCA looks like, but you obviously already know that (note this sample has a maxitems setting of 10:

'myFALproperty' => array(
    'exclude' => 0,
    'label' => 'LLL:EXT:xy_sample/Resources/Private/Language/locallang_db.xlf:tx_xysample_domain_model_myobject.myFALproperty',
    'config' => 
    \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
        'myFALproperty',
        array('maxitems' => 10)
    ),
),

Solution 3

Step 1/2

You can get the URI to the PDF file like this:

In your FLUID Template:

<f:uri.image src="{file.uid}" treatIdAsReference="1"/> 

OUTPUT /path/to/your/pdf

This works even if "originalResource is null for file", like you stated in your question.

Step 2/2

Create a link in your FLUID Template (using inline notation)

<a href="{f:uri.image(src:'{file.uid}', treatIdAsReference: 1)}">
Download PDF
</a>

Test environment

  • typo3 v7.6.19
Share:
11,793
smitrovic
Author by

smitrovic

Updated on August 02, 2022

Comments

  • smitrovic
    smitrovic almost 2 years

    I have question about file download with Extbase and FAL. I can render image with

    <f:image src="{file.uid}" alt="" width='100' height="100" treatIdAsReference="1"/> 
    

    I can get image, but I also have PDF file for download, and I can't use this ViewHelper. Is there any other ViewHelper for file downloads? If i dump that file i get this:

    file => TYPO3\CMS\Extbase\Domain\Model\FileReferenceprototypepersistent entity (uid=1342, pid=310)
             originalResource => NULL
             uid => 1342 (integer)
             _localizedUid => 1342 (integer)modified
             _languageUid => 0 (integer)modified
             pid => 310 (integer)
    

    originalResource is null for file, and for image, image is printed with that ViewHelper. And I can't get file...

    Any help is welcome...

  • Ghanshyam Gohel
    Ghanshyam Gohel over 7 years
    Link:{myObject.myFALproperty.originalResource.link}
  • Uwe Schmelzer
    Uwe Schmelzer almost 6 years
    Oh man, I do love wordpress. It is so much less complicated than typo3. And it achives the same things...