verbb / navigation

A Craft CMS plugin to create navigation menus for your site.
Other
90 stars 23 forks source link

tree() function does not have certain properties inherent to a node #298

Closed TomDeSmet closed 2 years ago

TomDeSmet commented 2 years ago

Description Every node has an active property but it isn't added to the array when getting the data from the tree() function. I tried debugging and noticed this in the Navs.php buildNavTree() function. In the forEach there, if you trace $node->active it returns true or false. Than there's this line: $nodeTree[$key] = $node->toArray(); If I trace $nodeTree[$key]["active"] it errors because the active property is not in the array. Why did it get removed?

The same with target for example. It would be so nice to have all those node properties inside that function! Because than we have everything straight away to build our nav in Vue, instead of needing to create a custom data object in twig or a PHP function.

Additional info

engram-design commented 2 years ago

Yeah, the problem is that toArray() only converts properties of a node element, not methods. For example, there's no active or target properties, but there's getActive() and getTarget(), which through PHP's magic __get() methods they can be callable by active or getActive().

We also need to return a collection of arrays here, because we're adding children which doesn't exist as a property.

Should be added for the next release. To get this early, change your verbb/navigation requirement in composer.json to:

"require": {
  "verbb/navigation": "dev-craft-4 as 2.0.0",
  "...": "..."
}

Then run composer update.

engram-design commented 2 years ago

Added in 2.0.1