Search code examples
typo3fluidview-helpersfal

TYPO3 get page resources first media image file url of parent page in fluid template


I try to output the first image referenced in the page properties tab "Resources" media relations of the parent page of the current page in a fluid template.

I have got the parent page's id as data.pid.

Specifically I want the image's url.

I have tried <f:image treatIdAsReference="1" src="{v:page.info(pageUid:\"{data.pid}\", field:\"media\")}" /> to no avail (got TYPO3 Exception No file reference (sys_file_reference) was found for given UID: "1"). I believe the media field is only a counter of number of relations added.

I have tried the vhs ViewHelper by {v:page.resources(uid: '{data.pid}') -> v:iterator.first() -> v:variable.set(name: 'image')} but the resulting image variable is only the char '1' and when passed to f:image an exception along the lines of "src must be string or FileObject" is thrown.

I know of the sys_file_reference table but I didn't find a predefined fluid template mean (eg. a ViewHelper) to access the uid of the file or file reference when I know the page id. sys_file_reference table snippet

Btw.: the only way to get the current page's page properties tab "Resources" first media relation image file url I got working is {f:uri.image(src:"/fileadmin/{data_pageimage.0.originalFile.identifier}")}. All other means I tried by ViewHelper such as f:uri.image complained about the src not being a reference (treatIdAsReference true), src being data.media, or the getter of some data_pageimage.0 subobject's id was protected.

If a general way exists to get an image's url by providing a page id and a page properties "file field"'s name (so also for og_image) I'll be glad to read about it.

We are using TYPO3 12 LTS.


Solution

  • Have a look at the FilesProcessor. It can serve you the object you want to use in your template.

    Media from current page

    10 = FLUIDTEMPLATE
    10 {
        # ... (your other stuff)
    
        # Before TYPO3 v12.1 you have to specify the fully-qualified class name of the processor
        # dataProcessing.10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
        # Since TYPO3 v12.1 one can also use the available alias
        dataProcessing.10 = files
        dataProcessing.10 {
            as = mediaimages
            references.fieldName = media
            references.table = pages
        }
    }
    

    Media from parent page

    For selecting media of another page in your rootline, you can use levelmedia

    10 = FLUIDTEMPLATE
    10 {
        # ... (your other stuff)
    
        # Before TYPO3 v12.1 you have to specify the fully-qualified class name of the processor
        # dataProcessing.10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
        # Since TYPO3 v12.1 one can also use the available alias
        dataProcessing.10 = files
        dataProcessing.10 {
            as = mediaimages
            references.data = levelmedia: -1
        }
    }
    

    After this preparation, you can simply access the files via {mediaimages} and by the index:

    <f:image image='{mediaimages.0}' />