Reuse field groups in templates multiple times

I want to reuse field groups multiple times in a template.

Simplified example

blueprints/fields/button.yml

type: group
fields:
    buttonText:
        label: Text
        type: text
        required: true
    buttonUrl:
        label: URL
        type: url
        required: true
    buttonColor:
        label: Color
        type: select
        options:
            red: Red
            green: Green
            blue: Blue

blueprints/pages/example.yml

title: Example

fields:
    headline:
        label: Headline
        type: text
    body:
        label: Body
        type: textarea
    buttonOne: fields/button
    notes: 
        label: Notes
        type: textarea
    buttonTwo: fields/button

I am aware that the example above does only work for the first reuse of the button.yml field group since field names contained in the field group must be unique.

Is there a way to change or prefix the field names within a field group when reusing/extending it in a template?

Using blocks is not an option in that case since the client needs to manage a huge amount of pages with a given content structure.

Putting the single fields from button.yml directly in the template seems repetitive.

Putting field groups into other field types such as objects or structures works technically but seems more like a workaround, especially when applied to many field groups that are used to build a component-like system with buttons, accordions, sliders, etc. (probably just my perception).

I have read some older related threads (1, 2) but it seems that there was no solution so far.

For another CMS I saw a solution where you could add a “prefix” property to the reused field group that prefixes all field names in that field group.

How do you solve this in your projects? Do I miss something in the guide or reference?

1 Like

Maybe programmable blueprints could be of help to you. Have you heard about them?

1 Like

For some background: The reason why this doesn’t work is because it result in fields having the field name, so the next field with the same name will overwrite the first etc.

1 Like

Thank you very much, I had indeed not yet considered the programmable blueprints. As far as I have been able to understand the cookbook so far, I can realise my blueprint with it.

Thank you for the background information. I had hoped that there would be a simple solution, e.g. in the form of field properties that allow directly extending field groups in the same template more than once.

Is a solution already being planned, e.g. using a “prefix” field property? If not, I would create a feature request for this.

There’s nothing like this planned so far. Please feel free to add it to feedback.getkirby.com

Personally, I would go for programmable blueprints.

Another option could be to just have the three fields separately in blueprint “snippets” and then do something like

prefixA_buttonText: fields/button-text
prefixA_buttonUrl: fields/button-url
prefixA_buttonColor: fields/button-color

While a bit more verbose, it sticks to pure yml blueprints and keeps the markup still relatively compact - probably not much more than you would have when you need to pass on the prefix to the extend.

2 Likes

I just came across here while researching another topic, but I thought I leave a hint to the object field (from Kirby 3.8 onwards). I think it is a often overlooked but great field and might serve you well here:

You can group your button related fields into a single field, e.g.

// site/blueprints/fields/button.yml
type: object
fields:
  link:
    type: link
  label:
    type: text
//. ..

and reuse it in your page blueprint

// site/blueprints/pages/default.yml
buttonA: fields/button
buttonB: fields/button

The fields are now neatly grouped alongside each other in the panel and you can reuse the whole config.

Probably not helpful anymore to you, but maybe helpful to some other people who come across here.

Cheers

2 Likes

Thank you. I will definitely give the object field a try!