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

get_field on term #32

Closed
danygit opened this issue Nov 21, 2018 · 11 comments
Closed

get_field on term #32

danygit opened this issue Nov 21, 2018 · 11 comments
Assignees
Labels

Comments

@danygit
Copy link

danygit commented Nov 21, 2018

Version information

  • Plugin version : 1.1.4
  • PHP : 7.2.10
  • WordPress :
  • ACF version : 5.7.7
  • Polylang version : 2.4

Steps to reproduce

  1. Add acf field in term
  2. do get_field('NAME_OF_FIELD'), WP_Term Term) on page.

What was expected

No error

What happened instead

( ! ) Catchable fatal error: Object of class WP_Term could not be converted to string in /var/www/html/web/wp-content/plugins/acf-options-for-polylang/classes/helpers.php on line 16

My opinion

  1. The problem come to
    add_filter( 'acf/validate_post_id', [ $this, 'set_options_id_lang' ], 10, 2 );

Filter is deprecated and dont work with acf term field.

@MaximeCulea MaximeCulea self-assigned this Nov 21, 2018
@MaximeCulea
Copy link
Contributor

Hello @danygit,

Thank you for the report. It seems something serious.

I will be watching this soon, then.

@Deithso
Copy link

Deithso commented Jan 10, 2019

Hello @MaximeCulea

First, Thank you for this amazing plugin.

i'm facing the same probleme

Catchable fatal error: Object of class WP_Term could not be converted to string in /var/www/clients/client1/web73/web/wp-content/plugins/acf-options-for-polylang-master/classes/helpers.php on line 16

@danygit have you fixed it ?

Thank you

My Configuration
Version information
Plugin version : 1.1.5
PHP : 7.2
WordPress : 4.9
ACF version : 5.7.7
Polylang version : 2.5

@danygit
Copy link
Author

danygit commented Jan 10, 2019

No i dont use this plugin. I do it myself with my own code.

@Deithso
Copy link

Deithso commented Jan 10, 2019

No i dont use this plugin. I do it myself with my own code.

Okey Thank you, do you have a hint how you did it ? i'm try to fix it in the plugin, otherwise i'll do in with my own code

@danygit
Copy link
Author

danygit commented Jan 10, 2019

You add acf field in option page with argument variable language in key

            $currentLanguage = $this->getCurrentLanguage();
            $language        = $nameAdd = '';
            if ( $currentLanguage !== static::DEFAULT_LANGUAGE ) {
                $language = $currentLanguage;
                $nameAdd  = '-' . $currentLanguage;
            }

            acf_add_local_field_group( array(
                'key'                   => 'group_5ba4e61953f92' . $language,
                'title'                 => __( 'Options', 'DOMAIN' ),
                'fields'                => array(
                    array(
                        'key'               => 'field_5ba4e6612dfb5' . $language,
                        'label'             => __( 'Footer', 'DOMAIN' ),
                        'name'              => 'gp-schedules' . $nameAdd,

And to retrieve the option field = get_field('toto', 'options');

 add_action( 'acf/load_value', [ $this, 'loadAcfValue' ], 102, 3 );
    }

    public function loadAcfValue( $value, $post_id, $field ) {
        if ( $post_id === 'options' ) {
            $currentLanguage = $this->getCurrentLanguage();
            if (
                $currentLanguage !== static::DEFAULT_LANGUAGE &&
                ! empty( $field['name'] ) &&
                ! in_array( substr( $field['name'], - 2 ), $this->languageList ) &&
                (
                    ! isset( $field['polylang'] ) || $field['polylang'] === true
                )
            ) {
                $valueInOtherLanguage = get_field( $field['name'] . '-' . $currentLanguage, $post_id );

                return $valueInOtherLanguage;
            }
        }

        return $value;
    }

@Deithso
Copy link

Deithso commented Jan 10, 2019

@danygit

Thank you for the fast reply,

i found the source of the bug, it happens only when i get_field from a term

for exemple i use this code to get a field (link) added to woocommerce product category
$linkedPage = get_field('category_linked_to',$category);

according to ACF docs there are 3 ways to get a field from a taxonomy im using the 3rd

The bug occures when calling get_field on a taxonomy, i'm passing the object WP_Term as the second param. the function that throws the exceptipn can't handle the object because it's expecting a string.

To fix this use the 1st or 2nd way to retrieve the field

Another fix i'm using currently as i can't fix erros one by one is to add a test and the function becomes

Edit the file

wp-content/plugins/acf-options-for-polylang-master/classes/helpers.php on line 16

public static function original_option_id( $post_id ) {
    if(is_a($post_id, 'WP_Term')){
        $post_id = $post_id->taxonomy.'_'.$post_id->term_id;
    }
    return str_replace( sprintf( '_%s', pll_current_language( 'locale' ) ), '', $post_id );
}

@MaximeCulea
Copy link
Contributor

Wow, thank you guys for the hint! And sorry for the delay (because of holidays).
I should seriously need to digg into this soon ..

@Deithso
Copy link

Deithso commented Jan 10, 2019

Wow, thank you guys for the hint! And sorry for the delay (because of holidays).
I should seriously need to digg into this soon ..

Anytime @MaximeCulea

Merci de nous dire une fois le fix est en place que je puisse mettre à jour le plugin.

Merci & bonnes vacances.

@Jaska
Copy link

Jaska commented Mar 16, 2019

Thanks for the fix @Deithso. I'm hoping @MaximeCulea can import the fix into the plugin soon.

@MaximeCulea
Copy link
Contributor

MaximeCulea commented Mar 19, 2019

This is related to #40

The team will be fixing all cases today #44

I let you know when the package is ready. Thank you for your patience.

@MaximeCulea
Copy link
Contributor

Hi @Deithso, @Jaska, @danygit,

It has been fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants