0

I'm in the process of writing a plugin for my website. It has a file for the top level file, but all of the child pages are generated by the plugin itself. This is mostly because the plugin has to deal with a database.

Currently my webpage is setup as mysite.com/plugin_page. This plugin page does in fact have an associated .md file. The plugin is able to generate child pages such as mysite.com/plugin_page/view/$ and mysite.com/plugin_page/new.

When I'm viewing mysite.com/plugin_page the correct page is marked as active, so it's highlighted as I'd expect in the navbar. Once I go to a child page, however, no page is highlighted.

I want mysite.com/plugin_page/view/$ to highlight it's parent page, just like a blog entry would have blog highlighted in the example themes.

Jacobm001
  • 4,431
  • 4
  • 30
  • 51

1 Answers1

0

After digging through the Grav source code I figured out the solution to this problem. It involves a couple steps.

1. The route must be set

If the page's route isn't set, the page will never return true when active() is called. Since no page ever returns true, the highlight never appears.

Example Code:

$uri  = $this->grav['uri'];
$page = $this->grav['page'];

...

$page->route($uri->path());

2. The page needs to be assigned to the appropriate parent page

If the generated page isn't the parent page itself, you need to assign it to the appropriate parent.

Example Code:

$pages        = $this->grav['pages'];
$parent_route = $this->config->get('plugins.myplugin.parent_route');
$page->parent($pages->find($parent_route));
Jacobm001
  • 4,431
  • 4
  • 30
  • 51