Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: ,

This topic contains 1 reply, has 2 voices.

Last updated by Waqas Bin Hasan 1 year, 7 months ago.

Assisted by: Waqas Bin Hasan.

Author Posts
June 23, 2023 at 7:34 am #13882201

patrickA-12

Problem with WPML ACF Multilingual when the ACF select field is dynamically populated with a multidimensional array

June 23, 2023 at 7:59 am #13882399

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Reference to our chat, I've reported this ticket as well.

Meanwhile, as you noted in the other thread (https://wpml.org/forums/topic/acf-select-optgroup-causes-fatal-error/), please use the workaround mentioned by our devs:

Edit classes/Strings/Traversable/Field.php and replace

protected function transform( Transformer $transformer, $value, $config ) {
		if ( is_array( $value ) ) {
			foreach ( $value as $key => $label ) {
				$value[ $key ] = $transformer->transform( $label, $config );
			}
		} else {
			$value = $transformer->transform( $value, $config );
		}

		return $value;
	}

with

protected function transform( Transformer $transformer, $value, $config ) {
		if ( is_array( $value ) ) {
			foreach ( $value as $key => $label ) {
        if ( is_string( $label ) ) {
    				$value[ $key ] = $transformer->transform( $label, $config );
        }
			}
		} else {
			$value = $transformer->transform( $value, $config );
		}

		return $value;
	}

And if you want to translate the strings, you need to update the code in the functions.php file to

add_filter('acf/load_field/key=field_6452d0242b965',function($field) {
	$field['choices'] = array(
		'group1'=>array('val1'=> __( 'Value 1', 'my_plugin' ), 'val2'=> __( 'Value 2', 'my_plugin' )),
		'group2'=>array('val3'=> __( 'Value 3', 'my_plugin' ), 'val4'=> __( 'Value 4', 'my_plugin' )),
	);
	return $field;
});

Regards.