Cms: Can't access the labels of dropdown fields nested in table fields.

Created on 10 Sep 2019  路  4Comments  路  Source: craftcms/cms

Description

It seems the new nested dropdown fields in Craft 3.2 aren't as powerful as "normal" dropdown fields. Case and point; I'd like to access my dropdown field labels and that doesn't seem to be possible. (Looks like the selected value is being cast to a string as opposed to opening up all the available option data.)

For example, here is a "normal" dropdown field dumped (using {% dd() %}):

craft\fields\data\SingleOptionFieldData#1
(
    [craft\fields\data\SingleOptionFieldData:_options] => [
        0 => craft\fields\data\OptionData#2
        (
            [label] => 'Test'
            [value] => 'test'
            [selected] => true
        )
        1 => craft\fields\data\OptionData#3
        (
            [label] => 'Test 2'
            [value] => 'test2'
            [selected] => false
        )
    ]
    [label] => 'Test'
    [value] => 'test'
    [selected] => true
)

And then the exact same dropdown field, just nested inside a table field now:

[
    0 => [
        'col1' => 'test'
        'nestedDropdown' => 'test'
    ]
]

Is there a possibility of getting some more advanced functionality for nested dropdown fields?

Additional info

  • Craft version: 3.2.9
  • PHP version: 7.1.30
  • Database driver & version: MariaDB 10.3.15
  • Plugins & versions: ~

Most helpful comment

Gotcha... For anyone else that runs across this issue, I ended up building a macro that queries the field itself to get all that info:

{% macro getLabel(context, tableHandle, rowNumber, dropdownFieldHandle) %}
    {% set value = context[tableHandle][rowNumber - 1][dropdownFieldHandle] %}
    {% set field = craft.app.fields.getFieldByHandle(tableHandle) %}
    {% set nestedDropdown = field.columns | filter( col => col.handle == dropdownFieldHandle) | first %}
    {% set selectedOption = nestedDropdown.options | filter( option => option.value == value) | first %}
    {{ selectedOption.label }}
{% endmacro %}

{{ _self.getLabel(entry, 'aTable', 1, 'nestedDropdown') }}

All 4 comments

Just stumbled across Brandon's comment on this issue which most likely answers my question here... 馃憖

Yeah it鈥檚 just not possible, sorry. You will need to hard-code the labels in your templates.

Gotcha... For anyone else that runs across this issue, I ended up building a macro that queries the field itself to get all that info:

{% macro getLabel(context, tableHandle, rowNumber, dropdownFieldHandle) %}
    {% set value = context[tableHandle][rowNumber - 1][dropdownFieldHandle] %}
    {% set field = craft.app.fields.getFieldByHandle(tableHandle) %}
    {% set nestedDropdown = field.columns | filter( col => col.handle == dropdownFieldHandle) | first %}
    {% set selectedOption = nestedDropdown.options | filter( option => option.value == value) | first %}
    {{ selectedOption.label }}
{% endmacro %}

{{ _self.getLabel(entry, 'aTable', 1, 'nestedDropdown') }}

Awesome. Thank you for the workaround @jalendport! Works a treat on my project a la
{{ macros.getLabel(model, 'productSpecs', loop.index, 'name') }} in my {% for %} loops.

Was this page helpful?
0 / 5 - 0 ratings