Skip to content

Commit

Permalink
Support latest Grav + Admin RC releases and comply to PSR 0/2
Browse files Browse the repository at this point in the history
  • Loading branch information
Sommerregen committed Jun 24, 2016
1 parent 1b6d07a commit 0098814
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 173 deletions.
49 changes: 20 additions & 29 deletions blueprints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,32 @@ author:
name: Sommerregen
email: [email protected]
homepage: https://github.com/sommerregen/grav-plugin-themer
keywords: [themer, theme, plugin]
keywords: ['themer', 'theme', 'plugin']
docs: https://github.com/sommerregen/grav-plugin-themer/blob/master/README.md
bugs: https://github.com/sommerregen/grav-plugin-themer/issues
license: MIT/GPL

form:
validation: strict
fields:
global:
type: section
title: PLUGINS.THEMER.GLOBAL_CONFIG
underline: 1
enabled:
type: toggle
label: PLUGINS.THEMER.PLUGIN_STATUS
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool

fields:
enabled:
type: toggle
label: PLUGINS.THEMER.PLUGIN_STATUS
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
templates:
type: themeselect
label: PLUGINS.THEMER.TEMPLATES
help: PLUGINS.THEMER.TEMPLATES_HELP
placeholder: PLUGINS.THEMER.TEMPLATES_PLACEHOLDER
selectize: true
multiple: true

templates:
# Not ready for production yet... :'-(
# type: themeselect
# selectize: true
# multiple: true

type: selectize
label: PLUGINS.THEMER.TEMPLATES
help: PLUGINS.THEMER.TEMPLATES_HELP
placeholder: PLUGINS.THEMER.TEMPLATES_PLACEHOLDER

validate:
type: commalist
validate:
type: commalist
6 changes: 1 addition & 5 deletions blueprints/themer.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
form:
validation: loose

fields:
tabs:
type: tabs
active: 1

fields:
advanced:
fields:
Expand All @@ -15,7 +11,7 @@ form:
type: themeselect
label: PLUGINS.THEMER.THEME
help: PLUGINS.THEMER.THEME_HELP
'@config-default': "system.pages.theme"
config-default@: system.pages.theme
toggleable: true
selectize:
create: true
Expand Down
36 changes: 18 additions & 18 deletions languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
de:
PLUGINS:
THEMER:
GLOBAL_CONFIG: "Globale Einstellungen"
PLUGIN_STATUS: "Plugin Status"
THEMER: "Themer"
GLOBAL_CONFIG: 'Globale Einstellungen'
PLUGIN_STATUS: 'Plugin Status'
THEMER: 'Themer'

TEMPLATES: "Theme Templates"
TEMPLATES_HELP: "Standard-Templates im Admin Panel (frei lassen um alle verfügbaren Templates anzuzeigen)"
TEMPLATES_PLACEHOLDER: "< Zeige alle Templates >"
TEMPLATES: 'Theme Templates'
TEMPLATES_HELP: 'Standard-Templates im Admin Panel (frei lassen um alle verfügbaren Templates anzuzeigen)'
TEMPLATES_PLACEHOLDER: '- Zeige alle Templates -'

THEME: "Theme"
THEME_HELP: "Wähle ein Theme für diese Seite aus"
THEME: 'Theme'
THEME_HELP: 'Wähle ein Theme für diese Seite aus'

# English
en:
PLUGINS:
THEMER:
GLOBAL_CONFIG: "Global plugin configurations"
PLUGIN_STATUS: "Plugin status"
THEMER: "Themer"
GLOBAL_CONFIG: 'Global plugin configurations'
PLUGIN_STATUS: 'Plugin status'
THEMER: 'Themer'

TEMPLATES: "Theme templates"
TEMPLATES_HELP: "Default theme page templates to show in admin panel (leave empty to show all page templates)"
TEMPLATES_PLACEHOLDER: "< Show all page templates >"
TEMPLATES: 'Theme templates'
TEMPLATES_HELP: 'Default theme page templates to show in admin panel (leave empty to show all page templates)'
TEMPLATES_PLACEHOLDER: '- Show all page templates -'

THEME: "Theme"
THEME_HELP: "Select a theme for the page"
THEME: 'Theme'
THEME_HELP: 'Select a theme for the page'

# Fench
fr:
PLUGINS:
THEMER:
GLOBAL_CONFIG: "Configurations générales du plugin"
PLUGIN_STATUS: "Statut du plugin"
GLOBAL_CONFIG: 'Configurations générales du plugin'
PLUGIN_STATUS: 'Statut du plugin'
239 changes: 118 additions & 121 deletions themer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,136 +33,133 @@
*/
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],
'onGetPageTemplates' => ['onGetPageTemplates', 0],
];
}

/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
if ($this->config->get('plugins.themer.enabled')) {
$events = [
'onPageInitialized' => ['onPageInitialized', 1000]
];

// Set admin specific events
if ($this->isAdmin()) {
$this->active = false;
/**
* 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 = [
'onBlueprintCreated' => ['onBlueprintCreated', 0]
'onPageInitialized' => ['onPageInitialized', 1000]
];
}

// Register events
$this->enable($events);
// 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();
}

/**
* 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)
{
if ($this->config->get('plugins.themer.enabled')) {
/** @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) {
if ($path = $locator->findResource("themes://{$template}/templates/")) {
$types->scanTemplates($path);

/**
* 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);

/**
* 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;

/**
* 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 $theme;
}
$page = $page->parent();
return $default;
}

return $default;
}
}

0 comments on commit 0098814

Please sign in to comment.