Support

Account

Home Forums Bug Reports load_field after using get_field_object & passing a variable by reference

Unread

load_field after using get_field_object & passing a variable by reference

  • Hi,

    I have an unusual situation and I am not sure if it is by design, or a glitch, 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:
    I need to be able to use get_field_object on a field and then later in the same function use load_field filter on the same field, and it will no run the filter.

    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 this field 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

    THIS WILL NOT WORK 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 WILL WORK however, it is not what we need since I would not know which fields to manually add to the a filter_sorts array before using get_post_object to test them first.

    Problem Two:
    I need to be able to use closure to pass an array by reference so that we can add elements to it while in the filter, which we can access after the filter has ran.

    // 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 to the array
    	$test_pass_by_reference = $field['sorting-filtering'];
    			
    	return $field;
    
    });

    At this point the test_pass_by_reference is an empty array.
    $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.
    $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 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

Viewing 1 post (of 1 total)

The topic ‘load_field after using get_field_object & passing a variable by reference’ is closed to new replies.