Search code examples
typo3-9.x

TYPO3 9: Recursive count subpages of root page


is TYPO3 able to collect recursive and show the amount of subpages (no storages) from a specific root page? Tried this typoscript snippet but it is not working somehow.

20 = CONTENT
20 {
   table = pages
   select {
      selectFields = count(*)
      pidInList = <ROOT-PID>
      andWhere = (hidden=0 AND deleted=0)
   }
   renderObj = COA
   renderObj {
      10 = TEXT
      10 {
         field = count(*)
         wrap = Counted pages: |
      }
   }
}

Maybe there is some function in TYPO3 9?

Thanks in advance!


Solution

  • As the data is stored as a tree you need a recursion to get all pages from all sublevels.

    As you can't know the depth (or expect a high number) and typoscript as a configuration language has no recursion build in a pure typoscript solution would be complicated.

    Here a userfunc written in PHP and with recursion could be a quick solution.


    In general:
    Why do you need that number?
    Maybe there is a simpler solution


    here a pure typoscript solution you might start with:

    for each menu-entry you get the sum of all pages up to three levels deeper and the count of pages on the next level.

    temp.menudef = TMENU
    temp.menudef {
        #target = _top
        noBlur = 1
        #expAll = 1
        wrap = <ol>|</ol>
        NO = 1
        NO {
            stdWrap.cObject = COA
            stdWrap.cObject {
                1 = TEXT
                1.field = title
                1.required = 1
    
                10 = LOAD_REGISTER
                10.level1uids.cObject = COA
                10.level1uids.cObject {
                    10 = CONTENT
                    10.table = pages
                    10.select.pidInList.data = field:uid
                    10.renderObj = TEXT
                    10.renderObj.field = uid
                    10.renderObj.wrap = |,
                    20 = TEXT
                    #20.field = uid
                    20.data = field:uid
                }
                10.level2uids.cObject < .10.level1uids.cObject
                10.level2uids.cObject.10.select.pidInList.data = register:level1uids
                10.level2uids.cObject.20.data = register:level1uids
    
                10.level3uids.cObject < .10.level1uids.cObject
                10.level3uids.cObject.10.select.pidInList.data = register:level2uids
                10.level3uids.cObject.20.data = register:level2uids
    
                20 = TEXT
                20.wrap = <!-- --> (|
                20.required = 1
                20.override.numRows.table = pages
                20.override.numRows.select.pidInList.data = register:level3uids
                20.override.stdWrap.ifEmpty =
                20.override.stdWrap.ifEmpty.wrap = |
    
                30 = TEXT
                30.wrap = -|)
                30.required = 1
                30.override.numRows.table = pages
                30.override.numRows.select.pidInList.field = uid
                30.override.stdWrap.ifEmpty =
                30.override.stdWrap.ifEmpty.wrap = |
            }
    
        }
        NO.wrapItemAndSub = <li>|</li>
        NO.accessKey = 1
    
        ACT < .NO
        #ACT = 1
        ACT.wrapItemAndSub = <li class="activ">|</li>
    
        SPC < .NO
        #SPC = 1
        SPC.wrapItemAndSub = <li><span class="spacer">|</span></li>
    
        CUR < .ACT
        #CUR = 1
        CUR.linkWrap = <span class="act">|</span>
        CUR.wrapItemAndSub = <li class="current">|</li>
    }
    
    
    temp.submenu = HMENU
    temp.submenu {
        entryLevel = 0
        wrap = <div  class="smenu">|</div>
    
        1 < temp.menudef
        2 < .1
        3 < .2
        4 < .3
        5 < .4
    }