How to define special links with custom tags

In this tutorial, we show how one can easily define own tags in TypoScript, known as custom tags (or user defined tags), to allow for special links.

We will define the following types of links:

"nofollow" link to prevent the transfer of PageRank

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":

Defining a nofollow link in TypoScript

TypoScript SETUP
   1: tt_content.text.20.parseFunc.tags {
   2:   link_nf < copy">lib.parseFunc.tags.link
   3:   link_nf.typolink.ATagParams = rel="nofollow"
   4: }
   5: 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 for other language version

TypoScript SETUP
   1: tt_content.text.20.parseFunc.tags {
   2: # language UID 0 = German
   3:   link_de < lib.parseFunc.tags.link
   4:   link_de.typolink.additionalParams = &L=0
   5: # language UID 1 = English
   6:   link_en < lib.parseFunc.tags.link
   7:   link_en.typolink.additionalParams = &L=1
   8: }
   9: lib.parseFunc.tags.link_de < tt_content.text.20.parseFunc.tags.link_de
  10: 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:

Defining a link to a tt_news record

TypoScript SETUP
   1: tt_content.text.20.parseFunc.tags {
   2:   link_ttnews < lib.parseFunc.tags.link
   3:   link_ttnews = PHP_SCRIPT 
   4:   link_ttnews {
   5:     stripNL = 0
   6: # Pass a parameter from Typoscript to a PHP script: UID of the page containing the SINGLE view of tt_news
   7:     id_singleView = 18
   8: # Call the PHP script
   9:     file = fileadmin/scripts/link_ttnews_parser.php
  10:   }
  11: }
  12: lib.parseFunc.tags.link_ttnews < tt_content.text.20.parseFunc.tags.link_ttnews

The PHP script "link_ttnews_parser.php" in the directory "fileadmin/scripts/" 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

/fileadmin/scripts/link_ttnews_parser.php
  1. <?php
  2. /**
  3.  * Parser for 'link_ttnews' tag
  4.  */
  5. $parameters = explode(' ', $this->parameters['allParams']); 
  6. $num_params = count($parameters);
  7.  
  8. if ($num_params > 0 && !empty($parameters[0])) {
  9. 	// retrieve the tt_news record with the given UID
  10. 	$records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
  11. 		'*', 'tt_news',
  12. 		'uid = ' . $parameters[0]
  13. 		. ' AND hidden=0 AND deleted=0'
  14. 		. $GLOBALS['TSFE']->sys_page->enableFields('tt_news'),
  15. 		'', '', 1
  16. 	);
  17.  
  18. 	// if tt_news record was found, assign link parameters
  19. 	if (count($records) > 0) {
  20. 		require_once(PATH_tslib.'class.tslib_content.php');
  21. 		$this->cObj = t3lib_div::makeInstance('tslib_cObj');
  22. 		
  23. 		// generate the link
  24. 		$link['parameter'] =  $this->cObj->getTypoLink_URL(
  25. 			$conf['id_singleView'],
  26. 			array('tx_ttnews[tt_news]' => urlencode($parameters[0]))
  27. 		);
  28.  
  29. 		// if further parameters were passed, use them as 'title' attribute
  30. 		if ($num_params > 1) {
  31. 			// remove the first parameter (UID)
  32. 			array_shift($parameters);
  33. 			// assign title attribute
  34. 			$link['ATagParams'] = 'title="' . implode(' ', $parameters) . '"';
  35. 		}
  36. 		
  37. 		// return the generated link
  38. 		$content .= $this->typolink($this->getCurrentVal(), $link);
  39. 	} else {
  40. 		// if record not found => return the anchor text
  41. 		$content .= $this->getCurrentVal();
  42. 	}
  43. } else {
  44. 	// if no parameters passed => return the anchor text
  45. 	$content .= $this->getCurrentVal();
  46. }
  47. ?> 

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.

Comments for How to define special links with custom tags

Be the first to comment this article!


Add a comment


(will not be displayed, Gravatar is enabled)



(Fields marked with a * are mandatory)

Visitors found this page by searching for these keywords:

typolink nofollow · typo3 link tag language format internal · tt_content.html.parsefunc.tags.link lib.parsefunc.tags.link · typo3 php extension parsefunc.tags.link.typolink.additionalparams · typo3 parsefunc.tags.link.typolink.additionalparams extension