Simplify the TYPO3 Translation Workflow: TransFusion's Q2/2024 Roadmap
In the dynamic world of web development, staying ahead means continually innovating and refining existing tools. With TransFusion, our mission is to…
$content = '<a href="index.php?id='.$GLOBALS['TSFE']->id.
'&doTheMagic=1">Do it!</a>';
That piece of code will work like you expect it: It will jump to the current page and pass the parameter doTheMagic to your script. Great. Almost. Just imagine, you are running a multi-lingual website. The information about the currently selected language will usually be passed from page to page by adding the parameter &L=x to the URL where x is an integer value corresponding with the uid of the sys_language record. Now, if the visitor of your website clicks the magic button in your frontend plugin, the L flag won't be transmitted and the next page will be displayed with the default language, ignoring whatever language has been selected before. Okay, let's take that into account:$content = '<a href="index.php?id='.$GLOBALS['TSFE']->id.
'&L=' . intval (t3lib_div::_GP('L')).
'&doTheMagic=1">Do it!</a>';
Now the L parameter will be added to your URL and the next page will be displayed in the correct language. Great. Almost, because what happens if there are parameters you don't know of?Function name | Purpose |
pi_getPageLink | Returns a URL to a certain page within TYPO3 by passing the page id. This is useful for creating your own special links or action parameters in an HTML form. |
pi_linkToPage | Returns a whole HTML link (<a href="...">...</a>) pointing to a TYPO3 page by passing a page id and a label. This is the most basic way of creating internal links. |
pi_linkTP | Returns a whole HTML link pointing to the current page. Usually you will need this for passing some additional parameters to your plugin. |
pi_linkTP_keepPIvars | Returns an HTML link to the current page while keeping currently set values in piVars. We'll discuss piVars later in this article. |
pi_linkTP_keepPIvars_url | Returns a URL to the current page while keeping currently set values in piVars. Use this instead of pi_linkTP_keepPIvars if you only need the URL, not the whole link. |
pi_list_linkSingle | Returns a link to a single display of a certain record. This function is rather special and has to do with the browsing functions in pi_base. We won't discuss that in this tutorial. |
Let's start with the easy ones. I'll just give you an example for each of the first three functions. Note that these are very simplistic, non real-world examples and not all parameters of the pi_base methods are discussed here. Again: Have a look into the sources, all the parameters are explained there.
pi_getPageLink:
1: // Link to the parent page and if there is none, link to
// the current page instead:
3:if (intval($GLOBALS['TSFE']->page['pid'])) {
4: $label = 'parent page';
5: $id = $GLOBALS['TSFE']->page['pid'];
} else {
7: $label = 'this page';
8: $id = $GLOBALS['TSFE']->id;
}
11: // Create the link and a sample form:
12:$link = $this->pi_getPageLink($id)
13:$out = '
14: <form action="'.$link.'">
15: <input type="submit" value="' . $label . '" />
</form>
17: ';
pi_linkToPage:
1: // Prepare the trick:
2:$label = 'Do the magic on a different page';
3:$id = 43; // The magic page
4:$params = array (
5: 'doTheMagic' => 1
6:);
7: // Create the link:
8:$out = $this->pi_linkToPage($label, $id, '', $params);
pi_linkTP:
1: // Get prepared:
2:$counter = intval (t3lib_div::_GP('tx_myext_counter'));
3:$label = 'Increase by 1';
4:$params = array (
5: 'tx_myext_counter' => $counter + 1,
6: 'tx_myext_mood' => t3lib_div::_GP('tx_myext_mood'),
);
8: // Create the link:
9:$out = 'The current value is ' . $counter;
$out .= $this->pi_linkTP ($label, $params)
Side note: In some examples I used the function t3lib_div::_GP() which returns GET and POST variables. If you don't know about the class t3lib_div yet, have a look at it's file in the t3lib/ directory. It contains a great collection of useful functions which make a TYPO3 coder's day easier.
While these three functions are quite self-explanatory, the remaining two need further examination (I'll skip pi_list_linkSingle which belongs to the list view / single view functions pibase). They do something with the so called piVars, but what are piVars then?
As you have seen in the last example (pi_linkTP), a GET (or POST) variable called tx_myext_mood has been added to the URL parameters although we didn't touch it. This variable might be passed to our script from somewhere else and we now have to take care that we don't lose it while we're linking to our own page.
But why do we have to take care of tx_myext_mood while the type parameter is handled automatically? Well, that is because some parameters might be registered with the linkVars TypoScript directive (see <link http: typo3.org documentation document-library core-documentation doc_core_tsref current>TSref for details) and tx_myext_mood obviously isn't.
Wouldn't it be great if we could register a certain set of parameters (ie. variables) which we use in our extension and they are passed to our script each time we link to it? That's what the piVars are for.
piVars
Actually piVars is only an array in the pi_base class which we can use in our frontend plugin (because it is inherited from pi_base). Let's fill it with some important information:
$this->piVars['mood'] = 'great';
$this->piVars['boardingSkillLevel'] = 'neverSeenSnow';
Now, if you use the link functions with ...keepPIvars in their name, you won't have to take care of these values – they are just passed with each link you create:
$out = pi_linkTP_keepPIvars ('link to myself');
will generate a link like this:
<a href="index.php?id=3&tx_myext[mood]=great&tx_myext[boardingSkillLevel]=neverSeenSnow">link to myself</a>
... and if you changed your mood in the meantime, just modify the parameter:
$out = pi_linkTP_keepPIvars ('link to myself',
array ('mood'=>'overwhelmed', 'boardingSkillLevel' => null));
<a href="index.php?id=3&tx_myext[mood]=overwhelmed>link to myself</a>
Play around with this concept and especially have a look at the sample code the kickstarter wizard creates for you.
Example URI | What's that? |
This TYPO3's default URI style | |
You get this type of URIs if you enable the simulateStaticDocuments option. Find out more about simulateStatic in the TS reference: <link http: typo3.org documentation document-library>typo3.org/documentation/document-library/doc_core_tsref/quot_CONFIG_quot/ | |
Although they never produced a sound, they are called speaking URIs and are available in TYPO3 from version 3.6.0 and up. You'll need the extension speakinguris or realurl: <link http: typo3.org documentation document-library speakinguris>typo3.org/documentation/document-library/speakinguris/ <link http: typo3.org documentation document-library speakinguris realurl>typo3.org/documentation/document-library/speakinguris/<link http: typo3.org documentation document-library realurl>typo3.org/documentation/document-library/realurl/ |
In the dynamic world of web development, staying ahead means continually innovating and refining existing tools. With TransFusion, our mission is to…
The TYPO3 Association member poll for the Quarter 2/2024 budget ideas has been finished and the four winning ideas will be funded by the TYPO3…
In mid-March, we applied the final touches to the ACL Improvements initiative, which was chosen as a Community Budget Idea for Q1 2024. Here is a…
Last year was marked by the revitalization of the Localization Team. We re-established regular meetings and started working on a number of issues,…
EU laws can foster or hinder open-source innovation in the name of personal digital sovereignty. Legal expert Neil Peretz gave a tour of current…
As part of a wider initiative to promote TYPO3 in the UK, CMS ambassador Tom Warwick and colleagues participated in two conferences in central London…