FlexForms

FlexForms can be used to store data within an XML structure inside a single DB column. The typical use case is to configure plugins within a content element.

You may want to configure individual plugins differently, depending on where they are added. The configuration set via the FlexForm mechanism applies to only the content record it has been configured for. The FlexForms configuration for a plugin can be changed by editors in the backend. This gives editors more control over plugin features and what is to be rendered.

Using FlexForms you have all the features of TCA, so it is possible to to use input fields, select lists, show options conditionally and more.

Example Use Cases

  • Use the plugin to configure a different view, e.g. list, single
  • The bootstrap_package uses FlexForms to configure rendering options, e.g. a transition interval and transition type (slide, fade) for the carousel.
../../_images/FlexFormCarousel.png

How it Works for Content Elements

  1. In the extension, a configuration schema is defined and attached to one or more plugins.
  2. When the plugin is added to a page, it can be configured as defined by the configuration schema.
  3. The configuration for this content element is automatically saved to tt_content.pi_flexform.
  4. The extension can read current configuration and act according to the configuration.

Steps to Perform (Extension Developer)

  1. Create configuration schema in T3DataStructure format (XML)

    Example: Configuration/FlexForms/Registration.xml.

    <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
    <T3DataStructure>
        <sheets>
            <sDEF>
                <ROOT>
                    <TCEforms>
                        <sheetTitle>LLL:EXT:extkey/Resources/Private/Language/Backend.xlf:settings.registration.title</sheetTitle>
                    </TCEforms>
                    <type>array</type>
                    <el>
                        <!-- Add settings here ... -->
    
                        <!-- Example setting: input field with name settings.timeRestriction -->
                        <settings.includeCategories>
                            <TCEforms>
                                <label>LLL:EXT:example/Resources/Private/Language/Backend.xlf:settings.registration.includeCategories</label>
                                <config>
                                    <type>check</type>
                                    <default>0</default>
                                    <items type="array">
                                        <numIndex index="0" type="array">
                                            <numIndex index="0">LLL:EXT:bootstrap_package/Resources/Private/Language/Backend.xlf:setting.registration.includeCategories.title</numIndex>
                                        </numIndex>
                                    </items>
                                </config>
                            </TCEforms>
                        </settings.includeCategories>
    
                        <!-- end of settings -->
    
                    </el>
                </ROOT>
            </sDEF>
        </sheets>
    </T3DataStructure>
    
  2. The configuration schema is attached to one or more plugins

    The vendor name is Myvendor, the extension key is example and the plugin name is Registration.

    In Configuration/TCA/Overrides/tt_content.php add the following:

    $GLOBALS['TCA']['tt_content']['types']['list']['subtypes_addlist']['example_registration'] = 'pi_flexform';
    \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPiFlexFormValue(
        // plugin signature: <extension key without underscores> '_' <plugin name in lowercase>
        'example_registration',
        // Flexform configuration schema file
        'FILE:EXT:example/Configuration/FlexForms/Registration.xml'
    );
    

    Tip

    The plugin signature is used in the database field tt_content.list_type as well, when the tt_content record is saved. If you are confused about how to handle underscores and upper / lowercase, check there to see what your plugin signature is.

    Also look on the page Naming conventions.

  3. Access the settings in your extension:

    The settings can be read using one of the methods described below, e.g. from an Extbase controller action, from a PHP function (without using the Extbase framework), from TypoScript or from within a Fluid template.

More Examples

The definition of the data types and parameters used complies to the column types defined by TCA.

The settings must be added within the <el> element in the FlexForm configuration schema file.

Select Field

<settings.orderBy>
    <TCEforms>
        <label>LLL:EXT:example/Resources/Private/Language/Backend.xlf:settings.registration.orderBy</label>
        <config>
            <type>select</type>
            <renderType>selectSingle</renderType>
            <items>
                <numIndex index="0">
                    <numIndex index="0">LLL:EXT:example/Resources/Private/Language/Backend.xlf:settings.registration.orderBy.crdate</numIndex>
                    <numIndex index="1">crdate</numIndex>
                </numIndex>
                <numIndex index="1">
                    <numIndex index="0">LLL:EXT:example/Resources/Private/Language/Backend.xlf:settings.registration.orderBy.title</numIndex>
                    <numIndex index="1">title</numIndex>
                </numIndex>
            </items>
        </config>
    </TCEforms>
</settings.orderBy>

See also

Populate a select Field with a PHP Function (itemsProcFunc)

<settings.orderBy>
    <TCEforms>
        <label>LLL:EXT:example/Resources/Private/Language/Backend.xlf:settings.registration.orderBy</label>
        <config>
            <type>select</type>
            <itemsProcFunc>Myvendor\Example\Backend\ItemsProcFunc->user_orderBy</itemsProcFunc>
            <renderType>selectSingle</renderType>
            <items>
                <!-- empty by default -->
            </items>
        </config>
    </TCEforms>
</settings.orderBy>

The function user_orderBy populates the select field in BackendItemsProcFunc.php:

class ItemsProcFunc
{
     /**
     * Modifies the select box of orderBy-options.
     *
     * @param array &$config configuration array
     */
    public function user_orderBy(array &$config)
    {
        // simple and stupid example
        // change this to dynamically populate the list!
        $config['items] = [
            // label, value
            ['Timestamp', 'timestamp'],
            ['Title', title]
        ];
    }

    // ...
 }

How this looks when configuring the plugin:

../../_images/FlexFormsItemsProcFunc.png

See also

Display Fields Conditionally (displayCond)

Some settings may only make sense, depending on other settings. For example in one setting you define a sorting order (by date, title etc.) and all sort orders except “title” have additional settings. These should only be visible, if sort order “title” was not selected.

You can define conditions using displayCond. This dynamically defines whether a setting should be displayed when the plugin is configured. The conditions may for example depend on one or more other settings in the FlexForm, on database fields of current record or be defined by a user function.

Again, the syntax and available fields and comparison operators is documented in the TCA reference:

See also

How to Read FlexForms From an Extbase Controller Action

The settings can be read using $this->settings in an Extbase controller.

$includeCategories = (bool) ($this->settings['includeCategories'] ?? false);

Important

If you wish to access a setting from your controller via $this->settings, the name of the setting must begin with settings directly followed by a dot (.).

How to Read and Write FlexForms From PHP

Some situation make it necessary to access FlexForms via PHP. The following APIs are available to work with FlexForms from within PHP:

In order to convert a FlexForm to a PHP array, the xml2array method can be used:

$flexFormArray = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($flexFormString);

In order to convert an PHP array into an FlexForm, the :php`flexArray2Xml` method can be used:

$flexFormTools = new \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools();
$flexFormString = $flexFormTools->flexArray2Xml($flexFormArray, true);

How to Access FlexForms From TypoScript

It’s possible to read values from FlexForms within TypoScript, this is explained within the TypoScript Reference:

See also

How to Access FlexFroms From Fluid

FlexForm settings can be read from within a Fluid template using {settings}.

Steps to Perform (Editor)

After inserting a plugin, the editor can configure this plugin by switching to the tab “Plugin” or whatever string you defined to replace this.

../../_images/FlexformBackend.png

Credits

Some of the examples were taken from the extensions news (by Georg Ringer) and bootstrap_package (by Benjamin Kott).

Further enhancements by the TYPO3 community are welcome!