advancedforms / advanced-forms

WordPress plugin to create forms using Advanced Custom Fields
75 stars 14 forks source link

af_get_field not working with sub fields inside "Group" field who is inside a "Group" field #76

Closed cedricDevWP closed 4 years ago

cedricDevWP commented 4 years ago

I commented on this problem https://github.com/advancedforms/advanced-forms/issues/35 but I prefer to create a new one.


Hi,

I think there is a problem when you have a group that owns another group.

Example :

1 ACF : image

who has a acf dog (see 2 ACF)

2 ACF : image

Who has a acf owner (see 3 ACF)

3 ACF : image

If i do :

af_get_field("civilite") => Return false

When i see the data : image

We do not go through the group

to correct I add:

if ( $sub_field_name == $field_key_or_name ) { return $sub_field_value; }elseif( $sub_field_name == 'groupe'){ foreach ($sub_field_value as $sub_field_name_2 => $sub_field_value_2) { if ( $sub_field_name_2 == $field_key_or_name ) { return $sub_field_value_2; } } }

image

for the future it would not be judicious to use the indentifiants and not the names like key?

fabianlindfors commented 4 years ago

Hi!

As you've noticed af_get_field won't go down nested groups. This is because there could be naming collisions of fields causing unexpected behavior.

The way ACF handles this makes it a bit hard. Because these fields are considered sub fields you have to go through the parent field to find its value. I would recommend doing something like this to fetch a sub field of a group: af_get_field( 'groupe' )[ 'civilite' ].

I've been thinking of ways to make this easier for a while but haven't come up with a good solution yet. Sorry about that!