getgrav / getgrav/grav-plugin-admin

Initialize Theme templates in Admin

Open
#1,819 0 comments 0 reactions 1 assignee Claimed by @mahagr View on GitHub
Dominant language
PHP
Stars
377
Forks
225
Avg merge
11h 51m
Merged PRs (30d)
4

Description

To make the [question asked on Discord](https://discordapp.com/channels/501836936584101899/506916956637495306/671455753256697858) more concrete, and make it easier to discover a solution for others, here's a replication and description.

I am trying to find a Page and render it with its template, in the event [`onAdminTaskExecute`](https://learn.getgrav.org/16/admin-panel/extending/admin-events#onAdminTaskExecute). To do this I:

1. Visit `http://localhost:8000/admin/task:processPageTemplate/admin-nonce:foobar`
2. Check that this is the wanted task and authorize it
3. [Enable Pages](https://github.com/getgrav/grav/issues/2737) in Admin
4. Initialize Themes and [Twig](https://github.com/getgrav/grav/blob/5ba4c8ee5beeed0283088b76d6aec9fe865c38fe/system/src/Grav/Common/Twig/Twig.php#L77), to [merge the Theme's templates into Twig](https://github.com/getgrav/grav/blob/5ba4c8ee5beeed0283088b76d6aec9fe865c38fe/system/src/Grav/Common/Twig/Twig.php#L97)
5. Find the `/home`-Page, included in Grav downloads by default
6. Dump `twig_paths` and get:

```php
^ array:7 [▼
0 => "C:\Users\Ole\Caddy\Task\user\plugins\flex-objects/admin/templates"
1 => "C:\Users\Ole\Caddy\Task\user\plugins\admin/themes/grav/templates"
2 => "C:\Users\Ole\Caddy\Task\user\plugins\email/templates"
3 => "C:\Users\Ole\Caddy\Task\user\plugins\form/templates"
4 => "C:\Users\Ole\Caddy\Task\user\plugins\login/templates"
5 => "C:\Users\Ole\Caddy\Task\user\plugins\error/templates"
6 => "C:/Users/Ole/Caddy/Task/system/templates"
]
```

Thus, this:

```php
$content = $this->grav['admin']->grav['twig']->processTemplate(
$Page->template() . '.' . $Page->templateFormat('html') . '.twig',
['page' => $Page]
);
dump($content);
```

Fails with `Template "default.html.twig" is not defined.`

The result is the same if I try to forcefully load the theme-templates with:

```php
foreach ($this->grav['config']->get('themes') as $theme => $config) {
$this->grav['twig']->twig_paths = array_merge(
$this->grav['admin']->grav['twig']->twig_paths,
$this->grav['locator']->findResources('themes://' . $theme . '/templates')
);
}
```

This was tested on a clean installation of Core 1.7.0-rc.3 and Admin 1.10.0-rc.3, creating a new plugin with the DevTools with this PHP:

```php
['onPluginsInitialized', 0]
];
}

public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->enable([
'onAdminTaskExecute' => ['onAdminTaskExecute', 0]
]);
}
}

public function onAdminTaskExecute(Event $e)
{
if ($e['method'] == 'taskProcessPageTemplate') {
if (!$e['controller']->authorizeTask('processPageTemplate', ['admin.maintenance', 'admin.super'])) {
header('HTTP/1.0 403 Forbidden');
echo '403 Forbidden';
exit;
}
if (isset(Grav::instance()['admin'])) {
if (method_exists(Grav::instance()['admin'], 'enablePages')) {
Grav::instance()['admin']->enablePages();
}
}
$this->grav['admin']->grav['themes']->init();
$this->grav['admin']->grav['twig']->init();
$Page = $this->grav['admin']->grav['pages']->find('/home');

// Check registered Twig paths
dump('twig_paths');
dump($this->grav['admin']->grav['twig']->twig_paths);

// Try to forcefully load templates
foreach ($this->grav['config']->get('themes') as $theme => $config) {
$this->grav['twig']->twig_paths = array_merge(
$this->grav['admin']->grav['twig']->twig_paths,
$this->grav['locator']->findResources('themes://' . $theme . '/templates')
);
}

// Check rendered template
$content = $this->grav['admin']->grav['twig']->processTemplate(
$Page->template() . '.' . $Page->templateFormat('html') . '.twig',
['page' => $Page]
);
dump($content);
exit();
}
}
}
```

Copy of the plugin:
[task-test.zip](https://github.com/getgrav/grav-plugin-admin/files/4124445/task-test.zip)

Copy of the Grav-installation:
[grav-admin-v1.7.0-rc.3.zip](https://github.com/getgrav/grav-plugin-admin/files/4124447/grav-admin-v1.7.0-rc.3.zip)

As you write, @mahagr, Admin doesn't trigger the necessary events by default. But both `onThemeInitialized` and `onTwigTemplatePaths` are fired, and `$this->grav['admin']->grav['twig']->init()` should seemingly register the templates. Any thoughts on why they don't?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.