Support

Account

Home Forums General Issues Get main group from sub clone field

Solving

Get main group from sub clone field

  • Hi,

    I try to modify a field when is used in a given group.

    My field is inside a group which is cloned in another group which is cloned in the group i want.

    group1 > (clone) group2 > (clone) group3 > the field

    When i’m in acf/load_field filter, i can get its direct parent group (group3) with parent attribute but how i can get group1 ?

    add_filter( 'acf/load_field/name=field', function($field){
    
    $field['parent']; // = group3
    
    acf_get_field_ancestors($field); // return nothing
    
    return $field;
    } );
  • I’m not sure you can do this at all. I don’t use cloned field much. But I would start by getting the parent field and see where that leads me

    
    $parent = get_field($field['parent'], $post_id);
    echo '<pre>'; print_r($parent); echo '</pre>';
    
  • Yes, for the moment i get the parent field from root group and i go through its children :

    add_filter( 'acf/get_fields',  'acf_get_fields', 10, 2 );
    function acf_get_fields( $fields, $parent ) {
    	if ( $parent['key'] === 'group1' ) {
    		foreach ( $fields as &$field ) {
    			if ( $field['name'] === 'my_parent_field' ) {
    				foreach ( $field['sub_fields'] as $k => &$sub_field ) {
    					if ( $sub_field['name'] === 'my_target_field' ) {
    						// my code
    					}
    				}
    			}
    		}
    	}
    	return $fields;
    }
    

    But it would be great if acf_get_field_ancestors works with clone fields 🙂

    Sadly, i looked at the source code and apparently, it could takes a lot of work.

  • The reason for my question was, that instead of trying to get ancestors you might try walking backwards. The parent of the field is set, is the parent of the parent field set in that field? Is the parent of that field set? etc

    but I did use the wrong function in my example

    
    $parent = acf_get_field($field['parent'], $post_id);
    echo '<pre>'; print_r($parent); echo '</pre>';
    
  • $field['parent'] returns a group name (not a field). acf_get_field works on it ? And event if it works, i will get the group in the good context in order to have its parent ? (A group can have a parent ?)

    When i have time i will test but i have little hope.

    Anyway, i will update this post with the result

  • To be completely honest, I’m not sure about the answers to your questions. There are several function in ACF that are used internally and not documented on this site. acf_get_field() is one of those. These functions can take arguments.

    acf_get_field_group() is another of these functions.

    and another is acf_get_fields($group_key), which will return an array of all of the fields in a groups, including sub fields, and I’m assuming this would include clone fields.

    Like I said in my original answer, I don’t know exactly how to do what you want to do, but somehow ACF must know do something to tell it how the clone field is related to the parent group.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.