Custom Fields
Re-Envisioned
Support MB Views Variables inside field names

  • Creator
    Topic
  • #33107
    Resolved AMX
    Participant

    Hi,

    due to certain limitations of groups and cloneable fields, I had to create multiple rows/series of regular fields. The pattern is more or less like this:

    'series_1_some_field', 'series_1_some_other_field', (...)
    'series_2_some_field', 'series_2_some_other_field', (...)

    Now I am building a template for an HTML table with all these series of these fields.
    I would like to use the iteration (series) number inside the field name, so that I don't have to repeat the same thing over and over again.

    Something like that:

    {% for i in 1..20 %}
    
    <tr role="row">
          <td role="cell"> {{ i }} </td>
          <td role="cell"> {{ post.series_[~i~]_some_field }}</td>
          <td role="cell"> {{ post.series_[~i~]_some_other_field }} </td>
    </tr>
    
    {% endfor %}

    I found the 'bracket notation' in Drupal forums, and tried a few variations,
    like {{post.[series_~i~_some_field] }} etc. But I cannot make it work, and the official Twig documentation is rather short and doesn't have too many examples.

    I know it's super easy with cloneable fields, and I use them normally, but in this case I can't use them.

    Is it possible to pass the iteration number inside the field name?

    Greetings,
    Tom

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #33124
    Long Nguyen
    Moderator

    Hi Tom,

    It's a rare case, I've not used this before. You can follow this topic to know how to access the dynamic variable name in Twig https://stackoverflow.com/questions/24697313/how-to-access-dynamic-variable-names-in-twig

    #33169
    AMX
    Participant

    The methods with attribute or _context mentioned there somehow didn't work for me.

    But I used the |join filter to construct the field id as a variable in a loop, and then mb.rwmb_meta(var) to display its value. It worked.

    Greetings,
    Tom

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.