Merge branch 'feature/hide_home_in_urls' into develop

This commit is contained in:
Andy Miller
2016-01-04 11:14:57 -07:00
3 changed files with 57 additions and 1 deletions

View File

@@ -20,6 +20,18 @@ form:
show_root: false
help: PLUGIN_ADMIN.HOME_PAGE_HELP
home.hide_in_urls:
type: toggle
label: PLUGIN_ADMIN.HIDE_HOME_IN_URLS
help: PLUGIN_ADMIN.HIDE_HOME_IN_URLS_HELP
highlight: 0
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool
pages.theme:
type: themeselect
classes: fancy

View File

@@ -15,6 +15,7 @@ languages:
home:
alias: '/home' # Default path for home, ie /
hide_in_urls: false # Hide the home route in URLs
pages:
theme: antimatter # Default theme (defaults to "antimatter" theme)

View File

@@ -85,6 +85,8 @@ class Page
protected $markdown_extra;
protected $etag;
protected $last_modified;
protected $home_route;
protected $hide_home_route;
/**
* @var Page Unmodified (original) version of the page. Used for copying and moving the page.
@@ -118,6 +120,10 @@ class Page
*/
public function init(\SplFileInfo $file, $extension = null)
{
$config = self::getGrav()['config'];
$this->hide_home_route = $config->get('system.home.hide_in_urls', false);
$this->home_route = $config->get('system.home.alias');
$this->filePath($file->getPathName());
$this->modified($file->getMTime());
$this->id($this->modified().md5($this->filePath()));
@@ -132,6 +138,8 @@ class Page
$this->published();
$this->urlExtension();
// some extension logic
if (empty($extension)) {
$this->extension('.'.$file->getExtension());
@@ -1295,8 +1303,18 @@ class Page
}
if (empty($this->route)) {
$baseRoute = null;
// calculate route based on parent slugs
$baseRoute = $this->parent ? (string) $this->parent()->route() : null;
$parent = $this->parent();
if (isset($parent)) {
if ($this->hide_home_route && $parent->route() == $this->home_route) {
$baseRoute = '';
} else {
$baseRoute = (string) $parent->route();
}
}
$this->route = isset($baseRoute) ? $baseRoute . '/'. $this->slug() : null;
if (!empty($this->routes) && isset($this->routes['default'])) {
@@ -1690,6 +1708,31 @@ class Page
return $pages->get($this->parent);
}
/**
* Gets the top parent object for this page
*
* @return Page|null the top parent page object if it exists.
*/
public function topParent()
{
$topParent = $this->parent();
if (!$topParent) {
return null;
}
while (true) {
$theParent = $topParent->parent();
if ($theParent != null && $theParent->parent() !== null) {
$topParent = $theParent;
} else {
break;
}
}
return $topParent;
}
/**
* Returns children of this page.
*