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

Query returns NULL in CP preview #384

Open
klick opened this issue Feb 5, 2024 · 4 comments
Open

Query returns NULL in CP preview #384

klick opened this issue Feb 5, 2024 · 4 comments

Comments

@klick
Copy link

klick commented Feb 5, 2024

Describe the bug

In my templates I am determining the current node id this way:

{% set currentNavNodeId = craft.navigation.getActiveNode({ handle: 'mainMenu' }).id %}

which works fine so far. The template breaks though in the CP's preview panel as well as the parameterised preview where craft adds info to the url (aka ?x-craft-preview=E8eOWX…) after applying any change to the entries content.

Steps to reproduce

  1. Editing Entry in the CP
  2. Click in-built “Preview” or “View → Primary entry page”
  3. Twig Runtime Error : Impossible to access an attribute ("id") on a null variable.

Craft CMS version

4.7.1

Plugin version

2.0.25

Multi-site?

Yes

Additional context

The navigation elements are entries.

@klick klick changed the title Query returns null in preview Query returns NULL in CP preview Feb 5, 2024
@engram-design
Copy link
Member

That would be correct behaviour if there is no active node. You probably shouldn't assume that craft.navigation.getActiveNode({ handle: 'mainMenu' }) always returns something, hence the error message that it's trying to get id from null. The two possible values from getActiveNode() are null or a NodeElement object.

I'd adjust your templates to handle this with a conditional, or a null coalescing operator.

{% set currentNavNodeId = craft.navigation.getActiveNode({ handle: 'mainMenu' }).id ?? null %}

@klick
Copy link
Author

klick commented Feb 6, 2024

Thank you for replying.

I could do that but would not be able to correctly render a preview. Why is there suddenly no NodeElement when in preview mode i wonder.

@engram-design
Copy link
Member

I do mean that you should be doing that anyway, as you might be on a page that doesn't have an active node, and you'll run into this error sooner or later.

As for why it doesn't work in preview, I'd say it's likely because we don't handle it, and we expect it to be a front-end request. I'll have a look into adding support for that.

@klick
Copy link
Author

klick commented Feb 8, 2024

For the time being I have now resorted to hard code the ID into the templates → {% set currentNavNodeId = 10987 %}.
Wich is not preferred as you can imagine but works as expected. I’ll watch this place for changes.

Thanks.

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

No branches or pull requests

2 participants