Typolink matters
Links are the magic stuff that keep the web together.
It would be understatement to say that links characterize the web.
They simply make the web. No links, no web.
Links get even more important for a content management system like
TYPO3. They not only point from one page to the other, they even controll
the whole system with all the many parameters. The parameters can
be visible for the user or hidden behind hash values or “pretty URLs”.
If this parameters are not perfectly done, the system starts to
fail. Language settings get lost, frames jump out of a frameset,
caching doesn’t work, the page is getting slow, XHTML becomes invalid,
JavaScript breaks, and and and …
You couldn’t write all this parameters in all this links by hand.
Even if you could, you would need to rewrite them all if an additional
parameter is required. For the same reasons you can’t simply patch up
a link in a PHP script. You have to use the one and only function, that
automatically mananges all the parameters without flaws. This function
is the famous “typolink” function for TYPO3.
This function is called indirectly by TS and some other functions, but
for sure if you program PHP you should be able to use this function directly.
Overview
The typolink is a function of the class tslib_cObj. The first parameter is
the linktext, the second an array with multiply configurations to controll
the links behaviour. This configurations are nicely documented in the
documentation TSref, where they are called properties.
So to use typolink, you perpare the controlling array, get an instance of
tslib_cObj and execute the function.
Example:
1.) Preparing the controlling array
$configurations['useCacheHash'] = 1; // make it a caching link
$configurations['parameter'] = 123; // target page id or external Url, Email, etc.
$configurations['section'] = 'par7'; // id for an anchor
$configurations['target'] = 'blank'; // set a frameset target for internal links
$configurations['extTarget'] = 'blank'; // set a frameset target for external links
$configurations['ATagParams'] = 'class="exampleClass" '; // set a class for the tag
$configurations['ATagParams'] .= 'id="exampleId" '; // set an id tor the tag
[ . . . ] // more attributs of the a tag
$configurations['additionalParams'] = 'myExtension[key1]=value1&myExtension[key2]=value2'; // Extension parameters
Hint: How to build proper additionalParams with Umlauts?
2.) Creating the object …
$cObject = t3lib_div::makeInstance('tslib_cObj');
Hint: Consider to use a singleton, if you create multiple links.
… or just use the current cObj of the plugin
$cObject = $this->cObj;
3.) Rendering the link tag or the plain link URL:
$linkTag = $cObject->typolink('Example text of the link', $configurations);
or
$configurations['returnLast'] = 'url'; // get it as URL
$url = htmlspecialchars($cObject->typolink(NULL, $configurations));
Hint: Typolink generates the link tag with htmlspecialchars. For the URL you have to do it yourself.
Doing it the object orientated way
Maybe you prefer to do it the object orientated way. You find a full working link object in the extension “lib”. You can use it directly or copy and rename it to make it part of your extension.