Field type with a pair tag
Before I get into it, this thread is here for discussion. So if you are an add-on developer, feel free to join the discussion. Input from Perch support is always welcome.
I have recently created a gallery field type that behaves in a similar way to repeaters, but it has other features that makes managing a variable number of images a bit easier than repeaters. The point is, the field type, similar to repeaters, accepts custom fields per item.
Template handlers, as far as I know, don't render the fields in edit forms. They are only used for output. So I couldn't use the following to add custom fields per item:
<perch:gallery id="images"> <perch:gallery id="image" type="image" label="Image"> <perch:gallery id="title" type="text" label="Title"> <perch:gallery id="unicorn" type="checkbox" value="1" label="Unicorn"> </perch:gallery>
What I ended up doing is:
- Use a single tag to render the field type in edit forms
- Use an attribute (called
repeater) to point to another template that has additional fields
- Render the additional fields from the template
- Use a repeater tag for output
<!--* This is used for editing *--> <perch:content id="gallery" type="gallery" label="Gallery" repeater="content/_gallery.html" suppress> <!--* This is used for output only *--> <perch:repeater id="gallery"> <perch:template path="content/_gallery.html"> </perch:repeater>
This works and I'm ok with the workflow. The part that I wish to improve is (3).
At the moment I'm checking the field types in
repeater="content/_gallery.html" and only rendering basic types like
checkbox. So basically:
$Form = $API->get('Form'); $Form->text($input_id, $value, $classes);
This works, but means I need to add support for each field type myself which is not ideal.
This also mean these fields may behave differently when added by my field type. For example, the
textarea field in my field type can't use any editors since I'm only rendering it as a regular text box and nothing else.
Questions / points to discuss:
(1) Edit form approach: Template handlers (pair tag) VS a single tag for editing plus a repeater for output. Is using template handlers even an option in the context of edit forms?
(2) Considering the approach I used, is there a better way to render the additional fields? Ideally any installed field type (built-in or third-party) can be used without me checking what type it is and rendering it myself.