Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ACF field non translatable #1050

Open
simon418 opened this issue Aug 4, 2021 · 3 comments
Open

Make ACF field non translatable #1050

simon418 opened this issue Aug 4, 2021 · 3 comments
Labels
enhancement New feature or request module: ACF Integration with ACF

Comments

@simon418
Copy link

simon418 commented Aug 4, 2021

Hi, i am using qtranslate xt and Advanced Custom fields. In my post i have a field called "Year" for which i want to disable the translation, so the value will stay when i switch the language.
In the Custom config of the plugin i can only put in IDs or Classes that i wont to be translatable, but since all fields are already translatable, i want the opposite.
Been searching for way way longer than i thought for an answer, there must be an easy solution for it i hope.
Any help is appreciated!

@MK-RD
Copy link

MK-RD commented Aug 5, 2021

I can't understand that either, because it should be possible to omit the translation function for certain elements with a CSS class like qtx-notranslate.

Certain fields such as the year of birth do not require translations or unnecessary workarounds!

@herrvigg herrvigg added the module: ACF Integration with ACF label Aug 5, 2021
@MK-RD
Copy link

MK-RD commented Aug 10, 2021

@simon418
Workaround: For urgent cases only!

Field Type
Text (qTranslate)

CSS Class (qtx-notranslate)
grafik

File (Class)
... /plugins/qtranslate-xt/modules/acf/src/acf_5/fields/text.php

Method
render_field($field)

Code

/**
 * Hook/override ACF render_field to create the HTML interface
 *
 * @param array $field
 */
function render_field( $field ) {
    global $q_config;
    $languages       = qtranxf_getSortedLanguages( true );
    $values          = $this->plugin->decode_language_values( $field['value'] );
    $currentLanguage = $this->plugin->get_active_language();

    $atts = array();

    $keys = array( 'type', 'id', 'class', 'name', 'value', 'placeholder' );
    if ( $field['maxlength'] !== "" ) {
        $keys[] = 'maxlength';
    }
    foreach ( $keys as $k ) {
        $atts[ $k ] = $field[ $k ];
    }

    $special_keys = array( 'readonly', 'disabled' );
    foreach ( $special_keys as $k ) {
        if ( isset( $field[ $k ] ) && $field[ $k ] ) {
            $atts[ $k ] = $k;
        }
    }

    /*! ADDED START */           
    if (str_contains($field['wrapper']['class'] ?? '', 'qtx-notranslate')) {
        $atts['class']         = $field['class'];
        $atts['class']         .= ' current-language';
        $atts['type']          = 'text';
        $atts['name']          = $field['name'] ."[$language]";
        $atts['value']         = $values[ $currentLanguage ];
        $atts['data-language'] = $currentLanguage;
        echo '<div class="acf-input-wrap">';
        echo '<input '. acf_esc_attrs($atts) .' />';
        echo '</div>';
        return;
    }
    /*! ADDED END */

    echo '<div class="acf-input-wrap multi-language-field">';

    foreach ( $languages as $language ) {
        $class = ( $language === $currentLanguage ) ? 'wp-switch-editor current-language' : 'wp-switch-editor';
        echo '<a class="' . $class . '" data-language="' . $language . '">' . $q_config['language_name'][ $language ] . '</a>';
    }

    foreach ( $languages as $language ) {
        $atts['class'] = $field['class'];
        if ( $language === $currentLanguage ) {
            $atts['class'] .= ' current-language';
        }
        $atts['type']          = 'text';
        $atts['name']          = $field['name'] . "[$language]";
        $atts['value']         = $values[ $language ];
        $atts['data-language'] = $language;
        echo '<input ' . acf_esc_attrs( $atts ) . ' />';
    }

    echo '</div>';
}

Of course, I wouldn't finally implement this like that!

@herrvigg herrvigg added the enhancement New feature or request label Jun 11, 2022
@herrvigg
Copy link
Collaborator

Ideally we should have a property in each field settings to opt-out individually.

For now with #1279 there are more settings to select which of the standard fields and sub-groups are enabled but these are still global settings. Same for extended qTranslate fields #1300.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request module: ACF Integration with ACF
Projects
None yet
Development

No branches or pull requests

3 participants