Utiliser les flexforms dans un plugin
De Typo3 CMS / Documentation Typo3 / Support Typo3.
Sommaire |
Déclaration du flexform
Pour utiliser un flexform dans un plugin, il faut commencer par créer un fichier flexform_ds.xml que nous placerons à la racine du dossier contenant notre plugin. Il faut ensuite demander à Typo3 de charger ce fichier xml au sein de notre plugin. Pour cela nous devons modifier le fichier ext_tables.php (en gras apparaissent les lignes modifiées)
<?php if (!defined ('TYPO3_MODE')) die ('Access denied.'); t3lib_div::loadTCA('tt_content'); $TCA['tt_content']['types']['list']['subtypes_excludelist'][$_EXTKEY.'_pi1']='layout,select_key'; //Ligne ajoutée $TCA["tt_content"]["types"]["list"]["subtypes_addlist"][$_EXTKEY."_pi1"]="pi_flexform"; t3lib_extMgm::addPlugin(Array('LLL:EXT:/locallang_db.php:tt_content.list_type_pi1', $_EXTKEY.'_pi1'),'list_type'); //Ligne ajoutée. t3lib_extMgm::addPiFlexFormValue($_EXTKEY.'_pi1', 'FILE:EXT:'.$_EXTKEY.'monflex/flexform_ds.xml'); ?>
Création du flexform
Une fois le fichier ext_tables modifié, on construit dans le fichier flexform_ds.xml la structure à reproduire en backOffice. Ci-dessous un exemple de fichier flexform. On retrouve dans ce dernier des exemples de champ "database relation", "selectbox", "rte", "checkbox", "string input".
<T3DataStructure> <meta> <langDisable>1</langDisable> </meta> <sheets> <sDEF> <ROOT> <TCEforms> <sheetTitle>LLL:EXT:sampleflex/locallang_db.php:tt_content.pi_flexform.sheet_general</sheetTitle> </TCEforms> <type>array</type> <el> <pages> <TCEforms> <exclude>1</exclude> <label>LLL:EXT:lang/locallang_general.php:LGL.startingpoint</label> <config> <type>group</type> <internal_type>db</internal_type> <allowed>pages</allowed> <size>3</size> <maxitems>22</maxitems> <minitems>0</minitems> <show_thumbs>1</show_thumbs> </config> </TCEforms> </pages> <recursive> <TCEforms> <label>LLL:EXT:lang/locallang_general.php:LGL.recursive</label> <config> <type>select</type> <items type="array"> <numIndex index="0" type="array"> <numIndex index="0"></numIndex> <numIndex index="1"></numIndex> </numIndex> <numIndex index="1" type="array"> <numIndex index="0">LLL:EXT:cms/locallang_ttc.php:recursive.I.1</numIndex> <numIndex index="1">1</numIndex> </numIndex> <numIndex index="2" type="array"> <numIndex index="0">LLL:EXT:cms/locallang_ttc.php:recursive.I.2</numIndex> <numIndex index="1">2</numIndex> </numIndex> <numIndex index="3" type="array"> <numIndex index="0">LLL:EXT:cms/locallang_ttc.php:recursive.I.3</numIndex> <numIndex index="1">3</numIndex> </numIndex> <numIndex index="4" type="array"> <numIndex index="0">LLL:EXT:cms/locallang_ttc.php:recursive.I.4</numIndex> <numIndex index="1">4</numIndex> </numIndex> <numIndex index="5" type="array"> <numIndex index="0">LLL:EXT:cms/locallang_ttc.php:recursive.I.5</numIndex> <numIndex index="1">250</numIndex> </numIndex> </items> <minitems>0</minitems> <maxitems>1</maxitems> <size>1</size> </config> </TCEforms> </recursive> </el> </ROOT> </sDEF> <display> <ROOT> <TCEforms> <sheetTitle>LLL:EXT:sampleflex/locallang_db.php:tt_content.pi_flexform.sheet_display</sheetTitle> </TCEforms> <type>array</type> <el> <disable_rte> <TCEforms> <label>LLL:EXT:sampleflex/locallang_db.php:tt_content.pi_flexform.disable_rte</label> <config> <type>check</type> </config> </TCEforms> </disable_rte> </el> </ROOT> </display> <error> <ROOT> <TCEforms> <sheetTitle>LLL:EXT:sampleflex/locallang_db.php:tt_content.pi_flexform.sheet_error</sheetTitle> </TCEforms> <type>array</type> <el> <show_errors> <TCEforms> <label>LLL:EXT:sampleflex/locallang_db.php:tt_content.pi_flexform.show_errors</label> <config> <type>check</type> </config> </TCEforms> </show_errors> <prepend_text> <TCEforms> <label>LLL:EXT:sampleflex/locallang_db.php:tt_content.pi_flexform.prepend_text</label> <config> <type>input</type> <size>30</size> </config> </TCEforms> </prepend_text> </el> </ROOT> </error> </sheets> </T3DataStructure>
Construction de flexforms dynamiques
Autre fonctionnalité intéressante, il est possible de rendre dynamique la construction des flexforms, en utilisant des classes PHP pour construire certains éléments. On utilise pour cela la fonction itemsProcFunc en déclarant un champ dynamique.
<dynamiqueField>
<TCEforms>
<label>une selectbox dynamique</label>
<config>
<type>select</type>
<itemsProcFunc>user_maclass->addElement</itemsProcFunc>
</config>
</TCEforms>
</dynamiqueField>
Reste ensuite à déclarer la classe class.user_maclass.php (ne pas oublier le user_) et d'y construire la fonction addElement.
Exemple :
class user_maclass { function addElement($config) { $optionList[0] = array(0 => 'option1', 1 => 'value1'); $optionList[1] = array(0 => 'option2', 1 => 'value2'); $config['items'] = array_merge($config['items'],$optionList); return $config; } }
la class class.user_maclass.php doit être incluse par le fichier ext_tables.php de la façon suivante :
include_once(t3lib_extMgm::extPath($_EXTKEY).'class.user_maclass.php');
Côté FrontOffice
Du côté du front office on peut récupérer les valeurs du flexform de la façon suivante :
$this->pi_initPIflexForm(); // Init and get the flexform data of the plugin $this->lConf = array(); // Setup our storage array... $piFlexForm = $this->cObj->data['pi_flexform']; foreach ($piFlexForm['data'] as $sheet => $data) foreach ($data as $lang => $value) foreach ($value as $key => $val) $this->lConf[$key] = $this->pi_getFFvalue($piFlexForm, $key, $sheet);
A la fin de ce code, vous avez un tableau contenant toutes les valeurs contenues dans votre flexform qu'il est désormais facile d'exploiter.
