2

I have a front-end profile form that is using a Super Table field to populate a user's phone number.

If the user has not populated the field before, everything works great. I am able to use new1 to collect the user's information.

It is when I am trying to display existing data that I am getting the error

Impossible to access an attribute ("id") on a string variable ("92").

I am following this guide to help show existing data.

This is what my template looks like:

{% set fieldHandle = 'phoneNumber' %}
{% set field = craft.fields.getFieldByHandle(fieldHandle) %}
{% set blocktype = craft.superTable.getSuperTableBlocks(field.id)[0] %}

<input type="hidden" name="fields[{{ fieldHandle }}]" value="">


<label for="phoneNumber">Phone Number</label>

{% if currentUser.phoneNumber.phoneNumber|length %}
    {% for block in currentUser[fieldHandle] %}
        <input type="hidden" name="fields[{{ fieldHandle }}][{{ block.id }}][type]" value="{{ blocktype }}">
        <input type="hidden" name="fields[{{ fieldHandle }}][{{ block.id }}][enabled]" value="1">

        <input type="text" name="fields[{{ fieldHandle }}][{{ block.id }}][fields][phoneNumber]" value="{{ block.phoneNumber.phoneNumber }}">    
    {% endfor %}
{% else %}
    <input type="hidden" name="fields[{{ fieldHandle }}][new1][type]" value="{{ blocktype }}">
    <input type="hidden" name="fields[{{ fieldHandle }}][new1][enabled]" value="1">

    <input type="text" name="fields[{{ fieldHandle }}][new1][fields][phoneNumber]"
    {%- if currentUser is defined and currentUser.phoneNumber.phoneNumber|length -%}value="{{ currentUser.phoneNumber.phoneNumber }}"{%- endif -%}>
{% endif %}

While trying to debug myself, this:

{{ currentUser[fieldHandle] }} // 92

So this:

{% for block in currentUser[fieldHandle] %}

...is really saying "for block in 92". So, that is why I am getting the error, I'm not sure what the correct syntax is to resolve the error though.

My super table is set to have a max rows of 1 and static field is selected.

Thank you for any suggestions!

3

I think the key thing here is that its a static Super Table field, which is treated differently to a repeatable one. Instead of looping through each block, there's only one. Fortunately, you have all the required information, as this is in fact the blocktype variable.

{% set fieldHandle = 'mySuperTableField' %}
{% set field = craft.fields.getFieldByHandle(fieldHandle) %}
{% set blocktype = craft.superTable.getSuperTableBlocks(field.id)[0] %}

<form method="post" accept-charset="UTF-8">
    {{ getCsrfInput() }}
    <input type="hidden" name="action" value="users/saveUser">
    <input type="hidden" name="userId" value="{{ currentUser.id }}">

    {# Start with the top-level Super Table field #}
    <input type="hidden" name="fields[{{ fieldHandle }}]" value="">

    {# Ensure any existing rows in your Super Table field are saved #}
    <input type="hidden" name="fields[{{ fieldHandle }}][{{ blocktype }}][type]" value="{{ blocktype }}">
    <input type="hidden" name="fields[{{ fieldHandle }}][{{ blocktype }}][enabled]" value="1">

    <input type="text" name="fields[{{ fieldHandle }}][{{ blocktype }}][fields][customField1]" value="Value 1">
    <input type="text" name="fields[{{ fieldHandle }}][{{ blocktype }}][fields][customField2]" value="Value 2">
    <input type="text" name="fields[{{ fieldHandle }}][{{ blocktype }}][fields][customField3]" value="Value 3">

    <input type="submit">
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.