forked from Sommerregen/grav-plugin-themer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
themer.php
165 lines (146 loc) · 4.63 KB
/
themer.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Themer v1.1.0
*
* This plugin enables you to use different themes on one site
* individual set per page or collection.
*
* Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
* http://benjamin-regler.de/license/
*
* @package Themer
* @version 1.1.0
* @link <https://github.com/sommerregen/grav-plugin-themer>
* @author Benjamin Regler <[email protected]>
* @copyright 2015-2016, Benjamin Regler
* @license <http://opensource.org/licenses/MIT> MIT
* @license <http://opensource.org/licenses/GPL-3.0> GPLv3
*/
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Data\Blueprints;
use RocketTheme\Toolbox\Event\Event;
/**
* ThemerPlugin
*
* This plugin enables you to use different themes on one site individual
* set per page or collection.
*/
class ThemerPlugin extends Plugin
{
/**
* Return a list of subscribed events of this plugin.
*
* @return array The list of events of the plugin of the form
* 'name' => ['method_name', priority].
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
$events = [
'onPageInitialized' => ['onPageInitialized', 1000]
];
// Set admin specific events
if ($this->isAdmin()) {
$this->active = false;
$events = [
'onBlueprintCreated' => ['onBlueprintCreated', 0]
];
}
// Register events
$events['onGetPageTemplates'] = ['onGetPageTemplates', 0];
$this->enable($events);
}
/**
* Change theme of page
*/
public function onPageInitialized()
{
/** @var Page $page */
$page = $this->grav['page'];
$config = $this->mergeConfig($page);
if ($config->get('enabled') && ($theme = $this->mergeThemeConfig($page))) {
if ($theme !== $this->config->get('system.pages.theme')) {
// Update system configurations
$this->config->set('system.pages.theme', $theme);
// Reload themes to reflect changes
$this->grav['themes']->init();
// Reset and re-initialize Twig environment
$twig = $this->grav['twig'];
$twig->twig = null;
$twig->twig_paths = [];
$twig->init();
}
}
}
/**
* Add page template types.
*/
public function onGetPageTemplates(Event $event)
{
/** @var Types $types */
$types = $event->types;
/** @var Locator $locator */
$locator = $this->grav['locator'];
// Add theme templates to list
$templates = $this->config->get('plugins.themer.templates', '');
if ($templates && !is_array($templates)) {
$templates = explode(', ', $templates);
}
$templates = $templates ?: array_keys($this->grav['themes']->all());
foreach ($templates as $template) {
$template = strtolower($template);
if ($path = $locator->findResource("themes://{$template}/templates/")) {
$types->scanTemplates($path);
}
}
}
/**
* Extend page blueprints with ArchivePlus configuration options.
*
* @param Event $event
*/
public function onBlueprintCreated(Event $event)
{
/** @var Blueprints $blueprint */
$blueprint = $event['blueprint'];
if ($blueprint->get('form/fields/tabs')) {
$blueprints = new Blueprints(__DIR__ . '/blueprints/');
$extends = $blueprints->get($this->name);
$blueprint->extend($extends, true);
}
}
/**
* Merge global and page theme settings
*
* @param Page $page The page to merge the page theme configurations
* with the theme settings.
* @param bool $default The default value in case no theme setting was
* found.
*
* @return array
*/
protected function mergeThemeConfig(Page $page, $default = null)
{
while ($page && !$page->root()) {
if (isset($page->header()->theme)) {
$theme = $page->header()->theme;
if ($theme === '@default') {
$theme = $default;
}
return $theme;
}
$page = $page->parent();
}
return $default;
}
}