From 970bf77492f8f3a861d64c61fcd88dfbcfca301a Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 5 Jan 2017 16:02:23 -0700 Subject: [PATCH] Feature/theme dev improvements (#1232) * Initial improvements to help theme development * Added default language to site --- system/blueprints/config/site.yaml | 7 +++++ system/config/site.yaml | 1 + system/src/Grav/Common/Page/Page.php | 33 ++++++++++++++------ system/src/Grav/Common/Plugin.php | 46 ++++++++++++++++++---------- system/src/Grav/Common/Theme.php | 8 +++++ system/src/Grav/Common/Twig/Twig.php | 18 ++++++++--- system/src/Grav/Common/Utils.php | 20 ++++++++++++ 7 files changed, 102 insertions(+), 31 deletions(-) diff --git a/system/blueprints/config/site.yaml b/system/blueprints/config/site.yaml index f21121b887..fe783f084a 100644 --- a/system/blueprints/config/site.yaml +++ b/system/blueprints/config/site.yaml @@ -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 diff --git a/system/config/site.yaml b/system/config/site.yaml index 9bf28526ae..1142a5dd24 100644 --- a/system/config/site.yaml +++ b/system/config/site.yaml @@ -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 diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 2429ab29e2..9b5be0059e 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -1468,37 +1468,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(); @@ -1534,6 +1545,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(); } diff --git a/system/src/Grav/Common/Plugin.php b/system/src/Grav/Common/Plugin.php index e90e91e204..0c31a435ec 100644 --- a/system/src/Grav/Common/Plugin.php +++ b/system/src/Grav/Common/Plugin.php @@ -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; @@ -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)) { @@ -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 * diff --git a/system/src/Grav/Common/Theme.php b/system/src/Grav/Common/Theme.php index 1cfe362801..9063e5fc7c 100644 --- a/system/src/Grav/Common/Theme.php +++ b/system/src/Grav/Common/Theme.php @@ -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 @@ 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 * diff --git a/system/src/Grav/Common/Twig/Twig.php b/system/src/Grav/Common/Twig/Twig.php index 2af6b0a46a..9ef9e3e6de 100644 --- a/system/src/Grav/Common/Twig/Twig.php +++ b/system/src/Grav/Common/Twig/Twig.php @@ -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'), + ]; } } @@ -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(); diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 52334637d6..825abe9cd9 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -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 *