diff --git a/system/blueprints/config/system.yaml b/system/blueprints/config/system.yaml index 8acee8597..2d7f78b56 100644 --- a/system/blueprints/config/system.yaml +++ b/system/blueprints/config/system.yaml @@ -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 diff --git a/system/config/system.yaml b/system/config/system.yaml index 3dfaa1c3b..4b0bdb486 100644 --- a/system/config/system.yaml +++ b/system/config/system.yaml @@ -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) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 20e450a60..4cf61791d 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -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. *