简体   繁体   中英

How to make a predefined fieldset in TYPO3 Forms?

Using ext:form , I'm trying to make a custom field to collect addresses. So I reused the code for Fieldset to make a custom element:

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" xmlns:formvh="http://typo3.org/ns/TYPO3/CMS/Form/ViewHelpers" data-namespace-typo3-fluid="true">

<formvh:renderRenderable renderable="{element}">
    <fieldset id="{element.uniqueIdentifier}" class="form-group{f:if(condition: element.properties.elementClassAttribute, then: ' {element.properties.elementClassAttribute}')}" value="24">
        <fp:address page="{element.parentRenderable}"/>
        
        <f:if condition="{element.label}">
            <legend ><f:format.html parseFuncTSPath="lib.tx_form.parseFunc">{formvh:translateElementProperty(element: element, property: 'label')}</f:format.html></legend>
        </f:if>

        <f:for each="{addressElements}" as="element">
            <f:render partial="{element.templateName}" arguments="{element: element}" />
        </f:for>
        
    </fieldset>
</formvh:renderRenderable>
</html>

In the partial, I can manage to generate the fields in a Seciton using a custom view helper,

$addressFields = [
    'address-full-name' => 'Full name',
    'address-email' => 'Email',
    'address-phone' => 'Phone',
    'address-street' => 'Street',
    'address-city' => 'City',
    'address-state' => 'State',
    'address-zip' => 'Zip',
];
$fieldElements = [];
/** @var Section */
$section = $renderingContext->getVariableProvider()->get('element');

foreach ($addressFields as $identifier => $label) {
    $element = $section->createElement($identifier, 'Text');
    $element->setLabel($label);
    $fieldElements []= $element;
}

$renderingContext->getVariableProvider()->add('addressElements', $fieldElements);

but the fields don't show up in summary pages or with an email finisher; I'm assuming because they aren't defined in yaml.

Is it possible to have a preset group of fields without having to explicitly define the individual text fields in the form definition? I'd like to be able to just drop a field called "Address" into a form at will, using the form editor.

Using hooks is a lot simpler than what I was trying.

class AddressHooks 
{
    /**
     * @param RenderableInterface $renderable
     * @return void
     */
    public function initializeFormElement(RenderableInterface $renderable)
    {
        if ($renderable->getType() === 'Address') {
            $addressFields = [
                'address-full-name' => 'Full name',
                'address-email' => 'Email',
                'address-phone' => 'Phone',
                'address-street' => 'Street',
                'address-city' => 'City',
                'address-state' => 'State',
                'address-zip' => 'Zip',
            ];
    
            $fieldElements = [];
            
            /** @var Section */
            $section = $renderable;
    
            foreach ($addressFields as $identifier => $label) {
                $element = $section->createElement($identifier, 'Text');
                $element->setLabel($label);
                $fieldElements []= $element;
            }
        }
    }
}
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'][1595622570]
    = \Vendor\Package\Hooks\AddressHooks::class;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM