Support

Account

Home Forums Bug Reports Fetching value from cloned field in acf/load_field filter

Solving

Fetching value from cloned field in acf/load_field filter

  • Hi,

    I´m using ACF PRO v5.4.4 with WordPress 4.6.1.

    I have a field group, containing some fields, that I use as a clone in a layout in a field group with flexible content. I´m using acf/load_field to populate one of the select field with values from another field stored in wp_options. As get_field() is ran in the acf/load_field filter I get an error stating that $field['sub_fields'] is missing. This error is only occurring when the specific field group, containing the field to be filtered, is being edited.

    add_filter( 'acf/load_field/name=global_quotes_select', function( $field ) {
    
    	$global_quotes = get_field( 'global_quotes', 'option' );
    
    	foreach ( $global_quotes as $i => $quote ) {
    		$field['choices'][ $i ] = $quote['quote'];
    	}
    
    	return $field;
    
    });
    
    Notice: Undefined index: sub_fields in wp-content/plugins/advanced-custom-fields-pro/pro/fields/clone.php on line 407
    
    Warning: array_keys() expects parameter 1 to be array, null given in wp-content/plugins/advanced-custom-fields-pro/pro/fields/clone.php on line 407
    
    Warning: Invalid argument supplied for foreach() in wp-content/plugins/advanced-custom-fields-pro/pro/fields/clone.php on line 407
  • Hi @liss

    Would you mind sharing a JSON export of the field group so that I can do some testing on my end?

    I am looking forward to your response.

  • Hi @acf-support

    Thanks for your reply. I´ve attached the json-files for the field groups in question.

    Upon updating the above mentioned field group the headers already sent warning appears as well. With WP_DEBUG set to false this is of course not happening.

    Warning: Cannot modify header information - headers already sent by (output started at wp-content/plugins/advanced-custom-fields-pro/pro/fields/clone.php:405) in wp-includes/pluggable.php on line 1174

  • Hi @liss,

    I have tested the issue using the JSON files but I am not able to replicate the issue.

    Is it possible that you have a plugin conflict on your site that is causing the issue?

    Kindly try deactivating all the plugins you may have and let me know if the issue persists.

    Hope to hear from you soon.

  • Hi @acf-support

    I tried to disable all other plugins but the problem still remained.

    After that I did a fresh install of both WP and ACF PRO, without any other plugins and then using the JSON files from the first install. I then noticed that the fields appear to be treated differently when being synced and it no longer shows up as clone. The warnings and notices were no longer appearing either.

    Please see the attached screen shots that visualize the differences.

    Thanks in advance.

  • This reply has been marked as private.
  • Hi @acf-support ,

    Yes I put the json files in the acf-json folder in my theme folder and then choose to sync the field groups.

    I then deleted the json files and tried to import them through ACF Tools / Import Field Groups. This time everything works as it´s supposed to. The field group is of type clone and the values are fetched the way they should.

    I´ll test this a bit further tomorrow when I´m at work.

    Thanks for your help so far.

  • Hi @liss ,

    You are most welcome.

    Kindly let me know how it goes.

  • Hi @acf-support ,

    I´ve been testing some more and tried recreating all field groups, manually from scratch on a fresh install.

    • WP 4.6.1
    • ACF PRO 5.4.4
    • No other plugins

    The same error appears here too.

    The contents of the field global_quotes is set through the options page Theme settings.

    The error only seems to appear if there are values in the global_quotes field

    The filter for acf/load_field which populates the global_quotes_select:

    add_filter( 'acf/load_field/name=global_quotes_select', function( $field ) {
    
    	$global_quotes = get_field( 'global_quotes', 'option' );
    
    	if ( ! $global_quotes ) {
    		return $field;
    	}
    
    	foreach ( $global_quotes as $i => $quote ) {
    		$field['choices'][ $i ] = $quote['quote'];
    	}
    
    	return $field;
    
    });

    The Options page for Theme Settings:

    if ( function_exists( 'acf_add_options_page' ) ) {
    	acf_add_options_page([
    		'page_title' => __( 'Theme Settings', 'wptest' ),
    		'menu_title' => __( 'Theme Settings', 'wptest' ),
    		'menu_slug' => 'wptest_options',
    		'capability' => 'manage_options',
    		'redirect' => false,
    		'icon_url' => 'dashicons-welcome-widgets-menus',
    		'position' => 25,
    	]);
    }

    I´ve attached all three json-files needed to replicate the error. Could You please try and create everything manually the way I did?

    Thanks in advance.

  • I think that this topic is related to this one https://support.advancedcustomfields.com/forums/topic/fetching-value-from-cloned-field-in-acfload_field-filter/ and I have reported the sync issue with clone fields to the developer.

  • Hi @hube2

    It seems you linked to this topic.

    Could you please check that?

    Thanks.

  • It wasn’t a mistake. The other topic turns out to be a bug in syncing field groups with clone fields. Your problem happens when you sync a field groups with clone fields. The issues are related in some way and I have reported the fact that there is an issue with syncing field groups that have clone fields to the developer.

  • Hi guys

    Elliot here – ACF dev.

    Just want you to know that I’ve got this issue on my to-do for 5.4.7

    Thanks for your patience!’

    Cheers
    E

  • Hi guys

    Good news. I’ve started testing, debugging and believe to have found and fixed the issue!

    Can you please edit the file ‘admin/field-groups.php’ and replace the function ‘check_sync’ with the following: https://gist.github.com/elliotcondon/ebac045330753710e4ce379d036a2185

    This should solve the issue of a sub clone field replacing itself with selected fields during sync!

    Thanks
    Elliot

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

The topic ‘Fetching value from cloned field in acf/load_field filter’ is closed to new replies.