0

I'm trying to create sys_file_references in a scheduler task in TYPO3 7.6.

But in the created sys_file_references are only Title and Description to see, the file is missing.

My model class contains this:

    /**
     * uid of a sys_file
     *
     * @var integer
     */
    protected $originalFileIdentifier;

    /**
     * setOriginalResource
     *
     * @param \TYPO3\CMS\Core\Resource\FileReference $originalResource
     * @return void
     */
    public function setOriginalResource(\TYPO3\CMS\Core\Resource\FileReference $originalResource) {
        $this->originalResource = $originalResource;
        $this->originalFileIdentifier = (int)$originalResource->getOriginalFile()->getUid();
    }


    /**
     * @return \TYPO3\CMS\Core\Resource\AbstractFile|\TYPO3\CMS\Core\Resource\FileReference|\TYPO3\CMS\Core\Resource\Folder|\TYPO3\CMS\Core\Resource\ResourceInterface
     * @throws \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
     */
    public function getOriginalResource()
    {
        if ($this->originalResource === null) {
            $this->originalResource = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileReferenceObject($this->getUid());
        }

        return $this->originalResource;
    }

    /**
     * setFile
     *
     * @param \TYPO3\CMS\Core\Resource\File $falFile
     * @return void
     */
    public function setFile(\TYPO3\CMS\Core\Resource\File $falFile) {
        $this->originalFileIdentifier = (int)$falFile->getUid();
    }

Task code:

            $contentElement = $contentRepository->findByUid(123);

            if( file_exists($pathToFile)) {
                /** @var File $file */
                $file = $resourceFactory->getFileObjectFromCombinedIdentifier('1:/'.$pathToFile);

                /** @var SysFileReference $newFileReference */
                $newFileReference = $objectManager->get('VENDOR\\MyExtension\\Domain\\Model\\SysFileReference');

                $newFileReference->setFile($file);
                $newFileReference->setTitle('The Title');
                $newFileReference->setDescription('Lorem Ipsum Dolor');

                $contentElement->addMedia($newFileReference);
                $contentRepository->update($contentElement);
                $persistanceManager->persistAll();
            }

In the created sys_file_reference is the file missing: a busy cat
(source: honrath.de)

The $file I try to attach: a busy cat
(source: honrath.de)

2 Answers 2

2

It is very tricky to create file references with Extbase. Your best way is direct database manipulation:

protected function createFileReference($contentUid, $pid, $fieldName, File $file)
{
    $data = [
        'sys_file_reference' => [
            StringUtility::getUniqueId('NEW') => [
                'table_local' => 'sys_file',
                'uid_local' => $file->getProperty('uid'),
                'tablenames' => 'tt_content',
                'uid_foreign' => $contentUid,
                'fieldname' => $fieldName,
                'pid' => $pid,
            ]
        ]
    ];
    $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
    $dataHandler->start($data, []);
    $dataHandler->admin = true;
    $dataHandler->process_datamap();
}

The example above is for the Backend mode. You should use DataHandler there because it would also update various system tables.

0
0

This function deletes (set deleted=0) for a sys_file_reference

    protected function deleteFileReference($uid)
    {
        $cmd['sys_file_reference'][$uid]['delete'] = 1;
        $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
        $dataHandler->start([], $cmd);
        $dataHandler->admin = true;
        $dataHandler->process_cmdmap();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.