Feature/theme dev improvements (#1232)

* Initial improvements to help theme development

* Added default language to site
This commit is contained in:
Andy Miller
2017-01-05 16:02:23 -07:00
committed by GitHub
parent 0145f454b7
commit 970bf77492
7 changed files with 107 additions and 36 deletions

View File

@@ -16,6 +16,13 @@ form:
placeholder: PLUGIN_ADMIN.SITE_TITLE_PLACEHOLDER
help: PLUGIN_ADMIN.SITE_TITLE_HELP
default_lang:
type: text
label: PLUGIN_ADMIN.SITE_DEFAULT_LANG
size: vsmall
placeholder: PLUGIN_ADMIN.SITE_DEFAULT_LANG_PLACEHOLDER
help: PLUGIN_ADMIN.SITE_DEFAULT_LANG_HELP
author.name:
type: text
size: large

View File

@@ -1,4 +1,5 @@
title: Grav # Name of the site
default_lang: en # Default language for site (potentially used by theme)
author:
name: John Appleseed # Default author name

View File

@@ -1468,15 +1468,6 @@ class Page
return isset($order[0]) ? $order[0] : false;
}
/**
* Gets the URL with host information, aka Permalink.
* @return string The permalink.
*/
public function permalink()
{
return $this->url(true);
}
/**
* Gets the URL for a page - alias of url().
*
@@ -1489,16 +1480,36 @@ class Page
return $this->url($include_host);
}
/**
* Gets the URL with host information, aka Permalink.
* @return string The permalink.
*/
public function permalink()
{
return $this->url(true, false, true, true);
}
/**
* Returns the canonical URL for a page
*
* @param bool $include_lang
* @return string
*/
public function canonical($include_lang = true)
{
return $this->url(true, true, $include_lang);
}
/**
* Gets the url for the Page.
*
* @param bool $include_host Defaults false, but true would include http://yourhost.com
* @param bool $canonical true to return the canonical URL
* @param bool $canonical true to return the canonical URL
* @param bool $include_lang
*
* @param bool $raw_route
* @return string The url.
*/
public function url($include_host = false, $canonical = false, $include_lang = true)
public function url($include_host = false, $canonical = false, $include_lang = true, $raw_route = false)
{
$grav = Grav::instance();
@@ -1534,6 +1545,8 @@ class Page
// get canonical route if requested
if ($canonical) {
$route = $pre_route . $this->routeCanonical();
} elseif ($raw_route) {
$route = $pre_route . $this->rawRoute();
} else {
$route = $pre_route . $this->route();
}

View File

@@ -246,21 +246,23 @@ class Plugin implements EventSubscriberInterface, \ArrayAccess
/**
* Merge global and page configurations.
*
* @param Page $page The page to merge the configurations with the
* @param Page $page The page to merge the configurations with the
* plugin settings.
* @param bool $deep Should you use deep or shallow merging
* @param array $params Array of additional configuration options to
* @param mixed $deep false = shallow|true = recursive|merge = recursive+unique
* @param array $params Array of additional configuration options to
* merge with the plugin settings.
* @param string $type Is this 'plugins' or 'themes'
*
* @return \Grav\Common\Data\Data
* @return Data
*/
protected function mergeConfig(Page $page, $deep = false, $params = [])
protected function mergeConfig(Page $page, $deep = false, $params = [], $type = 'plugins')
{
$class_name = $this->name;
$class_name_merged = $class_name . '.merged';
$defaults = $this->config->get('plugins.' . $class_name, []);
$defaults = $this->config->get($type . '.' . $class_name, []);
$page_header = $page->header();
$header = [];
if (!isset($page_header->$class_name_merged) && isset($page_header->$class_name)) {
// Get default plugin configurations and retrieve page header configuration
$config = $page_header->$class_name;
@@ -269,11 +271,8 @@ class Plugin implements EventSubscriberInterface, \ArrayAccess
$config = ['enabled' => $config];
}
// Merge page header settings using deep or shallow merging technique
if ($deep) {
$header = array_replace_recursive($defaults, $config);
} else {
$header = array_merge($defaults, $config);
}
$header = $this->mergeArrays($deep, $defaults, $config);
// Create new config object and set it on the page object so it's cached for next time
$page->modifyHeader($class_name_merged, new Data($header));
} else if (isset($page_header->$class_name_merged)) {
@@ -284,16 +283,31 @@ class Plugin implements EventSubscriberInterface, \ArrayAccess
$header = $defaults;
}
// Merge additional parameter with configuration options
if ($deep) {
$header = array_replace_recursive($header, $params);
} else {
$header = array_merge($header, $params);
}
$header = $this->mergeArrays($deep, $header, $params);
// Return configurations as a new data config class
return new Data($header);
}
/**
* Merge arrays based on deepness
*
* @param bool $deep
* @param $array1
* @param $array2
* @return array|mixed
*/
private function mergeArrays($deep = false, $array1, $array2)
{
if ($deep == 'merge') {
return Utils::arrayMergeRecursiveUnique($array1, $array2);
} elseif ($deep == true) {
return array_replace_recursive($array1, $array2);
} else {
return array_merge($array1, $array2);
}
}
/**
* Persists to disk the plugin parameters currently stored in the Grav Config object
*

View File

@@ -8,6 +8,7 @@
namespace Grav\Common;
use Grav\Common\Page\Page;
use Grav\Common\Config\Config;
use RocketTheme\Toolbox\File\YamlFile;
@@ -59,6 +60,13 @@ class Theme extends Plugin
return true;
}
/**
* Override the mergeConfig method to work for themes
*/
protected function mergeConfig(Page $page, $deep = 'merge', $params = [], $type = 'themes') {
return parent::mergeConfig($page, $deep, $params, $type);
}
/**
* Simpler getter for the theme blueprint
*

View File

@@ -152,21 +152,28 @@ class Twig
$this->grav->fireEvent('onTwigExtensions');
$base_url = $this->grav['base_url'] . $path_append;
// Set some standard variables for twig
$this->twig_vars = $this->twig_vars + [
'config' => $config,
'uri' => $this->grav['uri'],
'base_dir' => rtrim(ROOT_DIR, '/'),
'base_url' => $this->grav['base_url'] . $path_append,
'base_url_simple' => $this->grav['base_url'],
'base_url_absolute' => $this->grav['base_url_absolute'] . $path_append,
'base_url_relative' => $this->grav['base_url_relative'] . $path_append,
'theme_dir' => $locator->findResource('theme://'),
'theme_url' => $this->grav['base_url'] . '/' . $locator->findResource('theme://', false),
'system' => $config->get('system'),
'theme' => $config->get('theme'),
'site' => $config->get('site'),
'uri' => $this->grav['uri'],
'assets' => $this->grav['assets'],
'taxonomy' => $this->grav['taxonomy'],
'browser' => $this->grav['browser'],
'base_dir' => rtrim(ROOT_DIR, '/'),
'base_url' => $base_url,
'base_url_simple' => $this->grav['base_url'],
'base_url_absolute' => $this->grav['base_url_absolute'] . $path_append,
'base_url_relative' => $this->grav['base_url_relative'] . $path_append,
'home_url' => $base_url == '' ? '/' : $base_url,
'theme_dir' => $locator->findResource('theme://'),
'theme_url' => $this->grav['base_url'] . '/' . $locator->findResource('theme://', false),
'html_lang' => $this->grav['language']->getActive() ?: $config->get('site.default_lang', 'en'),
];
}
}
@@ -320,6 +327,7 @@ class Twig
$twig_vars = $this->twig_vars;
$twig_vars['theme'] = $this->grav['config']->get('theme');
$twig_vars['pages'] = $pages->root();
$twig_vars['page'] = $page;
$twig_vars['header'] = $page->header();

View File

@@ -109,6 +109,26 @@ abstract class Utils
return (object)array_merge((array)$obj1, (array)$obj2);
}
/**
* Recursive Merge with uniqueness
*
* @param $array1
* @param $array2
* @return mixed
*/
public static function arrayMergeRecursiveUnique($array1, $array2)
{
if (empty($array1)) return $array2; //optimize the base case
foreach ($array2 as $key => $value) {
if (is_array($value) && is_array(@$array1[$key])) {
$value = static::arrayMergeRecursiveUnique($array1[$key], $value);
}
$array1[$key] = $value;
}
return $array1;
}
/**
* Return the Grav date formats allowed
*