TMENU with external URLs
By default, TYPO3's menu objects render links to pages of the type "External URL" as links to pages within the site's domain. When clicked, these links redirect to the desired external location. Sometimes, it's desirable to override this behaviour and have the pages link directly to the external url instead. Fortunately, TYPO3's menu objects are quite flexible enough to make this possible.
In such a case, we can disable the default link rendering and use TMENU's .stdWrap object to render an entirely new link using the value of the URL field in the External URL page record.
- // Create a menu where all pages of type "External
- // URL" link directly to the actual external url.
- lib.extUrl = HMENU
- lib.extUrl {
- 1 = TMENU
- 1 {
- // Remove 'onblur' attribute from links:
- noBlur = 1
- // Wrap the resulting menu in an unordered list:
- wrap = <ul>|</ul>
- NO {
- // Do not link whatever output we generate here:
- doNotLinkIt = 1
- // Wrap each menu item and its children in a
- // list item:
- wrapItemAndSub = <li>|</li>
- // Use a CASE cObject to build the menu links:
- stdWrap.cObject = CASE
- stdWrap.cObject {
- // Base the rendering on the value of the
- // 'doktype' field in the page record; '3'
- // is the External URL page type:
- key.field = doktype
- // The default rendering:
- default = HTML
- default {
- value {
- // Set the link text to the value of
- // the title field in the page record:
- field = title
- typolink {
- // Set the parameter of the link
- // to the uid field in the page
- // record:
- parameter.data = field:uid
- }
- }
- }
- // The special rendering for the External URL
- // page type:
- // Copy the default object into .3:
- 3 < .default
- 3 {
- value {
- typolink {
- parameter {
- // Unset the value of .data:
- data >
- // Set the parameter of the link
- // to the value of the url field
- // and prepend it with "http://":
- dataWrap = http://{field:url}
- }
- }
- }
- }
- }
- }
- }
- }
The above code relies on the CASE cObject—much like a switch construct in PHP, the CASE cObject allows multiple alternate Typoscript outputs on the basis of some value. CASE is often a useful alternative to conditions or .if in TS.
