Search code examples
menutypo3typoscript

TMENU in Typo3 - insert url from level above


I have a TMENU, and in that menu I would like to insert an URL from "Level 1" into a wrap item in "Level 2".

Current TypoScript looks like this:

10 = HMENU
10 {
    1 = TMENU
    1 {
        expAll = 1

        NO = 1
        NO.allWrap = <li>|</li>
        NO.allWrap.insertData = 1
        NO.ATagTitle.field = abstract // description // title
    }

    2 = TMENU
    2 {
        expAll = 1
        stdWrap.wrap = <ul><li><a href="*****URL FROM LEVEL 1 SHOULD BE HERE*****"></a></li>|</ul>

        NO = 1
        NO.allWrap = <li>|</li>
        NO.allWrap.insertData = 1
        NO.ATagTitle.field = abstract // description // title
    }
}

Any hints? Thanks for helping!


Solution

  • stdWrap properties are executed in a defined order (as they appear in documentation), see https://docs.typo3.org/m/typo3/reference-typoscript/master/en-us/Functions/Stdwrap.html#prepend

    Knowing that prepend is executed before wrap3 allows to solve this challenge.

    page = PAGE
    page.10 = HMENU
    page.10 {
        1 = TMENU
        1 {
            expAll = 1
            NO = 1
            NO.allWrap = <li>|</li>
            NO.allWrap.insertData = 1
            NO.ATagTitle.field = abstract // description // title
        }
    
        2 = TMENU
        2 {
            expAll = 1
            stdWrap.prepend = TEXT
            stdWrap.prepend {
              # remove `value` if page title shall be used
              value = URL from Level 1
              # using <current-page>.pid value pointing to previous level
              typolink.parameter.field = pid
              wrap = <li>|</li>
            }
            # `wrap3` is executed after `prepend`
            stdWrap.wrap3 = <ul>|</ul>
    
            NO = 1
            NO.allWrap = <li>|</li>
            NO.allWrap.insertData = 1
            NO.ATagTitle.field = abstract // description // title
        }
    }
    

    On a page-tree like this

    +- A
    |  +- AA
    |  +- AB
    |
    +- B
       +- BA
       +- BB
    

    The rendered HTML result looks like this

    <li><a href="/ts/a" title="A">A</a></li>
    <ul>
      <li><a href="/ts/a">URL from Level 1</a></li>
      <li><a href="/ts/a/aa" title="AA">AA</a></li>
      <li><a href="/ts/a/ab" title="AB">AB</a></li>
    </ul>
    
    <li><a href="/ts/b" title="B">B</a></li>
    <ul>
      <li><a href="/ts/b">URL from Level 1</a></li>
      <li><a href="/ts/b/ba" title="BA">BA</a></li>
      <li><a href="/ts/b/bb" title="BB">BB</a></li>
    </ul>
    

    Update on custom rendering replacing static value property

    Instead of using property stdWrap.prepend.value (like shown in example above), other cObject instructions could be used as well - in this case for instance RECORDS (see https://docs.typo3.org/m/typo3/reference-typoscript/master/en-us/ContentObjects/Records/Index.html)

    The following example renders fields nav_title (and if that's not defined title as fallback) of the parent page.

    page.10 {
        # ...
        2 {
            # ...
            stdWrap.prepend {
                # using `cObject` instead of `value`
                cObject = RECORDS
                cObject {
                    tables = pages
                    source.field = pid
                    # rendering definition for retrieved record of table pages
                    conf.pages = TEXT
                    # `//` is used as fallback delimiter here, NOT as comment
                    conf.pages.field = nav_title // title
                }
                # ...