I'm working on what I thought would be a relatively simple plugin; a custom fieldtype that would have 8 "sub-groups", each of which would consist of a numeric text field and a dropdown field with a few predefined options.
At the Twig/templating end of things, I'd like it to work like this:
{{ entry.customField.subGroup.text }} // (outputs the text field for that sub-group)
{{ entry.customField.subGroup.dropdown }} // (outputs the dropdown field for that sub-group)
{{ entry.customField.subGroup }} // (outputs a combined string of `subGroup.text` and `subGroup.dropdown`)
Additionally, in the field's settings, I have 8 lightswitch fields to disable any individual sub-group (the {% if %}
statement wrapping the code below).
All together, I'm putting it all together in the CustomField_input.twig
file like this:
{% set dropdownValues = [
{ label: 'Foo', value: 'foo' },
{ label: 'Bar', value: 'bar' },
{ label: 'Baz', value: 'baz' }
] %}
{% if field.subGroup %}
{% embed '_includes/forms/field' with {
label: 'Sub-Group A'|t,
instructions: "A sub-group of the field"|t,
id: 'subGroupA'
} %}
{% import "_includes/forms" as forms %}
{% block input %}
<div class="flex">
{{ forms.textField({
id: 'subGroupAText',
name: 'subGroupAText',
type: 'number',
value: field.subGroupAText ?? ''
}) }}
{{ forms.selectField({
id: 'subGroupADropdown',
name: 'subGroupADropdown',
options: dropdownValues,
value: field.subGroupADropdown ?? 'defaultDropdownValue'
}) }}
</div>
{% endblock %}
{% endembed %}
{% endif %}
First off, the above code renders the fields I want correctly, but no values get saved. Not sure what that's about. But before I resolve that, I suspect I should figure out what I need to alter in the above code to get the nested aspect of the custom field. Is it as simple as changing:
id: 'subGroupAText',
name: 'subGroupAText',
...
value: field.subGroupAText ?? ''
...to this...?
id: 'subGroupA[text]',
name: 'subGroupA[text]',
...
value: field.subGroupA[text] ?? ''