Retrieve Plone's Navigation Tree Using REST API

Posted by Fulvio Casali at Apr 13, 2018 11:46 AM |

Plone has a beautiful RESTful Hypermedia API, but its @navigation endpoint (see docs) is a bit too simplistic. As evidenced by the fact that the vast majority of websites out there have one form or another of dropdowns in their main navigation menus, going beyond the top level menu is almost non-negotiable. But @navigation does not offer this ability. What is one to do?

Well, after opening an issue in github, I decided to create a separate add-on as a proof of concept:

collective.restapi.navigationtree - PyPI

Tests are included to make sure it runs on Plone 4.3.latest, 5.0 and 5.1, for both Archetypes and Dexterity.

By default, Plone does not provide dropdown navigation menus. But pretty much every Plone site I have ever worked on has webcouturier.dropdownmenu installed to fill this gap. So I borrowed some of its code to generate the JSON response, and introduced a new endpoint called @navigationtree.

Currently, collective.restapi.navigationtree depends on webcouturier.dropdownmenu (as well as plone.restapi, of course), but my assumption was that if you need the former, you probably already have the latter installed. I also lean on webcouturier.dropdownmenu's configuration, in particular its dropdown_depth parameter. So you will get the same depth of your navigation tree in the JSON response as the site's menu. However, I'm already rethinking this dependency. It would be much cleaner to just add a query parameter to the endpoint to specify the desired tree depth than to rely on an external add-on's configuration. At some point I will release a new version with the dependency on webcouturier.dropdownmenu stripped out.

Example

request

Response:

response