Log1x / acf-composer

Compose ACF Fields, Blocks, Widgets, and Option Pages with ACF Builder on Sage 10.
https://github.com/Log1x/acf-composer
MIT License
425 stars 58 forks source link

Cache returns wrong translation for select choice #244

Open slackday opened 6 months ago

slackday commented 6 months ago

This might be complex but I would like to know more how acf:cache works so I can look for a solution. It seems the wrong value is returned for "choices" when page is translated using wpml.

I have defined the choices field with addSelect like this

            ->addSelect('gender', [
                'label' => 'Kön',
                'choices' => [
                    'mare' => _x('Mare', 'Gender select', 'sage'),
                    'stallion' => _x('Stallion', 'Gender select', 'sage'),
                    'gelding' => _x('Gelding', 'Gender select', 'sage'),
                ],
                'default_value' => 'stallion',
                'wpml_cf_preferences' => 1,
            ])

wpml_cf_preferences = 1 is so that the value of the select is copied to the translation.

I return the value in my template like this

        $gender = get_field('gender', $post_id);
        if (! empty($gender)) {
            $gender_object = get_field_object('gender');
            $gender = $gender_object['choices'][$gender] ?? $gender;
            $details['gender'] = [
                'label' => __('Gender', 'sage'),
                'value' => $gender,
            ];
        }

This works but if I change language on my site the untranslated value is returned.

If I run wp acorn acf:clear the correct value is returned. But running wp acorn acf:cache gives the wrong value again.

Log1x commented 6 months ago

The cache stuff converts everything to JSON here when writing the blocks from the Manifest here.

I'm sorry, but I do not use WordPress for any multi-lang projects so I'll need someone else to take lead when it comes to fixing issues like this. 🤗

slackday commented 6 months ago

Understood. And this is an edge case. Thanks for showing how caching works. I will have a look if this can be solved in a clean way. WPML uses a "glue" plugin called WPML ACF for handling fields in different languages. I know that when using this plugin and creating field and saving them as json in acf-json folder they are separated by a lang subfolder. Maybe something similar needs to happen here.