TYPO3 Fluid: How to get selected page categories in fluid

21 views Asked by At

I spent hours on this problem. I wanted to access the selected categories of my page in TYPO3 Fluid, but there is no default way to do that.

1

There are 1 answers

0
rcheetah On

There is no default way of doing this. You need to query the database and join tables yourself. Thanks for nothing Typo3 – nobody could have expected, that developers would actually want to access the selected categories in their templates. (╯°□°)╯︵ ┻━┻

In the setup Typoscript in the Fluidtemplate, add a DatabaseQueryProcessor:

page = PAGE
page {
    typeNum = 0
    (...)
    10 = FLUIDTEMPLATE
    10 {
        (...)
        dataProcessing {
            (...)
            30 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
            30 {
                table = sys_category
                selectFields = sys_category.*
                pidInList = root,-1
                recursive = 99
                join = sys_category_record_mm ON (sys_category_record_mm.uid_local=sys_category.uid)
                where = sys_category_record_mm.tablenames='pages' AND sys_category_record_mm.uid_foreign = ###pageuid###
                orderBy = sorting
                markers.pageuid.field = uid
                as = pageCategories
            }
        }
    }
}

This snippet creates a variable called pageCategories, which is an array of all selected categories for the current page. You can the access the title e. g. by pageCategories.0.data.title or loop over it in fluid:

<f:for each='{pageCategories}' as='category' iteration='inter'>
    <span class='category-tag'>{category.data.title}</span>
</f:for>

To be honest, I don’t understand the query syntax myself, but this is a working solution. There were just a few google results for this problem, and they either didn't work in my case, of where just never answered, so I wanted to share my solution.

Credits go to t3brightside who maintains the pagelist extension from which this code snippet was ~~inspired~~ (stolen).