We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Field type with a pair tag

Hello,

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:

  1. Use a single tag to render the field type in edit forms
  2. Use an attribute (called repeater) to point to another template that has additional fields
  3. Render the additional fields from the template
  4. 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 text, textarea and 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.

Hussein Al Hammad

Hussein Al Hammad 105 points

  • 9 months ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

I've had a discussion with another developer about this. The discussion was more about the use case of such a field type than the technical approach. So in case others are thinking it too:

What's the point of the custom field type here?

The field type allows the editor to add multiple images at once (new or existing) instead of clicking "add item" then adding the image, then clicking "add item" and adding the image, and so on.

It also has a grid view in which all the additional fields are hidden (so the editor only sees the thumbnails) which can make it easier to reorder the images.

Why not use the Perch Gallery app (or build your custom gallery app) and pull in the images from there?

This suggested workflow works, but not necessarily ideal.

If I have some 50+ Shop products and I want to add 4-8 images to each product, having something like a repeater to add a smaller number of images directly to the product is easier than:

  • Create the product
  • Go to a separate app
  • Create an album
  • Upload the images
  • Go back to the product
  • Select the album from a field like albumlist
  • Then to edit the images you have to find the album that corresponds to the product!

So what I'm trying to say is that what this field type solves and what an app like Perch Gallery solves are different things. Not to mention that the Perch Gallery app doesn't fully use Perch's asset resource system.


Now back to the original subject:

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.

The field type allows the editor to add multiple images at once

I like the sound of this.

Does this exist? The ability to add multiple images in one go would be very useful.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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?

Template handlers aren't used in edit forms, but field types are. It's fairly simple for me to add the ability for a field type to register tag pairs it's interested in handling.

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.

Yes, field types do this. If you know what type you want you can invoke a new field type and tell it to render its fields.

$FieldType = PerchFieldTypes::get($Tag->type, $Form, $Tag);
$output = $FieldType->render_inputs();
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Template handlers aren't used in edit forms, but field types are. It's fairly simple for me to add the ability for a field type to register tag pairs it's interested in handling.

That would be handy! Does this mean adding something like the following would be enough for the field to be added to the edit form?

<perch:gallery id="images">
<!--* fields *-->
</perch:gallery>

And would I be correct to assume that there would be a way to get the tags inside tag pairs?


Yes, field types do this. If you know what type you want you can invoke a new field type and tell it to render its fields.

Ah this should make things easier. When I do this I see the input fields are added with empty id. Is there a way for me to set it in this context?