typo3typo3-7.6.xtypo3-8.xtypo3-extensionsfal

How do I get uid of a File Reference Object in TYPO3?


I am trying to get a file through this code $f = $resourceFactory->getFileObject($uid); but the problem is the uid is a protected field in the file reference object, as seen below so I am not able to get the uid, and getUid() obviously wont work either.

So how can I get the uid of the file reference (FAL)

/**
* A file reference object (File Abstraction Layer)
*
* @api experimental! This class is experimental and subject to change!
*/
class FileReference extends 
\TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder
{
  /**
  * Uid of the referenced sys_file. Needed for extbase to serialize the
  * reference correctly.
  *
  * @var int
  */
protected $uidLocal;

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

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

    return $this->originalResource;
}
}

Solution

  • Given you have an instance of TYPO3\CMS\Extbase\Domain\Model\FileReference then you can use getOriginalResource() to get the wrapped TYPO3\CMS\Core\Resource\FileReference. If you need the referenced file, you can then use getOriginalFile(). Thus as a chained call:

    $file = $fileReference->getOriginalResource()->getOriginalFile();
    

    Notice that you don't have to use the ResourceFactory yourself in all of this, this is taken care of internally.