Merge pull request #148 from getgrav/feature/allow-plugins-hook-in-admin

Feature/allow plugins hook in admin
This commit is contained in:
Andy Miller
2015-09-07 07:58:58 -06:00
2 changed files with 35 additions and 3 deletions

View File

@@ -182,9 +182,29 @@ class AdminPlugin extends Plugin
// Replace page service with admin.
$this->grav['page'] = function () use ($self) {
$page = new Page;
$page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$self->template}.md"));
$page->slug(basename($self->template));
return $page;
if (file_exists(__DIR__ . "/pages/admin/{$self->template}.md")) {
$page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$self->template}.md"));
$page->slug(basename($self->template));
return $page;
}
// If the page cannot be found, try looking in plugins.
// Allows pages added by plugins in admin
$plugins = Grav::instance()['config']->get('plugins', []);
foreach($plugins as $plugin => $data) {
$folder = GRAV_ROOT . "/user/plugins/" . $plugin . "/admin";
if (file_exists($folder)) {
$file = $folder . "/pages/{$self->template}.md";
if (file_exists($file)) {
$page->init(new \SplFileInfo($file));
$page->slug(basename($self->template));
return $page;
}
}
}
};
}
@@ -223,6 +243,9 @@ class AdminPlugin extends Plugin
$twig->twig_vars['base_path'] = GRAV_ROOT;
$twig->twig_vars['admin'] = $this->admin;
// Gather Plugin-hooked nav items
$this->grav->fireEvent('onAdminTemplateNavPluginHook');
switch ($this->template) {
case 'dashboard':
$twig->twig_vars['popularity'] = $this->popularity;

View File

@@ -31,6 +31,15 @@
</span>
</a>
</li>
{% if grav.twig.plugins_hooked_nav %}
{% for label, item in grav.twig.plugins_hooked_nav %}
<li class="{{ (location == item.route) ? 'selected' : '' }}">
<a href="{{ base_url_relative }}/{{ item.route }}">
<i class="fa fa-fw {{ item.icon }}"></i> {{ label|tu }}
</a>
</li>
{% endfor %}
{% endif %}
<li class="{{ (location == 'plugins') ? 'selected' : '' }}">
<a href="{{ base_url_relative }}/plugins">
<i class="fa fa-fw fa-plug"></i> {{ "PLUGIN_ADMIN.PLUGINS"|tu }}