-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubpages.php
73 lines (62 loc) · 1.76 KB
/
subpages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
/**
* Subpages Plugin
*
* Displays a list of direct sub-pages of the current page.
*
* @author Andrew Calcutt <[email protected]>
* @license MIT
*/
class SubpagesPlugin extends Plugin
{
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
];
}
public function onPluginsInitialized()
{
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0]
]);
}
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function onTwigSiteVariables()
{
$page = $this->grav['page'];
// Get visible subpages
$subpages = $this->getVisibleSubpages($page);
$parent = $page->parent();
$this->grav['twig']->twig_vars['subpages'] = $subpages;
$this->grav['twig']->twig_vars['parent'] = $parent;
$this->grav['twig']->twig_vars['subpages_config'] = $this->config->get('plugins.subpages');
}
/**
* Get visible subpages
*
* @param Page $page
*
* @return array
*/
private function getVisibleSubpages(Page $page)
{
$subpages = [];
$children = $page->children();
if( $children ){
foreach ($children as $child) {
if( !$child->routable() ) continue;
if( !$child->visible() ) continue;
$subpages[] = $child;
}
}
return $subpages;
}
}