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

[BUG] MenuItem current property is always false in global context. #3000

Closed
xanghyr opened this issue May 14, 2024 · 4 comments
Closed

[BUG] MenuItem current property is always false in global context. #3000

xanghyr opened this issue May 14, 2024 · 4 comments
Labels
needs-info Need more info on this issue in order to properly investigate

Comments

@xanghyr
Copy link

xanghyr commented May 14, 2024

Expected Behavior

Should setup current current_item_parent current_item_ancestor with the good values.

Actual behavior

MenuItem properties : current current_item_parent current_item_ancestor are always false.

Steps to reproduce behavior

  1. Copy & paste the code from the documentation in the functions.php
// Example: Add a menu to the global context.
add_filter('timber/context', function ($context) {
    $context['menu'] = Timber::get_menu('primary-menu');

    return $context;
});
  1. {{ dump(menu) }} in your template. Search for current property inside MenuItem.
  2. Always false.

Notes

It felt weird as I don't remember having this bug in my previous WordPress/Timber, though it's my first time using Timber 2.x. So I did some research to find the origin.

I found that for the 3 MenuItem properties $current $current_item_parent $current_item_ancestor they use the native WordPress function _wp_menu_item_classes_by_context().
This function is using the global $wp_query; to setup those properties.

And I found that in the filter timber/context (add_filter( 'timber/context', array( $this, 'add_to_context' ) );) the global $wp_query; is not already set. This global is only set later, available with add_action( 'wp', array( $this, 'test_global_wp_query' ) );. But at this point you can't add variable to the global context, at least not that I know of.

When I do $context['header_menu'] = Timber::get_menu('header_menu'); inside a template, just before Timber::render() the properties current, etc. are all good. But I don't feel like it would a good solution to use some kind of function in every template I have to get the menus at this moment.

I think that menus are meant to be inside the global context, if that so, any idea on how to fix this ?

Thank you !

What version of Timber are you using?

2.1.0

What version of WordPress are you using?

6.5.3

What version of PHP are you using?

8.2

How did you install Timber?

Installed or updated Timber through Composer

@Levdbas
Copy link
Member

Levdbas commented May 15, 2024

Hi @xanghyr , welcome at Timber! I just recreated your issue on Timber 2.2 (just released) but I don't run into this issue.

  1. created a page
  2. Createad a menu named primary-menu
  3. fetched that menu in the context
  4. navigated to that page, dumped the menu
  5. current is true and I do have classes

Could you try debugging this further? Maybe create another menu with other menu items and query that menu?

@Levdbas Levdbas added the needs-info Need more info on this issue in order to properly investigate label May 15, 2024
@xanghyr
Copy link
Author

xanghyr commented May 17, 2024

Hello, thank you !

So I created a new clean & simple project from my boilerplate to test menus, and it works great... I don't know if it's a good news or a bad news ^^
It means there's something on my project that broke it, but for the moment I have no idea, I did nothing around menus, so...

Thank you anyway, i'll debug this further and come back here if I find anything interesting.

@Levdbas
Copy link
Member

Levdbas commented May 17, 2024

Thank you for your feedback @xanghyr, let me know if you find something interesting. Maybe try to change the name of menu names or menu locations and see if that helps! I'll close this issue for now but feel free to respond to this if you find something.

@Levdbas Levdbas closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2024
@xanghyr
Copy link
Author

xanghyr commented May 28, 2024

Hi, I'm back after I've been able to spend some time to fix my issue.

So the thing was that I had a feature required in add_action( 'init', array( $this, 'start_theme' ) );. And inside that file I used $context = Timber::context(); to work with stuff I load with add_filter( 'timber/context', array( $this, 'add_to_context' ) );.

So of course menus did not worked as it gets them before $wp_query is set inside init action.
Getting global context through different files in the project is convenient, but is a bad idea to do it too early :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-info Need more info on this issue in order to properly investigate
Projects
None yet
Development

No branches or pull requests

2 participants