I’ve created a plugin using the tutorial and screwing with admin and have the following:
latest.yaml :
enabled: true
theme: default
latest.php:
<?php
namespace Grav\Plugin;
use Grav\Common\Page\Collection;
use Grav\Common\Plugin;
class LatestPlugin extends Plugin
{
public static function getSubscribedEvents() {
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
public function onPluginsInitialized() {
/** @var Uri $uri */
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0]
]);
}
public function onPageInitialized()
{
$this->latestPages(1);
}
public function latestPages($count = 10)
{
/** @var Pages $pages */
$pages = $this->grav['pages'];
$latest = array();
foreach ($pages->routes() as $url => $path) {
$page = $pages->dispatch($url);
if ($page && $page->routable()) {
$latest[$page->route()] = ['modified' => $page->modified(), 'page' => $page];
}
}
// sort based on modified
uasort($latest, function ($a, $b) {
if ($a['modified'] == $b['modified']) {
return 0;
}
return ($a['modified'] > $b['modified']) ? -1 : 1;
});
// build new array with just pages in it
// TODO: Optimized this
$list = array();
foreach ($latest as $item) {
$list[] = $item['page'];
}
$this->grav['debugger']->addMessage(array_slice($list, 0, $count)[0]);
return array_slice($list, 0, $count);
}
}
As you can see here: http://qwyk.ly, the plugin is working (I get an array in the debugger)
My only problem is that in sidebar.html.twig (in user/themes/learn2/templates/partials) the following:
<ul>
<li>THE PLUGIN SHOULD SHOW DATA HERE </li>
{% for posts in latestPages(1) %}
<li>{{ posts.route }}</li>
{% endfor %}
<li>AND WOULD FINISH HERE</li>
</ul>
Fails, returning nothing at posts … All help gratefully received