Support

Account

Home Forums General Issues Overwrite value of group-field itself (using one of it's sub-fields)

Solved

Overwrite value of group-field itself (using one of it's sub-fields)

    • Rasso

    • January 4, 2021 at 5:10 am

    Hi there!

    I seem to have trouble posting my question on the forums, so here it is again, as a support ticket:

    I have a use case where I would like to save a selected sub-field’s value to the value of a group. Let’s say I have a group named multilingual_text where one sub-field is for English, the other one is for German. When updating the field, I would like the English value to also be saved in the value of the parent group. The default behavior is that ACF saves an empty string for the group’s value (class-acf-field-group.php, line 227):

    
    return '';
    

    Is there a way to overwrite this behavior? I don’t seem to be able to alter this with acf/update_value.

    Context: I am building a custom multilingual site. For some ACF field types (text, textarea, wysiwyg, …) I have a field setting multilingual of type true_false. When this setting is true, I am altering the field on acf/load_field so that it becomes a field of type group with sub fields for all my active languages, suffixed with the language code (e.g. multilingual_text_en, multilingual_text_de). On acf/format_value I return the active language’s sub-field. It works really great so far. The above enhancement would make it possible to keep the default language’s value even if my plugin would be disabled or the field was reset to is_multilingual => false.

  • No, you cannot change this.

    A group field does not actually have a value, it only has sub fields that have values.

    And this is not 100% correct. A group field is actually a repeater field that has exactly 1 row. In a repeater field the value of the repeater field in the DB is the number of rows that have been saved in the repeater. Since this number is not really needed for a group field acf set it to an empty value.

    My suggestion would be to have 2 fields that are conditional on the true/false field rather than convert the text field to a group field. For example a text field and a group field that contains text fields for each language.

    Then I would create an acf/save_post action. In my filter I would check the value of the true/false field and depending on how it was set I would copy the default language value from/to the other field so that if the toggle is switched the value is present.

    Another option besides a acf/save_post filter would be to add custom JavaScript that transfers the content on the default language field back and forth when the toggle is changed.

    • Rasso

    • January 4, 2021 at 5:53 am

    Thanks @hube2 for your quick answer! Actually, I was counting on you 😉

    With the field setting I meant a field setting for the acf-field-group-edit-screen:

    
    add_action("acf/render_field_settings/type=text", [$this, 'render_field_settings'], 9);
    

    The setting:

    
    /**
     * Render field settings for multilingual fields
     *
     * @param Array $field
     * @return void
     */
    public function render_field_settings( $field ) {
    
      acf_render_field_setting( $field, array(
        'label'         => __('Multilingual?'),
        'instructions'	=> '',
        'name'          => 'acfml_multilingual',
        'type'          => 'true_false',
        'ui'            => 1,
      ), false);
    
    }
    

    On the second try, it seems like I made it work. The solution was to hook into acf/update_value *after* the ACF hook (I used a priority of 12):

    
    add_filter("acf/format_value/type=group", [$this, 'format_multilingual_value'], 12, 3);
    

    My update_value-function:

    
    /**
     * Write the default language's $value to the group $value itself
     *
     * @param mixed $value
     * @param int $post_id
     * @param array $field
     * @return mixed
     */
    public function update_multilingual_value( $value, $post_id, $field ) {
      if( !$this->is_acfml_group($field) ) return $value;
      $default_language = acfml()->get_default_language();
      $value = get_field("{$field['name']}_$default_language", $post_id, false);
      return $value;
    }
    
    • Rasso

    • January 4, 2021 at 6:00 am

    Thanks @hube2 for your quick answer! Actually, I was counting on you 😉

    With the field setting I meant a field setting for the acf-field-group-edit-screen:

    
    add_action("acf/render_field_settings/type=text", [$this, 'render_field_settings'], 9);
    

    The setting:

    
    /**
     * Render field settings for multilingual fields
     *
     * @param Array $field
     * @return void
     */
    public function render_field_settings( $field ) {
    
      acf_render_field_setting( $field, array(
        'label'         => __('Multilingual?'),
        'instructions'	=> '',
        'name'          => 'acfml_multilingual',
        'type'          => 'true_false',
        'ui'            => 1,
      ), false);
    
    }
    

    On the second try, it seems like I made it work. The solution was to hook into acf/update_value *after* the ACF hook (I used a priority of 12):

    
    add_filter("acf/format_value/type=group", [$this, 'format_multilingual_value'], 12, 3);
    

    My update_value-function:

    
    /**
     * Write the default language's $value to the group $value itself
     *
     * @param mixed $value
     * @param int $post_id
     * @param array $field
     * @return mixed
     */
    public function update_multilingual_value( $value, $post_id, $field ) {
      if( !$this->is_acfml_group($field) ) return $value;
      $default_language = acfml()->get_default_language();
      $value = get_field("{$field['name']}_$default_language", $post_id, false);
      return $value;
    }
    
  • I guess, since ACF is not actually using the value on a group field that this can be done as the value stored in the DB does not really matter. But it is the only field that I can think of this working for.

    • Rasso

    • January 4, 2021 at 8:29 pm

    weird – after a quick edit on my reply, it disappeared from the thread, but when I try to post it again, the forum seems to detect a duplicate…

    
    ERROR: Duplicate reply detected; it looks as though you’ve already said that!
    
    • sjain

    • June 7, 2021 at 11:14 pm

    Hi,

    Mention to us what you are attempting to do in the German Language?

    My site utilizes ACF Pro in the back end.

    I have a gathering field with 3 subfields:

    1. A radio catch. which shouldn’t be deciphered. I just need to get to the catch esteem picked by the site’s manager. This worth is something very similar for any language.

    I set this present field’s Translation inclinations to duplicate

    2 and 3. Picture handle each. a similar picture can be utilized in any of the dialects, yet it’s Alt, Captions, and title tag ought to be deciphered (which btw, is the situation for all media in my site).

    Interpretation inclinations for these two picture fields: not certain what to utilize?

    Furthermore there’s simply the interpretation inclinations for the gathering field itself. Which confounds me, since I’m as of now being approached to set the inclinations for each of the subfields.

    When

    Is there any documentation that you are following?

    Your docs.

    My seetings are

    * Dashboard > WPML > Settings > Post Types Translation checked the ‘Translatable – use interpretation if accessible or fallback to default language’ for the Media (connection)’ post sort.

    * Dashboard > WPML > Settings > Media Translation > How to deal with media for new substance: checked ‘While transferring media to the Media library, make it accessible in all dialects’ and different ones are unchecked.

  • Hi,

    Mention to us what you are attempting to do in German Language?

    My site utilizes ACF Pro in the back end.

    I have a gathering field with 3 subfields:

    1. A radio catch. which shouldn’t be deciphered. I just need to get to the catch esteem picked by the site’s manager. This worth is something very similar for any language.

    I set this present field’s Translation inclinations to duplicate

    2 and 3. Picture handle each. a similar picture can be utilized in any of the dialects, yet it’s Alt, Captions, and title tag ought to be deciphered (which btw, is the situation for all media in my site).

    Interpretation inclinations for these two picture fields: not certain what to utilize?

    Furthermore there’s simply the interpretation inclinations for the gathering field itself. Which confounds me, since I’m as of now being approached to set the inclinations for each of the subfields.

    When

    Is there any documentation that you are following?

    Your docs.

    My seetings are

    * Dashboard > WPML > Settings > Post Types Translation checked the ‘Translatable – use interpretation if accessible or fallback to default language’ for the Media (connection)’ post sort.

    * Dashboard > WPML > Settings > Media Translation > How to deal with media for new substance: checked ‘While transferring media to the Media library, make it accessible in all dialects’ and different ones are unchecked.

  • Mention to us what you are attempting to do in German language?

    My site utilizes ACF Pro in the back end.

    I have a gathering field with 3 subfields:

    1. A radio catch. which shouldn’t be deciphered. I just need to get to the catch esteem picked by the site’s manager. This worth is something very similar for any language.

    I set this present field’s Translation inclinations to duplicate

    2 and 3. Picture handle each. a similar picture can be utilized in any of the dialects, yet it’s Alt, Captions, and title tag ought to be deciphered (which btw, is the situation for all media in my site).

    Interpretation inclinations for these two picture fields: not certain what to utilize?

    Furthermore there’s simply the interpretation inclinations for the gathering field itself. Which confounds me, since I’m as of now being approached to set the inclinations for each of the subfields.

    When

    Is there any documentation that you are following?

    Your docs.

    My seetings are

    * Dashboard > WPML > Settings > Post Types Translation checked the ‘Translatable – use interpretation if accessible or fallback to default language’ for the Media (connection)’ post sort.

    * Dashboard > WPML > Settings > Media Translation > How to deal with media for new substance: checked ‘While transferring media to the Media library, make it accessible in all dialects’ and different ones are unchecked.

Viewing 9 posts - 1 through 9 (of 9 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.