Taming the Typo3 Flexform Radio Button with itemsProcFunc
Typo3's Flexform is a powerful tool for creating complex and configurable content elements. When building a custom content element, you might find yourself needing a radio button field with dynamic options. This is where itemsProcFunc
comes in, allowing you to populate your radio buttons with data from a function.
Scenario: Dynamic Options for a Content Element
Let's imagine you're building a "Testimonial" content element. You want a field to choose the testimonial's "type," which could be "Customer," "Employee," or "Partner." Instead of hardcoding these options, you want them to be dynamically fetched from a database table.
Here's the initial Flexform code:
<TCEforms>
<sheet title="Testimonial Settings">
<field name="testimonial_type" label="Testimonial Type">
<config>
<type>select</type>
<renderType>selectSingle</renderType>
<itemsProcFunc>yourExtensionKey\MyExtension\Hooks\Flexform::getTestimonialTypes</itemsProcFunc>
</config>
</field>
</sheet>
</TCEforms>
Dissecting the Code
itemsProcFunc
: This attribute defines the function that will provide the radio button options.yourExtensionKey\MyExtension\Hooks\Flexform::getTestimonialTypes
: This is the fully qualified path to your function within your extension.
The Power of itemsProcFunc
The beauty of itemsProcFunc
lies in its flexibility. You can leverage this attribute to dynamically populate radio buttons with:
- Data from your database: Fetch data from your database tables and display it as radio button options.
- External APIs: Retrieve data from external services and display it in the form.
- Custom logic: Implement custom logic to generate a list of options based on specific conditions or user preferences.
Building the Function
To complete the scenario, here's how you would implement the getTestimonialTypes
function:
<?php
namespace yourExtensionKey\MyExtension\Hooks\Flexform;
use TYPO3\CMS\Core\Database\ConnectionPool;
class Flexform {
public static function getTestimonialTypes() {
$queryBuilder = ConnectionPool::getInstance()->getQueryBuilderForTable('your_table_name');
$statement = $queryBuilder
->select('uid', 'title')
->from('your_table_name')
->where('type = "testimonial"')
->execute();
$items = [];
while ($row = $statement->fetchAssociative()) {
$items[] = [$row['uid'], $row['title']];
}
return $items;
}
}
Explanation:
- We connect to the database and fetch the necessary data (in this example, the "uid" and "title" of testimonial entries) from your database table.
- We construct a multidimensional array with the "uid" and "title" for each testimonial type, which will be used as the radio button values and labels respectively.
- The
items
array is returned, which is used by Typo3 to render the radio buttons.
Additional Considerations
- Caching: For performance optimization, ensure that the data fetched for dynamic radio buttons is cached appropriately.
- Error Handling: Always include error handling in your
itemsProcFunc
to gracefully handle cases where data retrieval fails.
Conclusion
By using itemsProcFunc
, you can create dynamic and flexible radio buttons within your Typo3 Flexform. This opens up possibilities for creating more robust and interactive content elements that adapt to your specific needs. Remember to consider caching and error handling to ensure the smooth operation of your itemsProcFunc
functions.
Further Reading: