Support

Account

Home Forums ACF PRO get_field_object issues with load_field and referenced variables

Solved

get_field_object issues with load_field and referenced variables

  • Hi,

    I have an unusual situation and I am not sure if it is by design, or a bug (I submitted a similar post in the bug reports forum on this topic but I really need help with it as it may not be a bug,) or something I am not doing properly. I have spent a week on this and isolated the problem.

    It is a two-part problem that occurs in different situations but appears to be related.

    Problem one:
    In this first problem, I need to be able to access the field object (get_field_object) for an array of field keys to build an array of the fields that have a custom field argument set, which was added with the action acf/render_field_settings.

    Then loop this array with load_field. However, since we have already called these fields with the get_field_object it will not run the filter.

    // Create an array to hold field keys for fields that have the field argument of sorting-filtering turned on. 
    $filter_sorts = array();
    		
    // This is a partial array to illustrate the problem, there are over 50 fields in the post that could be in the array which would be dynamically built with get_post_meta() for all the fields on the post.
    $collected_fields = array( 'field_5c60c179e7ffd', 'field_5c5771f906565', );
    		
    // Loop the array.
    foreach ( $collected_fields as $collected_field ) {
    			
    // Get the field object for the fields in the array.
    	$field_settings = get_field_object( $collected_field, 9344 );
    			
    	// Avoid the php unidentified object error for fields that do not have sorting-filtering turned on. 
    	if ( !isset( $field_settings[ 'sorting-filtering' ] ) ) continue;				
    	// Make sure sorting-filtering is not empty; if it is continue foreach.
    	if ( is_null( $field_settings[ 'sorting-filtering' ] ) ) continue;
    	if ( empty( $field_settings[ 'sorting-filtering' ] ) ) continue;
    		
    	// Add field keys that have sorting-filtering turned on.
    	$filter_sorts[] = $field_settings[ 'key' ];
    		
    } // END foreach loop
    
    // filter_sorts properly equals ["field_5c5771f906565"] because this is the only field of the two that has sorting-filtering turned on. Again this could be out of many fields that were passed by get_post_meta.
    				
    //Loop the filter_sorts array after it has been filled.		
    foreach ( $filter_sorts as $filtersort ) {
    			
    	// Add the load_field filter
    	add_filter('acf/load_field/key='.$filtersort, function ( $field ) {
    				
    		// Do someting
    				
    		return $field;
    
    	});
    			
    } // END foreach loop

    THE CODE ABOVE WILL NOT FIRE OFF THE FILTER because we have called field ‘field_5c5771f906565’ with the get_field_object earlier in the function.

    HOWEVER IF WE:

    // Just send the field (as a test) by itself in an array without using get_field_object with the field that has sorting-filtering turned on it will run the filter.
    $filter_sorts = array( 'field_5c5771f906565', );
    		
    //Loop the filter_sorts array.		
    foreach ( $filter_sorts as $filtersort ) {
    			
    	// Add the load_field filter
    		add_filter('acf/load_field/key='.$filtersort, function ( $field ) {
    				
    		// Do someting
    				
    		return $field;
    
    	});
    			
    } // END foreach loop

    THIS FILTER WILL RUN however, it is not what we need since I would not know which fields to manually add to the filter_sorts array before using get_post_object to test them first.

    Problem Two:
    This problem also involves load_field and get_field_object.
    I need to be able to use closure to pass an array by reference so that we can add elements to the array while in the filter, which we can access after the filter has completed.

    // Create array to hold fields that have sorting-filtering turned on.
    $test_pass_by_reference = array();
    		
    // Add the filter for a field that does have sorting-filtering turned on, this would be an array of fields, however, for simplicity I have called a single field that has sorting-filtering turned on.
    add_filter('acf/load_field/key=field_5c5771f906565', function ( $field ) use ( &$test_pass_by_reference ) {
    			
    	// Add this field argument to the array
    	$test_pass_by_reference = $field['sorting-filtering'];
    
            // Change a field argument
            $field['wrapper']['class'] = 'error';
    			
    	return $field;
    
    });

    At this point the test_pass_by_reference is an empty array but should contain a value.
    $test_pass_by_reference = [];

    However, if I call the same field with get_field_object directly after the filter has been applied.
    $field_settings = get_field_object( ‘field_5c5771f906565’, 9344 );

    Then the test_pass_by_reference array will show the proper value. It is confusing as to why this would even happen and what is holding up the variables from appearing from the PHP passed reference?
    $test_pass_by_reference = [ 1 ];

    It is important that we can use acf/render_field_settings to add a field argument and then check that argument to decide what to do to other field arguments or vica versa to use pass by reference to add values to an array that are persistent after the filter is complete to do something to these fields without having to call get_field_object for the passed variables to exist.

    Please help.
    Thank you
    Geoffrey

  • For problem one support suggested that I use perpare_field since get_field_object() already called the field already you cannot then use load_field to call the data again.

    However, there has been no response to problem 2. Which is how do you get data to persist outside of a filter after the filter has completed.

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

The topic ‘get_field_object issues with load_field and referenced variables’ is closed to new replies.