Posted on Leave a comment

String comparison in Typoscript through user functions in TYPO3

TYPO3 Logo

Generally, string comparison using the on-board tools provided by TypoScript can be quite cumbersome in TYPO3. Although, for instance for globalStrings there exists the possibility to use regular expressions and the * character as wildcard, oftentimes this is not flexible enough to handle more complex conditions, such as combined conditions.

User functions in TypoScript

Luckily, it is quite easy to add more complex string comparison functionality to TypoScript through user functions. Simply add your user defined function in localconf.php, e.g.

/**
 * Return TRUE on success, FALSE otherwise.
 * @return boolean
 */
function user_match($var1, $var2, $var3) {
  if($var1 == $var2) {
    return TRUE;
  }

  if($var1 == $var3) {
    return preg_match('#...#', $var2);
  }

  ...
     
  return false;
}

You can then call this function in TypoScript as conditional statement:

[userFunc = user_match(value1, value2, value3)]
  # do something here
[end]

Have a look at the Typoscript Conditions reference for more information.

Posted on 1 Comment

Configure robots.txt for Realurl in TYPO3

TYPO3 Logo

In order to configure robots.txt for the Realurl extension in TYPO3 you need to set two things:

  1. Add filename for page type 201 in realurl_config.php
  2. Add some TypoScript to process robots generation

Add filename for page type in realurl_config.php

$TYPO3_CONF_VARS['EXTCONF']['realurl']['_DEFAULT'] = array(
  ...
  // configure filenames for different pagetypes
    'fileName' => array(
        'defaultToHTMLsuffixOnPrev' => 0,
        'index' => array(
            'print.html' => array(
                'keyValues' => array(
                    'type' => 98,
                ),
            ),
            ...
            // add robots.txt page type
            'robots.txt' => array(
                'keyValues' => array(
                    'type' => 201
                )
            )
        ),
  ...

Add TypoScript to process robots.txt generation

robots = PAGE
robots {
  typeNum = 201
  config {
    disableAllHeaderCode = 1
    additionalHeaders = Content-type:text/plain
  }
  10 = TEXT
  10 {
    wrap (
User-Agent: *
Allow: /                #allow everything by default
Disallow: /fileadmin/templates     #then add restrictions
Disallow: /typo3/        
Disallow: /t3lib/        
Disallow: /typo3conf/    
Disallow: /typo3temp/    
Disallow: /*?id=*        

User-agent: googlebot  # Google specific settings
Disallow: /*?tx_indexedsearch

Sitemap: /?eID=dd_googlesitemap # finally add some sitemap
    )
} 

Be sure to flush the cache and you are all set!

Posted on 2 Comments

Generate hreflang alternate tags in TYPO3

TYPO3 Logo

Here is a quick TypoScript snippet that generates hreflang alternate tags in TYPO3. Take note of the following parameters:

  • special.value: set IDs of languages available, e.g. 0
  • stdWrap.cObject.value: specify the locale corresponding to special.value, e.g. de-AT
  • stdWrap.typolink.additionalParams: specify the special.value values for each locale set

TypoScript code to generate hreflang alternate tags

Here’s the snippet for a couple of exemplary locales:

page.headerData.100 = HMENU
page.headerData.100 {
  special = language
  special.value = 0,1,2,3,4
  1 = TMENU
  1 {
    NO = 1
    NO {
      stdWrap.cObject = TEXT
      stdWrap.cObject {
        value = de-AT || en || it-IT || fr-FR || sl-SI
      }
      linkWrap = 
        stdWrap.typolink.parameter.data = page:uid
        stdWrap.typolink.additionalParams = &L=0 || &L=1 || &L=2 || &L=3 || &L=4
        stdWrap.typolink.returnLast = url
      }
    }
  }
}

Snippet Output

The snippet above produces the following output:

<link rel="alternate" hreflang="de-AT" href="http://www.somedomain.at/at-page" />
<link rel="alternate" hreflang="en" href="http://www.somedomain.com/en-page" /> 
<link rel="alternate" hreflang="it-IT" href="http://www.somedomain.it/it-page" /> 
<link rel="alternate" hreflang="fr-FR" href="http://www.somedomain.fr/fr-page" /> 
<link rel="alternate" hreflang="sl-SI" href="http://www.somedomain.si/si-page" />

That’s it!