Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Admin-plugin elements

Started by Muut Archive 9 years ago · 3 replies · 712 views
9 years ago

Are all the elements in the admin-plugin constructed by its theme? That is, the components structured with HTML/Twig in user/plugins/admin/themes/grav/templates/. I'm working on some alternate styles for the plugin, and would like to construct a simple test-page within the admin that displays the whole range of components that the plugin uses.

It would also be quite helpful for visual regression tests for the plugin itself and any future-themes or styles made for it.

9 years ago

Yes, plus some base form fields provided by the Form plugin.

9 years ago

Ok, I've been looking at how the Data Manager plugin does this, and the Plugin Tutorial, but I still end up at a 404 when accessing the route within the admin. I have a pluginname/blueprints/pluginname.yaml, as well as a pluginname/blueprints/pluginname.html.twig, and run the following in pluginname/pluginname.php:

PHP
<?php
namespace Grav\Plugin;

use Grav\Common\Data;
use Grav\Common\Plugin;
use Grav\Common\Grav;
use Grav\Common\Uri;
use Grav\Common\Taxonomy;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;

class PluginNamePlugin extends Plugin {

    protected $route = 'pluginname';

    public static function getSubscribedEvents() {
        return [ 
            'onPageInitialized' => ['onPageInitialized', 0],
            'onGetPageTemplates' => ['onGetPageTemplates', 0],
            'onAdminMenu' => ['onAdminMenu', 0],
            'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0]
        ];
    }
    public function onPageInitialized(Event $event) {
        if ($this->isAdmin()) {
            $assets = $this->grav['assets'];
            $pluginsobject = (array) $this->config->get('plugins');
            if (isset($pluginsobject['pluginname'])) {
                if ($pluginsobject['pluginname']['enabled'] && $pluginsobject['pluginname']['current']) {
                    $assets->addCss('plugin://pluginname/styles/' . $pluginsobject['pluginname']['current'] . '.css', 1);
                    $assets->addCss('plugin://pluginname/styles/radioimage.css', 1);
                }
            }
        }
    }
    public function onTwigTemplatePaths() {
        if ($this->isAdmin()) {
            $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
        }
    }
    public function onGetPageTemplates($event) {
        if ($this->isAdmin()) {
            $types = $event->types;
            $locator = Grav::instance()['locator'];
            $types->scanBlueprints($locator->findResources('plugin://' . $this->name . '/blueprints'));
            $types->scanTemplates($locator->findResources('plugin://' . $this->name . '/templates'));
        }
    }
    public function onAdminMenu() {
        if ($this->isAdmin()) {
            $this->grav['twig']->plugins_hooked_nav['pluginname'] = ['route' => $this->route, 'icon' => 'fa-database'];
        }
    }
}

To my eyes this should load any blueprints and templates, as well as create the menu-icon and route for /admin/pluginname/. Am I missing some integral part that registers the route?

9 years ago

Thanks for the tip on Gitter @flaviocopes, the comments-plugin lead me in the right direction.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1349 9 years ago
Archive · by Muut Archive, 9 years ago
2 934 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2946 9 years ago
Archive · by Muut Archive, 9 years ago
3 1118 9 years ago