Huzaifa Mustafa
Huzaifa Mustafa

Reputation: 322

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;
}
}

Upvotes: 1

Views: 4732

Answers (2)

Dipak Parmar
Dipak Parmar

Reputation: 139

Work form me.

You can find or get file refernce uid using custom query.

In Controller :

$uid = $yourObject->getUid();
$fileReference = $this->yourRepository->getFileReferenceObject($uid);

In Repository

public function getFileRefernceHeaderLogo($uid){
        $query = $this->createQuery();
        $queryString = "SELECT *
                            FROM sys_file_reference
                                WHERE deleted = 0 
                                        AND hidden = 0 
                                            AND tablenames='your_table_name'
                                                AND fieldname='your_field_name'
                                                    AND uid_foreign =".$uid;

        $query->statement($queryString);
        return $res = $query->execute(true);            
    }

In Controller

$fileRefUid = $fileReference[0]['uid'];

Here you can get uid of file reference table.It is long process.

You can also get sys_file table uid for getFileObject.like,

$sys_file_uid = $fileReference[0]['uid_local'];

Upvotes: 0

Mathias Brodala
Mathias Brodala

Reputation: 6480

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.

Upvotes: 2

Related Questions