Okay
  Public Ticket #2938634
How to dynamically assign an ACF repeater?
Closed

Comments

  • Wilko started the conversation

    I watched the example of how to use an ACF repeater, However, if I would like to assign the repeater field dynamically, how could I do it?

    For example, I have WooCommerce product with both a field 'Plusses' and 'Minuses'. The idea is that both fields - ACF repeater fields- will be filled with a list, one with plusses, the other with  minuses. I want to create one widget where I can assign either the plusses or the minuses via some attribute (let's say : List Source'). 

    So the attribute should show all ACF fields... and I can pick the field I need and use the repeater -a field with the name 'list_value' for example. 

    That would prevent me having to create a separate widget for plusses and minuses which, as far as I can see right now, would be the only method to do. 

  •  1,194
    Amit replied

    I dont understand you completely but in general you can do anything using twig statements. 

    There are all sorts of examples here:

    https://unlimited-elements.com/docs/twig-code-example-if-else/

  • Wilko replied

    Thanks for the prompt reply. 

    What I would like to achieve is that the widget itself is agnostic about the name of the ACF field, so I can enter that name as an attribute. 

    Is there a way to use a text field to retrieve an ACF field? 

    I do realize that if there is not, I can do it with an 'if else' statement, but in that case I would have to enter all potential cases in the code, which makes it less flexible.

    Let's say, next time I would need the same functionality, but this time the fields are not 'Plusses' and 'Minuses', but 'Some list with general attributes'. 

  •   Max replied privately
  • Wilko replied

    > HI, you can use "set myList = pluses" 

    Yes, that's no problem but that would mean that the widget itself is not 'agnostic' of the datastructure behind it. I've actually got the whole widget working, but I'm not satisfied with the solution, since it isn't a solution I can easily use for other purposes. 

    It's more the idea that 'the code isn't as it ought to be' than 'I can't get the specific functionality working'.

    My preferred solution would be that in the widget the user of the widget first gets an ACL repeater field to choose from, when selecting it, subsequently they can enter the name of the field used within the repeater field. 


  •   Max replied privately
  •   Wilko replied privately
  •   Max replied privately