How to Define Custom Tags in TYPO3

How you can define your own tags for specific links in TYPO3. We show tags for nofollow links, links to other languages and links to tt_news records.

nofollow Link

If you are doing search engine optimization — especially for the search engine Google — you are probably already familiar with the concept of PageRank. The PageRank is a value in the range 1 to 10, which Google calculates and assigns to each single web page of a web site. In short, this value represents Google's assessment of the importance of a web page.

Each link (internal or external) that is placed on a page, passes a tiny fraction of its PageRank value to the linked page. This process is often called link juice, because value "flows" from one page to the other. If you don't want the linking page to lose value, you can assign the nofollow attribute to a link.

We will now extend the TYPO3 link by the attribute rel="nofollow" and call the new tag link_nf:

nofollow link in TypoScript TypoScript, SETUP section
tt_content.text.20.parseFunc.tags {
  link_nf < lib.parseFunc.tags.link
  link_nf.typolink.ATagParams = rel="nofollow"
}
lib.parseFunc.tags.link_nf < tt_content.text.20.parseFunc.tags.link_nf

The principle when defining custom tags is always the same: First, the characteristics of the "normal" TYPO3 links are copied (line 2). Then the actual definition of the new link type takes place. The last line makes the new link type work also in content elements such as bullet lists.

Once you have added this code to your TypoScript template, the new link tag will be available in all content elements. The syntax is similar to the standard link — just use the tag <link_nf>...</link_nf> instead of <link>...</link>.

Link to a page in another language

This particular link can be used in a multilingual web site that uses a single page tree ("one tree concept"). It solves the problem that normal links between various languages are not resolved properly.

Suppose a site has two languages, German (sys_language_uid 0) and English (sys_language_uid 1). If a content element of the German website contains a TYPO3 link tag, the resulting link always points to the German version of the linked page. The GET parameter "L" (abbreviation for "language") that is required to identify the language of a page is not passed.

With a custom tag for each language, we can achieve a linkage of different languages. The two tags that we define are called link_de (for links to the German language version) and link_en (for links to the English language version).

Link with L parameter TypoScript, SETUP section
tt_content.text.20.parseFunc.tags {
# language UID 0 = German
  link_de < lib.parseFunc.tags.link
  link_de.typolink.additionalParams = &L=0
# language UID 1 = English
  link_en < lib.parseFunc.tags.link
  link_en.typolink.additionalParams = &L=1
}
lib.parseFunc.tags.link_de < tt_content.text.20.parseFunc.tags.link_de
lib.parseFunc.tags.link_en < tt_content.text.20.parseFunc.tags.link_en

Link to a tt_news record

The next type of link we want to define is more complex: it offers the possibility to directly link to a record of the tt_news extension from within a TYPO3 content element. The unique ID (uid) of the tt_news record will be used as the link parameter. To be able to define this tag, we need to write an external PHP script that is called from within the TypoScript template.

Our tag is called link_ttnews and — in contrast to the standard TYPO3 link tag — takes the first parameter passed as the UID of the target and any other parameters as the link's 'title' attribute. The link is defined as follows:

Link to tt_news TypoScript, SETUP section
tt_content.text.20.parseFunc.tags {
  link_ttnews < lib.parseFunc.tags.link
  link_ttnews = PHP_SCRIPT
  link_ttnews {
    stripNL = 0
# Pass a parameter from Typoscript to a PHP script:
# UID of the page containing the SINGLE view of tt_news
    id_singleView = 18
# Call the PHP script
    file = fileadmin/scripts/link_ttnews_parser.php
  }
}
lib.parseFunc.tags.link_ttnews < tt_content.text.20.parseFunc.tags.link_ttnews

The PHP script /fileadmin/scripts/link_ttnews_parser.php is called from within TypoScript. The parameter id_singleView is passed. It contains the UID (in this case 18) of the page with the tt_news plugin's single view (SINGLE).

The second part of our link definition can be found in the PHP script:

PHP script to parse links to tt_news records
<?php
/**
 * Parser for 'link_ttnews' tag
 * ( /fileadmin/scripts/link_ttnews_parser.php )
 */
$parameters = explode(' ', $this->parameters['allParams']);
$num_params = count($parameters);
 
if ($num_params > 0 && !empty($parameters[0])) {
  // retrieve the tt_news record with the given UID
  $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
    '*', 'tt_news',
    'uid = ' . $parameters[0]
    . ' AND hidden=0 AND deleted=0'
    . $GLOBALS['TSFE']->sys_page->enableFields('tt_news'),
    '', '', 1
  );
 
  // if tt_news record was found, assign link parameters
  if (count($records) > 0) {
    require_once(PATH_tslib.'class.tslib_content.php');
    $this->cObj = t3lib_div::makeInstance('tslib_cObj');
     
    // generate the link
    $link['parameter'] =  $this->cObj->getTypoLink_URL(
      $conf['id_singleView'],
      array('tx_ttnews[tt_news]' => urlencode($parameters[0]))
    );
 
    // if further parameters were passed, use them as 'title' attribute
    if ($num_params > 1) {
      // remove the first parameter (UID)
      array_shift($parameters);
      // assign title attribute
      $link['ATagParams'] = 'title="' . implode(' ', $parameters) . '"';
    }
     
    // return the generated link
    $content .= $this->typolink($this->getCurrentVal(), $link);
  } else {
    // if record not found => return the anchor text
    $content .= $this->getCurrentVal();
  }
} else {
  // if no parameters passed => return the anchor text
  $content .= $this->getCurrentVal();
}
?>

If a valid record with the given UID (first parameter of the link_ttnews tag) can be found in the tt_news table, an a href tag is returned. If additional parameters were passed, a 'title' attribute will be assigned as well. If no valid tt_news record is found, the anchor text (text between <link_ttnews> and </link_ttnews>) will be returned unchanged.

These were some examples for custom tags in TYPO3. This way links can be defined for different requirements. By using TypoScript in combination with PHP there are virtually no limits for the links you can generate.