TYPO3 Extensions Configuration

April 1, 2008 on 4:58 pm | In TYPO3, English, Planet |

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:

  • 1. Generic Variables (ext_conf_template.txt)
  • 2. TYPO3_CONF_VARS
  • 3. 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>0</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.

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds. Valid XHTML and CSS. ^Top^