From 5ed810a2abeb770096ac418a19db73279b0f96be Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 18 Jan 2021 12:40:13 +0200 Subject: [PATCH] Fixed fatal error when moving a page using the old implementation [#2019] --- CHANGELOG.md | 1 + classes/plugin/AdminController.php | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc24f6fc..1ddaace9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Fixed notifications that would not be remembered as hidden * Fixed taxonomy field not listing existing options in Flex Pages * Fixed taxonomy field not working outside pages + * Fixed fatal error when moving a page using the old implementation [#2019](https://github.com/getgrav/grav-plugin-admin/issues/2019) # v1.10.0-rc.20 ## 12/14/2020 diff --git a/classes/plugin/AdminController.php b/classes/plugin/AdminController.php index e672b0ce..97cf36dd 100644 --- a/classes/plugin/AdminController.php +++ b/classes/plugin/AdminController.php @@ -8,6 +8,7 @@ use Grav\Common\Config\Config; use Grav\Common\Debugger; use Grav\Common\File\CompiledYamlFile; use Grav\Common\Filesystem\Folder; +use Grav\Common\Flex\Types\Pages\PageIndex; use Grav\Common\GPM\GPM as GravGPM; use Grav\Common\GPM\Installer; use Grav\Common\Grav; @@ -1366,7 +1367,10 @@ class AdminController extends AdminBaseController $pages = $this->admin::enablePages(); // Find new parent page in order to build the path. - $route = $data['route'] ?? dirname($this->admin->route); + $path = trim($data['route'] ?? dirname($this->admin->route), '/'); + if ($path === '.') { + $path = ''; + } /** @var PageInterface $obj */ $obj = $this->admin->page(true); @@ -1381,9 +1385,6 @@ class AdminController extends AdminBaseController $this->data['folder'] = $obj->slug(); } - // Ensure route is prefixed with a forward slash. - $route = '/' . ltrim($route, '/'); - // Check for valid frontmatter if (isset($data['frontmatter']) && !$this->checkValidFrontmatter($data['frontmatter'])) { $this->admin->setMessage($this->admin::translate('PLUGIN_ADMIN.INVALID_FRONTMATTER_COULD_NOT_SAVE'), @@ -1403,7 +1404,21 @@ class AdminController extends AdminBaseController } } - $parent = $route && $route !== '/' && $route !== '.' && $route !== '/.' ? $pages->find($route, true) : $pages->root(); + if ($path !== '') { + // First try to get page by its path. + $parent = $pages->get(GRAV_ROOT . '/' . $path); + if (!$parent) { + // Fall back using the route. + $route = '/' . preg_replace(PageIndex::PAGE_ROUTE_REGEX, '/', $path); + $parent = $pages->find($route, true); + if (!$parent) { + throw new \RuntimeException('New parent page cannot be resolved!'); + } + } + } else { + $parent = $pages->root(); + } + $original_order = (int)trim($obj->order(), '.'); try {