Issue Feature Closed

#97689Resolve file relation on JsonView configuration

Aristeidis Karavas 05/25/22, 11:46 updated 10/09/26, 17:45

Description

I recently created a headless project. For the project i used the news extension as well. With the help of the PSR-14, i got the news entries on my listener class. I wanted to convert the object to array because headless does not support extbase object, only json. I managed to convert everything to array with the following configuration:

$this->view->setConfiguration([
    'news' => [
        '_descendAll' => [
            '_recursive' => ['categories', 'falMedia', 'contentElements', 'tags']
         ],
     ]
]);

Everything worked fine until i saw the falMedia object. It resolved the sys_file_reference, meaning it found the file, but it didnt retrieve the object, instead it returned the UID of the file. This is what i got:

falMedia": {
  "0000000018019f7e000000007283656b": {
  "alternative": "",
  "description": "",
  "fileUid": 8,
  "link": "",
  "pid": 37,
  "showinpreview": 0,
  "title": "",
  "uid": 93
  }
},

The UID is the sys_file_reference id and the fileUid is the file id in the sys_file table.

AFAIK there is not a function implemented that does just that (i looked inside the JsonView class) so it would be great if that can be possible in the future

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.

Files 1

History 1

  1. Georg Ringer commented and changed status New Closed <span data-processor="relativetime" data-value="2026-10-09T17:45:45+00:00" > 10/09/26, 17:45 </span>

    Thanks for the report! I had a look at this on main.

    The JsonView only renders properties which are declared in the class and have a matching getter (ObjectAccess::getGettablePropertyNames()). For files and file references most of the interesting data (public URL, merged metadata like title and alternative) is only available through getters without a matching property, which is why you only get the raw reference data.

    Instead of adding special handling for FAL to the core, this can already be solved today in two ways:

    1. Extend the JsonView

    JsonView::transformObject() is protected, so a custom view can render files the way the project needs them:

    <?php
    
    declare(strict_types=1);
    
    namespace Vendor\MyExtension\View;
    
    use TYPO3\CMS\Core\Resource\FileInterface;
    use TYPO3\CMS\Extbase\Domain\Model\FileReference;
    use TYPO3\CMS\Extbase\Mvc\View\JsonView;
    
    final class FileAwareJsonView extends JsonView
    {
        protected function transformObject(object $object, array $configuration): array|string
        {
            if ($object instanceof FileReference) {
                $object = $object->getOriginalResource();
            }
            if ($object instanceof FileInterface) {
                return [
                    'uid' => $object->getProperty('uid'),
                    'publicUrl' => $object->getPublicUrl(),
                    'title' => $object->getProperty('title'),
                    'alternative' => $object->getProperty('alternative'),
                    'description' => $object->getProperty('description'),
                ];
            }
            return parent::transformObject($object, $configuration);
        }
    }

    Use it in the controller:

    protected ?string $defaultViewObjectName = FileAwareJsonView::class;

    The existing configuration with _descend / _recursive on falMedia then renders the file data.

    2. Convert the files before assigning them

    If the data is prepared in a PSR-14 listener anyway, the file references can be converted to plain arrays right there. Arrays are rendered by the JsonView as they are:

    $media = [];
    foreach ($news->getFalMedia() as $fileReference) {
        $file = $fileReference->getOriginalResource();
        $media[] = [
            'uid' => $file->getUid(),
            'publicUrl' => $file->getPublicUrl(),
            'title' => $file->getTitle(),
            'alternative' => $file->getAlternative(),
            'description' => $file->getDescription(),
        ];
    }
    $this->view->assign('media', $media);

    As both options work without changes to the core and every project needs a different set of file data (e.g. processed images in different sizes), I would like to close this issue. Please let me know if this does not cover your use case.