parallelogram / craft-nav
Navigation menus for Craft CMS: drag-and-drop trees of links, with per-site titles, active states and tagged caching.
Package info
github.com/parallelogram-dev/parallelogram-craft-nav
Type:craft-plugin
pkg:composer/parallelogram/craft-nav
Requires
- php: ^8.3
- craftcms/cms: ^5.6
- parallelogram/craft-kit: ^0.2 || *@dev
Requires (Dev)
- carthage-software/mago: ^1.59
- phpunit/phpunit: ^12.0
Suggests
- parallelogram/craft-cache: Purges cached pages when a menu changes.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 02:04:13 UTC
README
Navigation menus for Craft CMS 5. Menus are defined in project config, so they deploy with the code. Their trees are edited per site, active states are worked out per page, and resolved trees are cached with tags so a full-page cache purges the pages that show a menu when anything in it changes.
Status: 0.1.0 in development. This release has menu definitions, tree storage, the resolver, active states, the Twig API, caching, tree export and import, the control panel tree editor, per-site overrides for shared menus, version history and dead-link badges. The Verbb Navigation importer lives in Parallelogram's private migrator.
Requirements
- PHP 8.3 or later
- Craft CMS 5.6 or later
Menus
Admins create menus with New menu in the editor's header, and change one with the cog beside its History button (where admin changes are allowed). Each menu has:
- Name and handle, the handle being what templates use.
- Sites: shared (one tree, with links resolved in each site) or independent (a separate tree per site).
- Levels: how deep the tree may go, or any depth.
- Kinds of node editors may add.
Menus are stored under pNav.menus in project config. Deleting a menu deletes its trees and their saved versions.
Nodes
| Kind | What it is |
|---|---|
| Element link | An entry, category, asset or any other element Craft's Link field supports. Its title is the element's live title in the current site unless the node overrides it. |
| Custom URL | An absolute, root-relative, fragment or query URL. |
| Email address | A mailto: link. Without a title it shows the address. |
| Phone number | A tel: link. Without a title it shows the number. |
| Heading | A title with no link, rendered as a <span>. |
| Child entries | A structure entry's live children, in structure order, down to a set number of levels. An optional lightswitch field leaves an entry out, along with everything below it. |
Links are stored in the value format of Craft's own Link field ({"type": "entry", "value": "{entry:12@1:url}"}), with the Link field's URL suffix, target, title, class, ID, rel, ARIA label and download, plus hreflang. The URL suffix holds a query string and an anchor together (?tab=residential#team), as Craft stores it; the editor shows them as Add to the address and Jump to a section. download is 1 to download a file under its own name, or the name to save it as. Any node can also carry free key/value attributes, except href, class and aria-current, which the menu sets itself.
Disabled, expired, not-yet-live and deleted elements are left out on the front end, along with any nodes below them. The editor still shows those nodes, with a badge saying why they won't show (Disabled, Expired, Not live yet or Missing), so an editor can fix or remove them. A missing element stays a broken placeholder until it's fixed.
Editing a menu
Menus in the control panel lists the menus you may edit, with how many items each holds and how many are hidden, any broken links, when it was last saved and by whom, and whether it's shared by every site. Admins also get New menu and each menu's Settings. Each name opens the tree editor:
- Rows: each item shows a drag handle, an eye (shared menus), a 48px thumbnail, the title with any badges (Hidden, Disabled, Expired, Not live yet, Missing), a muted line saying what it links to (for example Entry · Projects · /project), and a … menu. The thumbnail is the one Craft shows for the element: an image asset, or the entry type's chosen thumbnail field; anything else gets Craft's image icon. A title that differs from the linked element's live title shows the live one as was “…”.
- Moving items: drag an item by its handle to reorder it. While dragging, move it right to make it a child of the item above, or left to take it out a level; a line shows where and how deep it will land. An item can't land inside itself, deeper than the menu's levels allow, or above items it would leave stranded. From the keyboard, focus an item and use ⌥↑ / ⌥↓ to move it and ⌥→ / ⌥← to indent and outdent it; the … menu has the same moves.
- Adding: Add item lists what you can add, grouped as Link to (entries, categories, files and other linkable elements, a web address, an email address, a phone number) and Structure (a heading, or a parent's child entries). Entries open Craft's chooser, where you can pick several at once; each becomes its own item. Everything else opens the item modal.
- Editing: click an item, or press Enter on it, to open its modal: what it links to (with Change link), Title in the menu (blank shows the linked element's live title, its placeholder), Open in, Jump to a section and Show in the menu. More link options has the tooltip, the screen reader label, the relationship (
rel, with a line explaining each value;noopeneris added for new windows), a download option for file links, and an ID. In Advanced mode admins also get Add to the address, the CSS class, the linked page's language and custom attributes. - The … menu: Edit, Open in the control panel, Copy link, Move up, Move down, Indent, Outdent, Duplicate (with everything below it) and Delete.
- Saving: Save (or Cmd/Ctrl+S) sends the whole tree. It's checked against the menu before anything is stored, and any problems are shown beside the items they're about. Leaving the page with unsaved changes asks first.
- Showing and hiding: in a shared menu, the eye after each item's handle shows or hides it (Show in menu, or Show on {site} when there's more than one site); H does the same from the keyboard. Hidden items stay in the editor, faded and marked Hidden, and are left out of the front end with everything below them. Independent menus have no hiding.
- Loading: the editor says it's loading until the tree is drawn, and Save stays off until then. If the tree can't be drawn, the page says so and Save stays off, so an editor that failed to load can't save over the menu. The server also refuses a save that would empty a menu that has items, unless you confirm Remove every item from this menu?
- Sites: an independent menu has a tree per site, with a site switcher above it. A shared menu has one tree for every site, and the switcher picks which site's titles are shown. When there's more than one site, a shared menu's items also have a Title on {site} for the site being shown. Moving, adding and removing items changes every site, from whichever site you're on; only titles and hiding are per site. Hiding an item hides everything below it.
- History: History lists the menu's latest 20 saves for the site (or for the whole shared menu), with who saved each and when. Preview shows a saved tree without changing anything, and Restore saves it as the menu's tree again, as a new version. A restore that no longer fits the menu (after its levels or kinds have changed) is refused with the reason. Saving a tree that's the same as the latest version doesn't add another.
- Advanced: admins get craft-kit's Advanced switch, remembered for them. It shows each item's kind and the modal's technical fields; everyone else always sees the simple view.
Permissions
Each menu gets an Edit “{menu}” permission (p-nav:edit:{menuUid}) under Parallelogram Nav in user and group permissions. It covers editing the tree, its per-site titles and hiding, its history and restoring a version. Admins can edit every menu. Creating, changing and deleting menus themselves stays with admins, through New menu and each menu's settings.
Templates
{# Loop over a menu's nodes #} <ul> {% for node in craft.pNav.tree('mainMenu') %} <li class="{{ node.active ? 'is-active' }}"> {% if node.isPassive() %} <span>{{ node.title }}</span> {% else %} <a href="{{ node.url }}" {{ node.newWindow ? 'target="_blank"' }}>{{ node.title }}</a> {% endif %} </li> {% endfor %} </ul> {# Or render nested BEM lists #} {{ craft.pNav.render('mainMenu', { block: 'menu', depth: 2 }) }} {# The node for the page being viewed #} {% set here = craft.pNav.activeNode('mainMenu') %}
Nodes answer to the property names Verbb Navigation's nodes use, so most templates move across unchanged: url, title, active, classes, customAttributes, hasActiveChild, children, newWindow, element and isPassive(). They also have id, kind, level, current and linkAttributes.
craft.pNav.activeNode() returns the deepest current node, or failing that the deepest active one. Given a handle it searches that menu; without one it searches every menu in name order.
render() outputs:
<ul class="nav"> <li class="nav__item nav__item--active nav__item--has-children"> <a class="nav__link nav__link--active" href="/projects">Projects</a> <ul class="nav__list nav__list--level-2"> <li class="nav__item nav__item--current nav__item--active"> <a class="nav__link nav__link--current nav__link--active" href="/projects/house" aria-current="page">House</a> </li> </ul> </li> </ul>
block sets the BEM block (nav by default) and depth the number of levels.
Active states
A node is current when it links to the page being viewed: its element is the page's element, or its URL's path is the page's path.
A node is active when it's current, or:
- the page is a structure descendant of the node's element,
- the page's URL sits below the node's URL path (
/projects/housebelow/projects; nothing sits below the home page this way), - the page's element relates to the node's element through the active relation field setting (products active under their category nodes, say), or
- one of the node's children is active.
Modules can change any node's active state:
use parallelogram\nav\events\ModifyActiveEvent; use parallelogram\nav\Plugin; use yii\base\Event; Event::on(Plugin::class, Plugin::EVENT_MODIFY_ACTIVE, function (ModifyActiveEvent $event) { if ($event->node?->id === 'shop' && str_starts_with($event->page?->path ?? '', '/cart')) { $event->active = true; } });
Active states are worked out for each request and never cached.
Caching
Each menu's resolved tree is cached per site in Craft's data cache through Parallelogram Craft Kit. It carries the tags of every element it loaded, the tags of its child-entry queries, and the menu's own p::nav:{handle} tag. When a page shows the menu, those tags are added to the page as well. So:
- saving a linked element, or adding, moving or saving a child entry, purges the cached tree and the pages showing it;
- saving a menu's tree, or changing its definition (including through a project config deploy), invalidates
p::nav:{handle}, which Parallelogram Cache uses to purge the pages showing it.
Moving trees between environments
php craft p-nav/trees/export trees.json php craft p-nav/trees/export --menu=mainMenu php craft p-nav/trees/import trees.json
Exports are keyed by menu handle, then by site handle, with * for a shared menu's single tree. A shared menu's per-site titles and hiding travel with it, keyed by site UID (which project config keeps the same in every environment). Each import that changes a tree is kept as a version, with no user. Imports match menus and sites by handle and report anything they skip: menus or sites that aren't there, a site mode that doesn't match, or a tree that doesn't fit its menu.
Settings
| Setting | |
|---|---|
| Active relation field | A relation field's handle. A page whose element relates to a node's element through it makes that node active. Leave blank to turn this off. |
Development
composer install composer check
The Craft integration suite runs from the sandbox project against its own install, under the pnv_ table prefix:
cd ../parallelogram-craft-sandbox
bash vendor/parallelogram/craft-nav/test/Craft/install-test-db.sh
vendor/bin/phpunit -c vendor/parallelogram/craft-nav/phpunit.craft.xml
The editor's drag engine
The tree editor (src/web/assets/editor/dist/editor.js) is a plain ES module with no build step. It imports the ParallelogramJS drag engine as @parallelogram-js/core/draggable.js. craft-kit's CP UI kit ships that copy and adds it to Craft's import map (the editor's asset bundle depends on UiKitAsset), so installing the plugin needs no npm and nothing loads from a CDN. To update the engine, update craft-kit.
The editor's pure logic has unit tests run with Node's own test runner, with no dependencies (npm run test:js): the level arithmetic for dragging (dist/nesting.js), what each row says, counts and keyboard shortcuts (dist/rows.js), and turning the modal's fields into a stored link and back (dist/link-fields.js). The rest of the script draws and moves list items and opens the modal; whether a tree fits its menu is checked by the server, which the Craft suite covers. Check these by hand after changing it:
- Drag an item to reorder it. Drag it right under the item above to nest it, and left to take it out a level; the accent line shows the depth. Try to nest past the menu's levels, or to take a middle item out of a list: it stays put.
- In a shared menu, click an item's eye (or press H on it): it fades and shows Hidden; Save, reload, and it's still hidden and missing from the front end. Click again to show it. The counts above the tree follow.
- Reload the editor a few times: it always shows its items, or the error message, never an empty menu with Save on. Remove every item and Save: it asks first.
- Focus an item and use ⌥ with the arrow keys to move, indent and outdent it; Enter opens it. The focus stays on the item.
- Open each entry in the … menu: Edit, Open in the control panel (new tab), Copy link, the moves (disabled where they can't apply), Duplicate and Delete.
- Add item → an entry type: pick several entries; each becomes an item. Add a web address, email, phone, heading and child entries; cancelling a new item's modal removes it.
- In the modal, Change link closes it, opens Craft's chooser, and reopens it with the new element. The footer (Delete item, Cancel, Apply) always stays visible. Relationship chips explain themselves on hover or focus. With Advanced on, add a custom attribute named
class: it's refused. - Save a tree with a problem (a custom URL like
ftp://x): the message appears beside that item. - Change something and leave the page: the browser asks first.
Licence
Proprietary: see LICENSE.md. The editor uses the ParallelogramJS drag engine (MIT) from craft-kit; see THIRD-PARTY-NOTICES.md.