Support

Account

Home Forums Add-ons Repeater Field Repeater field value in acf/update_value filter

Solved

Repeater field value in acf/update_value filter

  • Hi!

    I have a problem with a repeater field.
    My problem is almost the same as this one: https://support.advancedcustomfields.com/forums/topic/repeater-on-options-page-returns-count/
    I had this problem on front-end and found that I was creating the options page after the init but that I was asking for the value of my field before that. Moving them fixed the problem.
    But I have the same problem in back-end, but can’t find how to fix it…

    I have this filter:

    add_filter( ‘acf/update_value/name=myfield’, ‘sync_acf_field_language’, 10, 3 );

    if my function:
    function sync_acf_field_language( $fieldValue, $field, $post_id ) {
    die(var_dump($fieldValue));
    }

    it returns “1” (the number of rows)… Works fine on regular fields, but not on repeaters…

    Maybe that the acf/update_value filter is called before the loading of my plugin and there’s no way I can change this… I create the options page as soon as the plugin is included, it’s not attached to any filters.
    On front-end I know that I could get the values with a “while(have_rows)” loop, but in back-end I need the whole thing in an array. Any way to get the filter acf/update_value to be triggered later or to fix the bug that returns only the number ?

    Any ideas ?

    Thanks!

  • Unfortunately, what you’re seeing is correct. This hook happens just before the value of the field is stored in the DB and the actual value of the repeater field stored is the number of rows.

    What is it that you’re looking to do when the value is updated? There may be another way to accomplish it.

  • Thanks for your answer!!

    What I need is to sync the data of an option page between multiple language, using WPML.
    – In a plugin
    – Option page created via php by the plugin (acf_add_options_page, acf_add_local_field,etc)
    – When the user save the form in any language in the admin, I need the same data to be in the form when the user switch language. These are settings shared for all languages.

    Best approach I found was to watch for the save and then resave the data in all languages. Maybe there’s a better way…

    If not, here’s my code to do it. It works fine for all the fields, except one repeater)

    $henri_fields_to_sync = array(
    		"henri_google_analytics",
    		"henri_remove_accent",
    		"henri_disable_update",
    		"henri_password_protect",
    		"henri_social_icons_group",
    	);
    	if (  isset( $_SESSION['is_saving_acf_henri'] ) ) {
    		unset( $_SESSION['is_saving_acf_henri'] );
    	}
    	/** Sync acf_field between language */
    	function henri_sync_acf_field_language( $fieldValue, $field, $post_id ) {
    		if ( ! isset( $_SESSION ) ) {
    			session_start();
    		}
    		global $sitepress;
    		if ( !isset( $sitepress ) ) {
    			return;
    		}
    		if ( ! isset( $_SESSION['is_saving_acf_henri'] ) ) {
    			$_SESSION['is_saving_acf_henri'] = true;
    			$activeLang          = $sitepress->get_ls_languages();
    			foreach ( $activeLang as $key => $value ) {
    				if ( $key !== ICL_LANGUAGE_CODE ) {
    					set_global_option_icl( $post_id['name'], $fieldValue, $key );
    				}
    			}
    			unset( $_SESSION['is_saving_acf_henri'] );
    		}
    		return $fieldValue;
    	}
    
    	foreach ( $henri_fields_to_sync as $henri_field_to_sync ):
    		add_filter( 'acf/update_value/name=' . $henri_field_to_sync, 'henri_sync_acf_field_language', 10, 3 );
    	endforeach;

    Thanks again for your time!

    stef

  • I think that the best way to do this would be to use the acf/save_post filter https://www.advancedcustomfields.com/resources/acf-save_post/ with a priority < 10. This will run before ACF saves the values. You can also run it with a priority > 10 because either way you need to look at $_POST to see what fields are being updated.

    The main difference is that you’ll need to look for field keys not field names in the $_POST[‘acf’] array.

  • If works!!! 🙂
    Thank you very much! Here’s my final code in case it interests someone:

    
    <?php
    
    /**
     * Class Henri_ACF_SyncOptions
     *
     * This class sync the selected options fields between all active languages.
     *
     */
    class Henri_ACF_SyncOptions {
    
    	private static $inited = false;
    
    	/**
    	 * @var array
    	 * List fields to update
    	 */
    	private static $henri_fields_to_sync = array(
    		"field_group_henri_settings_general_analytics_henri_google_analytics",
    		"field_group_henri_settings_general_medias_henri_remove_accent",
    		"field_group_henri_settings_general_access_disable_update",
    		"field_group_henri_settings_general_access_password_protect",
    		"field_group_henri_settings_general_social_henri_social_icons_group",
    	);
    
    	public static function init() {
    		if ( self::$inited ) {
    			return;
    		}
    		self::$inited = true;
    		add_action( 'acf/save_post', array( 'Henri_ACF_SyncOptions', 'onPostSave' ), 10 );
    	}
    
    	public static function onPostSave() {
    		global $sitepress;
    		if ( ! isset( $sitepress ) ) {
    			return;
    		}
    		// array of field values
    		$postFields  = $_POST['acf'];
    		$activeLangs = $sitepress->get_ls_languages();
    		foreach ( self::$henri_fields_to_sync as $fieldToSync ):
    			if ( isset( $postFields[ $fieldToSync ] ) ):
    				foreach ( $activeLangs as $key => $value ) {
    					if ( $key !== ICL_LANGUAGE_CODE ) {
    						set_global_option_icl( $fieldToSync, $postFields[ $fieldToSync ], $key );
    					}
    				}
    			endif;
    		endforeach;
    	}
    
    }
    if(is_admin()):
    	Henri_ACF_SyncOptions::init();
    endif;
    
  • Thanks for your help.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.