[SOLVED] Get all Neos.NodeTypes:Headline on a page in correct order

Hi there,

following situation: I want to create table of contents from all headlines on a page.

tocItems = Neos.Fusion:Loop {
    items = ${q(node).find('[instanceof Neos.NodeTypes:Headline]').get()}

This will give me all headlines but not in the correct order. The ones that have the most parents (e.g. they are part of a row/column) come first, the ones on the first level last.

q(node).children() gives me the ContentCollection ‘main’. Even if I can start at ‘main’, then I have the problem that the current item could be a headline or a ContentCollection with a headline in it. How do i solve this?

Thanks in advance :slight_smile:

The „find“ operation has no ordering as it searches all children of a node recursively and there is no efficient way to sort this hierarchy.

However the „children“ operation allows filtering aswell but only on the direct children and thus has a defined order.

${q(docomentNode). children(‘main ‘).children(’[instanceof Neos.NodeTypes:Headline]’).get()}

This query returns all headlines from the main collection of the current document. Does this help in your case?

Thank you, Martin! With your help I’ve got the solution:

tocItems = Neos.Fusion:Loop {
    items = ${q(documentNode).children('main').children().get()}
    itemRenderer = Neos.Fusion:Case {
        isHeadline {
            condition = ${q(item).is('[instanceof Neos.Neos:Headline]')}
            renderer = Vendor.Package:TocItem {
                item = ${item}

        isContenCollection {
            condition = ${q(item).is('[instanceof Neos.Neos:ContentCollection]')}
            renderer = Neos.Fusion:Loop {
                items = ${q(item).find('[instanceof Neos.Neos:Headline]').get()}
                itemName = 'section'
                itemRenderer = Vendor.Package:TocItem {
                    item = ${section}

Loop over all children. If it’s a Headline, render it. If it’s a ContentCollection, look for all Headlines and render them. Seems easy now… :no_mouth:

Once upon a time I added a sorting operation to the listable package for a nested structure using the nodes relative index: https://github.com/Flowpack/Flowpack.Listable/blob/master/Classes/Fusion/Eel/FlowQueryOperations/SortRecursiveByIndexOperation.php

Maybe that can help you and you can either use the package or copy the helper.