funkjedi / acf-qtranslate

Adds qTranslate compatible fields for Text, Text Area, Wysiwyg Editor and Image.
http://wordpress.org/plugins/acf-qtranslate/
57 stars 32 forks source link

acf-text-field maxlength check failing #132

Open flameoftheforest opened 5 years ago

flameoftheforest commented 5 years ago

Hi,

On publishing, the class-acf-field-text.php/acf-field-text::validate_field checks the $field['maxlength'].

It is not able to pre-filter the value through acf-qtranslate.

The acf-field-text also does not have a hook to interface with this.

Here's my workaround which I put in my theme/functions.php

add_filter('acf/validate_value', 'qtranslate_interfaced_validation', 10, 4);
function qtranslate_interfaced_validation($valid, $value, $field, $input) {
    // class-acf-field-text.php/acf-field-text::validate_field checks the $field['maxlength']
    // but is not able to pre-filter it's value through acf-qtranslate
    // The acf-field-text also does not have a hook to interface with this.
    //
    // Here's a workaround:
    // If $valid is 'Value must not exceed'-prefixed
    // and $field['maxlength'] exists, we can be rather certain tat acf-field-text::validate_field has
    // failed check.
    // Perform the translation extraction and redo the check again.
    //
    // ///////////////////////////////////////////////////////////////////////////////////////
    $prefix = 'Value must not exceed';
    if (!function_exists('qtranxf_getLanguage')) return $valid;
    if (!array_key_exists('maxlength', $field)) return $valid;
    if (!is_string($valid)) return $valid;
    if (!(substr($valid, 0, strlen($prefix)) === $prefix)) return $valid;
    $active_language = apply_filters('acf_qtranslate_get_active_language', qtrans_getLanguage());
    $value = explode("[:{$active_language}]", $value)[1];
    $value = explode("[:", $value)[0];
    if (strlen($value) > $field['maxlength']) {
        return sprintf( __('Value must not exceed %d characters', 'acf'), $field['maxlength'] );
    }
    return true;
}

It would be really great if you can address this in acf-qtranslate please.

Thanks! Bryan Hillsong International