extbasetypo3-9.xtypo3-extensions

TYPO3 9.5 How to get File from stored FileReference


We have the a21glossary extension which we extend with our own extension a21glossary_file. In our extension we do a TCA override for the extension table and add our file field like this:

$fields = array(
    'tx_a21glossary_file_reference' => array(
        'label' => 'Winerap',
        'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
            'tx_a21glossary_file_reference',
            array(
                'foreign_types' => array(
                    \TYPO3\CMS\Core\Resource\File::FILETYPE_AUDIO => array(
                        'showitem' => '--palette--;;filePalette',
                    )
                ),
                'maxitems' => 1,
                'readOnly' => true,
                'foreign_match_fields' => [
                    'fieldname' => 'tx_a21glossary_file_reference',
                    'tablenames' => 'tx_a21glossary_main',
                    'table_local' => 'sys_file',
                ]
            ),
            'mp3'
        ),
    ),
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('tx_a21glossary_main', $fields);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
    'tx_a21glossary_main', // Table name
    '--palette--;Winerap;tx_a21glossary_file' // Field list to add
);

$GLOBALS['TCA']['tx_a21glossary_main']['palettes']['tx_a21glossary_file'] = array(
    'showitem' => 'tx_a21glossary_file_reference'
);

Also, the Domain/Model gets extended with the getters and setters for our new field.

So we can go to our glossary page and add list elements with the FileReference attached to it were can choose and file.

My question is: How and where can I get the actual file for the view layer because stored in the database is 0 or 1.

the model

namespace xxx\A21glossaryFile\Domain\Model;


use TYPO3\CMS\Extbase\Domain\Model\FileReference;

class Glossary extends \SveWap\A21glossary\Domain\Model\Glossary
{


    /**
     * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
     * @cascade remove
     */
    protected $tx_a21glossary_file_reference = null;

    /**
     * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference
     */
    public function getTxA21glossaryFileReference(): FileReference
    {
        return $this->tx_a21glossary_file_reference;
    }

    /**
     * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $tx_a21glossary_file_reference
     */
    public function setTxA21glossaryFileReference(\TYPO3\CMS\Extbase\Domain\Model\FileReference $tx_a21glossary_file_reference)
    {
        $this->tx_a21glossary_file_reference = $tx_a21glossary_file_reference;
    }
}

Before we switched to the model the audio file just got rendered by that piece, which is fully obsolete right? Now we use the model and pass the field data to the template where we just use it in our template structure.

tx_a21glossary_main {

    60 = FILES
    60 {
        references {
            table = tx_a21glossary_main
            uid.data = uid
            fieldName = tx_a21glossary_file_reference
        }
        renderObj = TEXT
        renderObj {
            stdWrap.data = file:current:publicUrl
            stdWrap.wrap = <audio class="audio" controls><source src="|" type="audio/mpeg"/></audio>

        }
    }
}

Solution

  • I assume the following (I named the field just "image") in your model:

    /**
     * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
     * @cascade remove
     */
    protected $image = null;
    
    /**
     * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
     */
    public function getImage()
    {
        return $this->image;
    }
    
    /**
     * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
     * @return void
     */
    public function setImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image)
    {
        $this->image = $image;
    }
    

    If all the types are provided, then just use the property in fluid like this:

    <f:image image="{item.image}" width="200c" height="200c" alt="Foo Bar"/>
    

    If you need further information from the actual image, do it like this:

    <f:image image="{item.image}" width="200c" height="200c" alt="{item.image.originalResource.originalFile.alternative}"/>
    // or
    {item.image.originalResource.publicUrl}
    

    Although the docs are not specifically for v9, the code there should still work.

    I guess the part

    'foreign_match_fields' => [
        'fieldname' => 'tx_a21glossary_file_reference',
        'tablenames' => 'tx_a21glossary_main',
        'table_local' => 'sys_file',
    ]
    

    can be safely deleted and may be the reason you just get 0/1. The connection to sys_file is the default and is done with the intermediate table sys_file_reference. Hence, the column in your table will always just contain the relation counter, not the uid of a sys_file (which is intended). The connection between your uid and the sys_file uid is stored sys_file_reference. Example TCA from a working v9 project:

    'image'            => [
        'label'  => 'FooBar Label',
        'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig('image', [
            'maxitems'         => 1,
            'appearance'       => [
                'createNewRelationLinkTitle' => 'LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:images.addFileReference'
            ],
            'overrideChildTca' => [
                'types' => [
                    '0' => [
                        'showitem' => '
                            --palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
                            --palette--;;filePalette'
                    ],
                ]
            ]
        ], $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'])
    ],