Search code examples
typo3extbasetypo3-9.xfilereference

TYPO3 9 Extbase EXT originalResource of FileReference is always NULL. Why?


I'm developing my own Extension. In this Extension it is possible to add Images to a content element. In the Backend viewing everything is working fine. In the DB the reference of sys_file to my database table via sys_file_reference looks good too.
But when I place <f:debug>{_all}</f:debug> in the template, the originalResource of the File is NULL.

Can somone tell me, what I'm missing?

Here is some code of my Extension: ext_tables.sql

#
# Table structure for table 'tx_redspaceproduct_domain_model_product'
#
CREATE TABLE tx_redspaceproduct_domain_model_product (

uid int(11) unsigned NOT NULL AUTO_INCREMENT,
pid int(11) unsigned DEFAULT '0' NOT NULL,

number int(5) DEFAULT '0' NOT NULL,
name varchar(255) DEFAULT '' NOT NULL,
slug varchar(255) DEFAULT '' NOT NULL,
subname varchar(255) DEFAULT '' NOT NULL,
image int(11) unsigned NOT NULL default '0',
flyer int(11) unsigned NOT NULL default '0',
featureteaser text DEFAULT '',
description text DEFAULT '',
specifications text DEFAULT '',
features text DEFAULT '',
accuracy text DEFAULT '',
category int(11) unsigned DEFAULT '0' NOT NULL,
type int(11) unsigned DEFAULT '0' NOT NULL,
sibling int(11) unsigned DEFAULT '0' NOT NULL,
related int(11) unsigned DEFAULT '0' NOT NULL,
accessory int(11) unsigned DEFAULT '0' NOT NULL,
package int(11) unsigned DEFAULT '0' NOT NULL,
download int(11) unsigned DEFAULT '0' NOT NULL,

hidden int(1) unsigned DEFAULT '0' NOT NULL,
deleted int(1) unsigned DEFAULT '0' NOT NULL,

PRIMARY KEY (uid),
FOREIGN KEY (category) REFERENCES tx_redspaceproduct_domain_model_category(uid),
FOREIGN KEY (type) REFERENCES tx_redspaceproduct_domain_model_type(uid)

);

tx_redspaceproduct_domain_model_product.php

'image' => [
        'exclude' => true,
        'label' => 'LLL:EXT:redspace_product/Resources/Private/Language/locallang_db.xlf:tx_redspaceproduct_domain_model_product.image',
        'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
            'image',
            [
                'appearance' => [
                    'createNewRelationLinkTitle' => 'LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:images.addFileReference'
                ],
                'overrideChildTca' => [
                    'types' => [
                        'foreign_types' => [
                            '0' => [
                                'showitem' => '
                                --palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
                                --palette--;;filePalette'
                            ],
                            \TYPO3\CMS\Core\Resource\File::FILETYPE_TEXT => [
                                'showitem' => '
                                --palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
                                --palette--;;filePalette'
                            ],
                            \TYPO3\CMS\Core\Resource\File::FILETYPE_IMAGE => [
                                'showitem' => '
                                --palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
                                --palette--;;filePalette'
                            ],
                            \TYPO3\CMS\Core\Resource\File::FILETYPE_AUDIO => [
                                'showitem' => '
                                --palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
                                --palette--;;filePalette'
                            ],
                            \TYPO3\CMS\Core\Resource\File::FILETYPE_VIDEO => [
                                'showitem' => '
                                --palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
                                --palette--;;filePalette'
                            ],
                            \TYPO3\CMS\Core\Resource\File::FILETYPE_APPLICATION => [
                                'showitem' => '
                                --palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
                                --palette--;;filePalette'
                            ]
                        ],
                    ],
                ],
                'maxitems' => 99
            ],
            $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
        ),
    ],

Product.php

<?php
namespace REDSPACE\RedspaceProduct\Domain\Model;

use \TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use \TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use \TYPO3\CMS\Extbase\Domain\Model\FileReference;

/***
 *
 * This file is part of the "Redspace Product" Extension for TYPO3 CMS.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 *  (c) 2019 Robin Probst <[email protected]>, REDSPACE AG
 *
 ***/

/**
 * Product
*/
class Product extends AbstractEntity
{

    /**
     * image
     * 
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<TYPO3\CMS\Extbase\Domain\Model\FileReference>
     * @TYPO3\CMS\Extbase\Annotation\ORM\Cascade("remove")
     */
    protected $image = null;

    /**
     * __construct
     */
    public function __construct()
    {
        $this->initStorageObjects();
    }

    /**
     * Initializes all ObjectStorage properties
     * 
     * @return void
     */
    protected function initStorageObjects()
    {
        $this->file = new ObjectStorage();
    }

    /**
     * Returns the image
     * 
     * @return ObjectStorage<FileReference> $image
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * Sets the image
     * 
     * @param ObjectStorage<FileReference> $image
     * @return void
     */
    public function setImage(ObjectStorage $image)
    {
        $this->image = $image;
    }
}

ProductRepository.php:

<?php
namespace REDSPACE\RedspaceProduct\Domain\Repository;

use \TYPO3\CMS\Extbase\Persistence\Repository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;

/***
 *
 * This file is part of the "Redspace Product" Extension for TYPO3 CMS.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 *  (c) 2019 Robin Probst <[email protected]>, REDSPACE AG
 *
 ***/

/**
 * The repository for Products
 */
class ProductRepository extends Repository
{
    protected $tableName = 'tx_redspaceproduct_domain_model_product';

    public function getProductOfCategory($category) {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)- >getQueryBuilderForTable($this->tableName);
        $statement = $queryBuilder
            ->select('*')
            ->from($this->tableName)
            ->where($queryBuilder->expr()->like('category', $category))
            ->execute();
   
        $rows = $statement->fetchAll();

        return $rows;
    }

    public function getFlyerIdentifier($uid) {
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)- >getQueryBuilderForTable($this->tableName);
        $statement = $queryBuilder
            ->select('identifier')
            ->from('sys_file')
            ->leftJoin('sys_file','sys_file_reference','sys_file_reference',$queryBuilder->expr()->eq('sys_file_reference.uid_local', $queryBuilder->quoteIdentifier('sys_file.uid')))
            ->where($queryBuilder->expr()->eq('sys_file_reference.uid_foreign', $uid))
            ->andWhere($queryBuilder->expr()->eq('extension', '"pdf"'))
            ->execute();
   
        $rows = $statement->fetchAll();

        return $rows;
    }
}

This is the Output of f:debug:
This is the Output of f:debug:

Deprecation Log: Deprecation Log


Solution

  • you can access the originalResource in the debug-ViewHelper if you call it directly:

    <f:debug>{image.originalResource}</f:debug>
    

    Reason: https://forge.typo3.org/issues/66727#note-2