Edit on GitHub
Jump to docs navigation

Field Types / Set field

Note: You are currently reading the documentation for Bolt 4.0. Looking for the documentation for Bolt 3.7 instead?

The set field allows you to organise semantically meaningful sub-fields together. A set defines the sub-fields that it has, which are then accessed using the set. Sets are usually used in combination with Collections, to create arrays of entities that have more than one value (e.g. a collection of Books where a Book has a title, publication date and author.)

Basic Configuration:

The configuration of a Set field consists of a few parts:

  • The 'key', used to identify the name of the set field.
  • type: set to define it as a set type.
  • Other optional values, like in other field types (like 'label' or 'group').
  • A fields key, that contains one or more sub-fields, for example text, image, html and select.

A straighforward example can look like this:

        contentset:
            type: set
            label: Content Set
            group: Sets
            fields:
                image:
                    label: Image with description
                    type: image
                text:
                    label: Paragraph
                    type: text

As you can see the field is configured with a type of set and then the sub-fields are configured under the fields attribute. In general you can include any valid fields within a set, there are a few that are not supported.


Note: Sets are useable with most of the available field types, except for a few where it would get too complex, or where it simply does not make sense to have more than one of. In short, do not use type: slug, type: set, type: collection or type: templateselect as fields in your set. These fields will not work as expected.

Example usage in templates:

When you want to use sets in your templates you will need to iterate over a set in order to access the individual sections of fields.

For instance if you just want to iterate over all fields within the set then the template code will look like this:

{% for field in record.contentset %}
    {{ field.fieldtype }}:
    {{ field }}
{% endfor %}

In the example above using {{ field }} will just output the value of the sub-field, if it is a text field such as like text, html, textarea or markdown. Since these are sometimes more complex fields you can use {{ field|image }} for images or {{ dump(field.value) }} to dump the value regardless of type. In practice, you'll often want to use the techniques described in the section below for individual fields to output the specific type of field in the layout you require.

Alternatively, if you want to loop over fields and get their names as well, you can use:

{% for name, field in record.contentset %}
    {{ name }}
    {{ field }}
{% endfor %}

If you know the names of the fields you want to render then you can fetch a field from the set by name. For instance using the same example as above but knowing that our set comprises of sub-fields image and text, we can output the fields like this:

    {{ record.contentset.image }}
    {{ record.contentset.text }}

Default value

The default value of the fields inside a set should be configured for the entire set, as shown in the example below.

Example:

        contentset:
            type: set
            label: Content Set
            group: Sets
            fields:
                image:
                    label: Image with description
                    type: image
                paragraph:
                    label: Paragraph
                    type: text
            default:
                paragraph: "This is teh default value of the paragraph field in the contentset"

Note: The value specified under default:text must match the default value for that field. For example, the default value of a text field is a string, while the default for an image is an object.



Edit this page on GitHub
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.