TYPO3 Extensions Configuration
This post try to explain how to develop TYPO3 extensions as flexible as possible, TYPO3 provides us with several mechanism to do it. Furthermore I’m going to show a very useful function to read every possible configuration variable on a TYPO3 extension. There is different ways to configure a TYPO3 extension (from more general to more concrete), and I’m going to explain how to use them:
-
- Generic Variables (ext_conf_template.txt)
-
- TYPO3_CONF_VARS
-
- Plugin Configuration
- a) TypoScript
- b) FlexForms
1. Generic Variables (ext_conf_template.txt)
For extension configuration variables we can use the file ext_conf_template.txt file, variables stored here are common to whole extension. Plugins, modules and classes can read these configuration variables.
For example, if in one extension you have a database table we usually use an extension configuration variable to save PID for the records of this table. The file ext_conf_template.txt would be (see more):
# cat=basic/int; type=int; label=Parent page uid (PID) for records of myTable myTablePID = 0
And you can read this information in your source code with:
$confArray = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][your-extension-key]); echo $confArray['myTablePID'];
2. TYPO3_CONF_VARS
We can use TYPO3_CONF_VARS array to store extension configuration variables too. We set these variables directly at file ext_localconf.php:
$TYPO3_CONF_VARS['EXTCONF'][$_EXTKEY]['myVariable'] = 'myValue';
And we’d can read these variables where we want. Moreover these variables could be modified from ext_localconf.php files of other extensions loaded later than our extension.
3. Plugins Configuration
TYPO3 has two more ways to configure plugins (frontend applications developed in a extension):
a) TypoScript
We can use directly TypoScript inside a template Setup on module Template. With a simple TypoScript code:
plugin.tx_myextension_pi1.myVar = value
We can set a value for the variable myVar that we are going to receive in our plugin as the second parameter of main method.
b) FlexForms
In addition we use FlexForms in order to add specific configuration to frontend plugins. And we usually defines a sheet to override extension configuration. The basic FlexForm for our plugins are something like this (see more):
<T3DataStructure> <meta> <langDisable>1</langDisable> </meta> <sheets> <sOverride> <ROOT> <TCEforms> <sheetTitle>Override Extension Configuration</sheetTitle> </TCEforms> <type>array</type> <el> <myTablePID> <TCEforms> <label>PID for myTable records</label> <config> <type>group</type> <internal_type>db</internal_type> <allowed>pages</allowed> <size>1</size> <maxitems>1</maxitems> <minitems></minitems> <show_thumbs>1</show_thumbs> </config> </TCEforms> </myTablePID> </el> </ROOT> </sOverride> </sheets> </T3DataStructure>
And to read this variables we use:
$this->pi_initPIflexForm(); $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'myTablePID');
Init function
Finally we’ve developed a function init to use in every plugin that we develop, this function reads all configuration variables and stores its in an array $this->conf. Next the source code of this function:
/** * Init Function: here all the needed configuration values are stored in class variables * * @param array $conf: configuration array from TS * @return void */ function init($conf) { $this->conf = $conf; // Store configuration $this->pi_setPiVarDefaults(); // Set default piVars from TS $this->pi_initPIflexForm(); // Init FlexForm configuration for plugin // Read extension configuration $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$this->extKey]); if (is_array($extConf)) { $conf = t3lib_div::array_merge($extConf, $conf); } // Read TYPO3_CONF_VARS configuration $varsConf = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey]; if (is_array($varsConf)) { $conf = t3lib_div::array_merge($varsConf, $conf); } // Read FlexForm configuration if ($this->cObj->data['pi_flexform']['data']) { foreach ($this->cObj->data['pi_flexform']['data'] as $sheetName => $sheet) { foreach ($sheet as $langName => $lang) { foreach(array_keys($lang) as $key) { $flexFormConf[$key] = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], $key, $sheetName, $langName); if (!$flexFormConf[$key]) { unset($flexFormConf[$key]); } } } } } if (is_array($flexFormConf)) { $conf = t3lib_div::array_merge($conf, $flexFormConf); } $this->conf = $conf; }
I think that this function can be very useful when you develop a TYPO3 extension.
Comments
Thanks for this post! Theres ist only one question left: Using Typoscript an flexforms at the same time for one value, what happens? Does the value form flexforms override the Typoscript value?
On 09/02/05 07:38, Manuel Rego Casasnovas wrote:
Yes, the value from FlexForms overrides the TypoScript value.
But you can modify the preference easily changing the parameter order in functions “array_merge”.Thanks for your comment ;-)
On 09/06/02 15:96, Eric wrote:
How is it possible to extend the flexform of an existing extension to add my own fields?
Thanks a million times!
On 09/08/21 19:28, Nick wrote:
Awesome Rego! This is the best, most concise explanation I’ve seen anywhere for all the ways a Typo3 extension can get configurations. Was struggling to get any kind of configs in to the first plugin I’ve ever written, and this was just what I needed. Thanks very much!
On 09/08/21 21:24, Manuel Rego Casasnovas wrote:
Hi Nick, thanks for your comments. It’s great to see that this is useful for someone else ;-)
On 10/02/21 10:46, Shery wrote:
Hi there, I want to install an extension in name PMK Shadowbox and it’s require a set up http://typo3.org/documentation/document-library/extension-manuals/and_shadowbox/1.0.2/view/1/2/
I am a bit confused. should I make a flex form first or it should be one file that I should configure.
I am absolutely lost :( HELPPPPPI appreciate your help,
On 10/03/08 08:09, Manuel Rego Casasnovas wrote:
Sorry, I’ve never used this extension. But it seems that when you install the extension and add a plugin you will have a flexform to configure it.
On 11/03/22 10:08, serengeti wrote:
Hey, great explanation!
Do you know how to read the configuration that is in the pageTSconfig ?
On 11/03/25 10:52, Manuel Rego Casasnovas wrote:
@serengeti sorry but right now I don’t know how to do that, I should need to review it.
- Previous: O Galego en TYPO3 II
- Next: Galician in TYPO3 III