backported theme_var enhanced logic from 1.7

This commit is contained in:
Andy Miller
2020-09-10 15:52:52 -06:00
parent 03c6e74c4d
commit b8b1bed7ed

View File

@@ -11,10 +11,12 @@ namespace Grav\Common\Twig;
use Cron\CronExpression;
use Grav\Common\Config\Config;
use Grav\Common\Data\Data;
use Grav\Common\Debugger;
use Grav\Common\Grav;
use Grav\Common\Language\Language;
use Grav\Common\Page\Collection;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Page\Media;
use Grav\Common\Scheduler\Cron;
use Grav\Common\Security;
@@ -168,8 +170,8 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
new \Twig_SimpleFunction('exif', [$this, 'exifFunc']),
new \Twig_SimpleFunction('media_directory', [$this, 'mediaDirFunc']),
new \Twig_SimpleFunction('body_class', [$this, 'bodyClassFunc']),
new \Twig_SimpleFunction('theme_var', [$this, 'themeVarFunc']),
new \Twig_SimpleFunction('header_var', [$this, 'pageHeaderVarFunc']),
new \Twig_SimpleFunction('theme_var', [$this, 'themeVarFunc'], ['needs_context' => true]),
new \Twig_SimpleFunction('header_var', [$this, 'pageHeaderVarFunc'], ['needs_context' => true]),
new \Twig_SimpleFunction('read_file', [$this, 'readFileFunc']),
new \Twig_SimpleFunction('nicenumber', [$this, 'niceNumberFunc']),
new \Twig_SimpleFunction('nicefilesize', [$this, 'niceFilesizeFunc']),
@@ -1284,17 +1286,64 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
/**
* Get a theme variable
* Will try to get the variable for the current page, if not found, it tries it's parent page on up to root.
* If still not found, will use the theme's configuration value,
* If still not found, will use the $default value passed in
*
* @param string $var
* @param bool $default
* @param $context Twig Context
* @param string $var variable to be found (using dot notation)
* @param null $default the default value to be used as last resort
* @param null $page an optional page to use for the current page
* @param bool $exists toggle to simply return the page where the variable is set, else null
* @return string
*/
public function themeVarFunc($var, $default = null)
public function themeVarFunc($context, $var, $default = null, $page = null, $exists = false)
{
$header = $this->grav['page']->header();
$header_classes = $header->{$var} ?? null;
$page = $page ?? $context['page'] ?? Grav::instance()['page'] ?? null;
return $header_classes ?: $this->config->get('theme.' . $var, $default);
// Try to find var in the page headers
if ($page instanceof PageInterface && $page->exists()) {
// Loop over pages and look for header vars
while ($page && !$page->root()) {
$header = new Data((array)$page->header());
$value = $header->get($var);
if (isset($value)) {
if ($exists) {
return $page;
} else {
return $value;
}
}
$page = $page->parent();
}
}
if ($exists) {
return false;
} else {
return Grav::instance()['config']->get('theme.' . $var, $default);
}
}
/**
* Look for a page header variable in an array of pages working its way through until a value is found
*
* @param $context
* @param string $var the variable to look for in the page header
* @param string|string[]|null $pages array of pages to check (current page upwards if not null)
* @param bool $exists if true, return the page where the var is found, not the value
* @return mixed
* @deprecated 1.7 Use themeVarFunc() instead
*/
public function pageHeaderVarFunc($context, $var, $pages = null)
{
if (is_array($pages)) {
$page = array_shift($pages);
} else {
$page = null;
}
return $this->themeVarFunc($context, $var, null, $page);
}
/**
@@ -1322,41 +1371,6 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
return $body_classes;
}
/**
* Look for a page header variable in an array of pages working its way through until a value is found
*
* @param string $var
* @param string|string[]|null $pages
* @return mixed
*/
public function pageHeaderVarFunc($var, $pages = null)
{
if ($pages === null) {
$pages = $this->grav['page'];
}
// Make sure pages are an array
if (!\is_array($pages)) {
$pages = [$pages];
}
// Loop over pages and look for header vars
foreach ($pages as $page) {
if (\is_string($page)) {
$page = $this->grav['pages']->find($page);
}
if ($page) {
$header = $page->header();
if (isset($header->{$var})) {
return $header->{$var};
}
}
}
return null;
}
/**
* Dump/Encode data into YAML format
*