Search code examples
typo3typoscript

Get all categories from a page into a data attribute in typoscript


In a TYPO3 project I want to collect all subpages with content and categories and render it on rootpage.

lib.dynamicContent = COA
lib.dynamicContent {

    10 = CONTENT
    10 {
        table = pages
        select.orderBy = sorting
        select.where = doktype != 199
        as = pageId
        renderObj = COA
        renderObj {
            10 = CONTENT
            10 {
                table = tt_content
                select {
                    pidInList.field = uid
                    orderBy = sorting
                    where = colPos = 0
                }
            }
            20 = CONTENT
            20 {
                table = sys_category
                select {
                    pidInList.field = root,-1
                    selectFields = sys_category.uid, sys_category.title
                    join = sys_category_record_mm ON sys_category.uid = sys_category_record_mm.uid_local
                    where.data = field:uid
                    where.intval = 1
                    where.wrap = sys_category_record_mm.uid_foreign=|
                }

                renderObj = COA
                renderObj {
                    10 = TEXT
                    10.field = title
                    10.wrap =  |
                    10.wrap.override = <span class="category">|</span>
                }

                stdWrap {
                    wrap = <div class="categories">|</div>
                }
            }

            stdWrap {
                if.isLessThan.field = doktype
                if.value = 3
                wrap = <section id="p{field:uid}" data-display="{field:sys_category.title}">|</section>
                wrap.insertData = 1
            }
        }
    }
}

This is my current typoscript. The first content element in renderObj helps me to get the content from all subpages and set the section id with the page uid. That works. The 20.CONTENT gives me the categories of the subpages. The following renderObj is just fo r test and this works and can be removed. But I want to render the categories as comma separated list in the data-display attribute and I do not see it. I splitted the wrap inside the data-display but that did not work and as there is a list I can not use a "variable" as in id. Can someone helps me to find a solution? Thank you in advise


Solution

  • I have a solution now. I restructured that and the following code is fine:

    lib.dynamicContent = COA
    lib.dynamicContent {
    
        10 = CONTENT
        10 {
            table = pages
            select {
                pidInList = this
                recursive = 1
                where = doktype = 1
                selectFields = pages.*, group_concat(sys_category.title SEPARATOR ',') AS categories
                join = sys_category_record_mm ON pages.uid = sys_category_record_mm.uid_foreign JOIN sys_category ON sys_category_record_mm.uid_local = sys_category.uid
                groupBy = pages.uid
            }
    
            renderObj = COA
            renderObj {
                10 = COA
                10 {
                    stdWrap.dataWrap = <section id="p{field:uid}" data-display="{field:categories}">|</section>
                    10 = CONTENT
                    10 {
                        table = tt_content
                        select {
                            pidInList.field = uid
                            orderBy = sorting
                            where = colPos = 0
                        }
                        
                        stdWrap.wrap = <div class="page-content">|</div>
                    }
                }
            }
        }
    }