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.

  1. // Create a menu where all pages of type "External
  2. // URL" link directly to the actual external url.
  3. lib.extUrl = HMENU
  4. lib.extUrl {
  5.   1 = TMENU
  6.   1 {
  7.     // Remove 'onblur' attribute from links:
  8.     noBlur = 1
  9.     // Wrap the resulting menu in an unordered list:
  10.     wrap = <ul>|</ul>
  11.     NO {
  12.       // Do not link whatever output we generate here:
  13.       doNotLinkIt = 1
  14.       // Wrap each menu item and its children in a
  15.       // list item:
  16.       wrapItemAndSub = <li>|</li>
  17.       // Use a CASE cObject to build the menu links:
  18.       stdWrap.cObject = CASE
  19.       stdWrap.cObject {
  20.         // Base the rendering on the value of the
  21.         // 'doktype' field in the page record; '3'
  22.         // is the External URL page type:
  23.         key.field = doktype
  24.         // The default rendering:
  25.         default = HTML
  26.         default {
  27.           value {
  28.             // Set the link text to the value of
  29.             // the title field in the page record:
  30.             field = title
  31.             typolink {
  32.               // Set the parameter of the link
  33.               // to the uid field in the page
  34.               // record:
  35.               parameter.data = field:uid
  36.             }
  37.           }
  38.         }
  39.         // The special rendering for the External URL
  40.         // page type:
  41.         // Copy the default object into .3:
  42.         3 < .default
  43.         3 {
  44.           value {
  45.             typolink {
  46.               parameter {
  47.                 // Unset the value of .data:
  48.                 data >
  49.                 // Set the parameter of the link
  50.                 // to the value of the url field
  51.                 // and prepend it with "http://":
  52.                 dataWrap = http://{field:url}
  53.               }
  54.             }
  55.           }
  56.         }
  57.       }
  58.     }
  59.   }
  60. }
Download this file: TMENU with External URLs

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.