-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Preserve the navigation on theme switch #38291
Comments
I think tackling this is the key part. The only feedback here is that I recall it was mentioned that the slug should be based on the template part area. Those areas typically use semantic names The secondary part will be considering how that works for classic themes that switch to a block theme. Another thing that would be on my mind is how WordPress will determine which menu should be surfaced for site navigation - #36667. Will it be via the slug? The two issues might have a dependency. |
@adamziel a generic convention-based slug (primary, secondary, etc) was not the result from previous discussions; as @talldan points out, this is tied to the removal of the need to name a menu upon creation because we have enough context to create a meaningful semantic slug from it. There's an implied hierarchy of fallbacks to use on switches that should take into account the template and template part a menu is placed in. I don't recall if there was an issue capturing that. |
@mtias I just updated the description.
AFAIR #36760 is the closest discussion. CC @draganescu @tellthemachines in case they remember something I don't. |
More prior art:
|
@getdave @adamziel @draganescu and I spent some time thinking about this today. We came up with this suggestion: Expected behaviour
Questions
Technical detailsIn order to save the previous wp_navigation post in the new theme we need to either:
cc @mtias |
Themes cannot know what navigation structure a site has. We need to allow themes to be opinionated for cases where they want to have a very specific layout (where they'll basically use placeholder individual link-items) but otherwise defer to what exists on the site and the user expectation of how it should work. The latter is probably close to 99% of the cases we have. The situation of page-listRight now themes are using page-list to have a fallback, not because they explicitly want page lists to show instead of the user menu. This makes it difficult to disambiguate a genuine use of page-list (rare) from just a fallback use (most common). That also means changing behaviour can be rough because a user might have become accustomed to it showing all their pages even if they have an old menu somewhere. So without changing how things work today:
Technical detailsNot sure I follow item 2. above. Why do we need to bother about the previous block theme? We should only look at |
@mtias item 2 is essentially doing the matching in a hook that gets executed during the theme switch. It alleviates the problem of preserving a meaningful slug throughout operations such as dragging a menu to a different template, choosing a different wp_navigation post for a menu that already has a slug etc. It comes with it's own problems, though. |
My guess is because the slugs haven't been implemented yet. I think the block is still using ids. It does have the semantic title that could be used (slugified) until proper slugs are implemented down the line. |
If we don't have semantic slugs for whatever reason we should just fallback to the next heuristic (I think we mentioned something like if only one It's also worth keeping in mind slugs can be changed, so they can be an aid but we cannot rely on their presence all the time. |
Thank you for providing some additional clarity here @mtias. There is a lot of discussion around the behaviour of the block at the moment, but I believe some solid planning at this stage will save as many hours of development time down the line so it's good to see the dialogue here. Quick plea for fixing what we haveI would like to suggest that before we start implementing any of the more advanced "auto picking" of menus ideas, we should look to get what we have now working correctly. As I see it that means making sure always have parity between what we see in the editor and what we have on the front end. This is inline with what you suggest above. To that end I have a question about the implementation details around this goal. The GoalMy understanding from what you communicated above, is that what we want to happen is for both editor and front end (i.e. PHP) to abide by the following rules:
What happens for Editor fallbacks?We can update the implementation to follow these rules. However, what happens in the case of the editor fallbacks when we get to the step of rendering a list of pages? Currently on
...then block attempts to pick the first
However if we want parity with the front end then the editor would have to render a list of pages as it's placeholder. After all this is what the front end would do. However in the editor we must still provide the user with choices. They must see the options to Therefore we're going to have to design/create a new placeholder state which is a hybrid of the current one and the page list block. Is that what you are envisaging? For what it's worth I don't think we can simply auto insert an uncontrolled ExampleFor clarity, here is an example of how the editor and the front end are currently out of sync. The editor shows a placeholder and the frontend shows a list of Pages. Screen.Capture.on.2022-06-30.at.20-04-37.mp4 |
Must we?
... so what is a placeholder for? If pages are useless delete them and you get to a placeholder, which you created and nothing rendered on the front end. I think this state is missing in terms of sync:
|
I think at until we solve the auto-assignment of menus (including classic menus) we need to allow users to choose an existing menu. Start from Empty should still be an option but perhaps it can be made less obvious because it's a 20% use case. I'd still like to see a revised design for a new Page List based placeholder state. Perhaps @javierarce can help? |
After chatting with @getdave and wrapping my head around this issue, I agree with @draganescu when he says that users have all they need to update one of these menus. If we can render a valid menu both on the front and the editor, then we don't need a special placeholder state. We only need one when users start from scratch or if we can't fall back to a valid menu. |
Also related #42600 |
I've been looking into this some more and here's what I've learnt about how I think this will need to be implemented. The ProblemCurrently the Nav block has a However IDs will change across different WP installations and thus using a post ID as the The SolutionWe need to use something to reference a One way to do this is to mirror what Template Parts does and use a string-based gutenberg/packages/block-library/src/template-part/block.json Lines 10 to 12 in a1256e1
We can then use this slug to look up the correct For example the following hierarchy:
...would result in the following slug: gutenberg/packages/block-library/src/template-part/edit/index.js Lines 60 to 64 in a1256e1
Data Fetching requires Post IDsMany of Gutenberg's Core Data fetching utilities are optimised to expect numerical Post IDs. For example the signature of
If we drop the postId from the Navigation block in favour of a unique getEntityRecord('postType', 'wp_navigation', { slug: 'header-header-dark-small' }); ...however this will not work as the function does not accept a query (you must supply a There is getEntityRecord('postType', 'wp_navigation', { slug: 'header-header-dark-small' }); We may be able to work around this by altering the Navigation REST API endpoint to handle string-based IDs instead of numeric Post IDs. This concept is borrowed from the Template Part REST API endpoint which has special handling for just such a use case. Here templates can be requested by passing a string-based slug as the getEntityRecord('postType', 'wp_template_part', 'twentytwentytwo//header') You can try this in your devtools console by doing (replace the template part slug accordingly):
Therefore for Navigation we could have a similar handling on the REST API to allow us to pass a string-based slug For example: // does not work - yet!
getEntityRecord('postType', 'wp_navigation', 'header-header-dark-small'); ...would result in the REST API querying for Hierarchical Navigation Post Lookups based on Template Part SlugWe also need a way to look up the most suitable Consider Theme A has a Nav block within the following template part hierarchy:
Theme B might have a different template hierarchy with only a single
On Theme switch the following query will be issued. This is because Theme B's Navigation block will have no
However the API should be intelligent enough to know that the slug This mechanic (or something similar/better) would help to ensure that the most suitable Navigation post is returned for Navigation blocks blocks in similar template part hierarchies when switching Themes. This has been partially explored in #36522. Notes / Questions
|
Why do you think this is inelegant? |
We should use the most immediate template, like
Yes, that's fine.
We should still be passing the entire context to focused mode when we have it since it's useful to display in different places. It's also important to note that the idea of using the template hierarchy to set default names is also to aid in loading the right menu in the first place and not rely on things like "primary" — if there's a header menu we should try to prioritize it for any header area, etc. |
At all? We should create an issue for this then. |
This sounds like a good approach. Alternatively we could just pull them all down and search though them, but it would be nicer to do it on the server. |
It's not very important right now, but we have said theme building blocks should not be exposed in post-editor / post-content before, or at least not be very prominent. |
What's the status of this after the updates to what we load in the placeholder? |
State of preserving Navigation on Theme switch for WP 6.1The current status as I understand it (cc @draganescu and @scruffian for confidence check) is:
For an 80% use case of a user who has a single Nav in the header and has a single Menu (or even a few different menus) the Theme switch experience is going to feel a lot more "seamless". In this case the block will pick the most recent Menu and display that in the block. Where this is less optimal is for the 20% use cases where the user might have several Navigations and several Menus. In this cases it may well be that the Theme switch experience will require some manual "correction" by the user following the switch. What we did not land in 6.1 is the ability to reference Navigation Menus by slug. This was intentionally deferred to 6.2 in order to allow more time for testing and refinement (especially as the feature offers little in the way of user experience when taken in isolation). This feature does however, unlock the door to allowing a more sophisticated theme switch experience, whereby the Navigation block will look to "intelligently" determine which Menu is most appropriate to use in the block (based on an as yet undetermined heuristic relating to template part hierarchy). I see this as a key feature of the block and so it's important we take the time to get it right. I hope that helps? |
Thanks for the overview, @getdave. Looks like it's in a much better place. What happens if I have a single classic menu and no new menus and switch to a block theme? Would it use page list or automatically import the classic menu? |
Currently it would use page list. |
That's a bummer, can we address the simplest case where it's 1 classic menu and no block menus so that it uses what exists? This should be much higher priority than sorting out the slug hierarchy. I'd also go as far as saying 1 menu || "primary" menu from classic should be used if you don't have block menus yet. |
I agree. I believe @draganescu is already looking into it. |
Here's an idea: #44173 |
Should we close this and keep remaining efforts on improving the slug allocation and resolvers? |
#44173 solved the last part of this which is in the reduced scope here. Slug management is a larger scope. |
Switching from Twenty Twenty-Two to another block theme resets the navigation.
Technically, it's because the navigation data is stored inside the template parts, for example:
The next theme is not aware of any customizations made and comes with its own presets. One theme I saw was preset to render a classic menu:
A different approach would be to infer the location/slug from the surrounding templates / template parts and use that to infer the new localization for the menu.
Prior discussions:
When this is implemented we should also consider the implications of #42600
The text was updated successfully, but these errors were encountered: