Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/theme dev improvements #1232

Merged
merged 2 commits into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions system/blueprints/config/site.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions system/config/site.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 23 additions & 10 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -1465,37 +1465,48 @@ public function order($var = null)
return isset($order[0]) ? $order[0] : false;
}

/**
* Gets the URL for a page - alias of url().
*
* @param bool $include_host
*
* @return string the permalink
*/
public function link($include_host = false)
{
return $this->url($include_host);
}

/**
* Gets the URL with host information, aka Permalink.
* @return string The permalink.
*/
public function permalink()
{
return $this->url(true);
return $this->url(true, false, true, true);
}

/**
* Gets the URL for a page - alias of url().
* Returns the canonical URL for a page
*
* @param bool $include_host
*
* @return string the permalink
* @param bool $include_lang
* @return string
*/
public function link($include_host = false)
public function canonical($include_lang = true)
{
return $this->url($include_host);
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();

Expand Down Expand Up @@ -1531,6 +1542,8 @@ public function url($include_host = false, $canonical = false, $include_lang = t
// 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();
}
Expand Down
46 changes: 30 additions & 16 deletions system/src/Grav/Common/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,23 @@ protected function parseLinks($content, $function, $internal_regex = '(.*)')
/**
* 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;
Expand All @@ -269,11 +271,8 @@ protected function mergeConfig(Page $page, $deep = false, $params = [])
$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)) {
Expand All @@ -284,16 +283,31 @@ protected function mergeConfig(Page $page, $deep = false, $params = [])
$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
*
Expand Down
8 changes: 8 additions & 0 deletions system/src/Grav/Common/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Grav\Common;

use Grav\Common\Page\Page;
use Grav\Common\Config\Config;
use RocketTheme\Toolbox\File\YamlFile;

Expand Down Expand Up @@ -59,6 +60,13 @@ public static function saveConfig($theme_name)
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
*
Expand Down
18 changes: 13 additions & 5 deletions system/src/Grav/Common/Twig/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,28 @@ public function init()

$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,
'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' => $this->grav['base_url'] . $path_append,
'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),
'site' => $config->get('site'),
'assets' => $this->grav['assets'],
'taxonomy' => $this->grav['taxonomy'],
'browser' => $this->grav['browser'],
'html_lang' => $this->grav['language']->getActive() ?: $config->get('site.default_lang', 'en'),

];
}
}
Expand Down Expand Up @@ -320,6 +327,7 @@ public function processSite($format = null)

$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();
Expand Down
20 changes: 20 additions & 0 deletions system/src/Grav/Common/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ public static function mergeObjects($obj1, $obj2)
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
*
Expand Down