From 1eb156f1aac64d388374c63cd401279f8d528481 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 24 Feb 2022 12:09:54 -0500 Subject: [PATCH] Fix issue processwire/processwire-issues#1535 --- wire/core/PagesEditor.php | 21 ++++++++++++--------- wire/core/PagesLoader.php | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/wire/core/PagesEditor.php b/wire/core/PagesEditor.php index 29744433..be41fed1 100644 --- a/wire/core/PagesEditor.php +++ b/wire/core/PagesEditor.php @@ -1834,7 +1834,7 @@ class PagesEditor extends Wire { // detect name from path $options['name'] = $name; } - if(empty($parent)) { + if(empty($parent) && !$this->pages->loader()->isLoading()) { // detect parent from path $parentPath = count($parts) ? implode('/', $parts) : '/'; $parent = $this->pages->getByPath($parentPath); @@ -1844,7 +1844,7 @@ class PagesEditor extends Wire { } // detect template from parent (when possible) - if(!$template && !empty($parent) && empty($options['id'])) { + if(!$template && !empty($parent) && empty($options['id']) && !$this->pages->loader()->isLoading()) { $parent = is_object($parent) ? $parent : $this->pages->get($parent); if($parent->id) { if(count($parent->template->childTemplates) === 1) { @@ -1856,14 +1856,17 @@ class PagesEditor extends Wire { } // detect parent from template (when possible) - if($template && empty($parent) && empty($options['id']) && count($template->parentTemplates) === 1) { - $parentTemplates = $template->parentTemplates(); - if($parentTemplates->count()) { - $numParents = $this->pages->count("template=$parentTemplates, include=all"); - if($numParents === 1) { - $parent = $this->pages->get("template=$parentTemplates"); + if($template && empty($parent) && empty($options['id']) && !$this->pages->loader()->isLoading()) { + if(count($template->parentTemplates) === 1) { + $parentTemplates = $template->parentTemplates(); + if($parentTemplates->count()) { + $numParents = $this->pages->count("template=$parentTemplates, include=all"); + if($numParents === 1) { + $parent = $this->pages->get("template=$parentTemplates"); + if(!$parent->id) $parent = null; + } } - } + } } // detect class from template diff --git a/wire/core/PagesLoader.php b/wire/core/PagesLoader.php index 64db8e35..bc42b2ef 100644 --- a/wire/core/PagesLoader.php +++ b/wire/core/PagesLoader.php @@ -64,6 +64,14 @@ class PagesLoader extends Wire { */ protected $debug = false; + /** + * Are we currenty loading pages? + * + * @var bool + * + */ + protected $loading = false; + /** * Page instance ID * @@ -415,7 +423,9 @@ class PagesLoader extends Wire { $cachePages = false; $template = null; $templatesByID = array(); + $loading = $this->loading; + if(!$loading) $this->loading = true; foreach($pagesIDs as $id => $templateID) { if(isset($templatesByID[$templateID])) { $template = $templatesByID[$templateID]; @@ -429,6 +439,7 @@ class PagesLoader extends Wire { $pages->add($page); } + if(!$loading) $this->loading = false; $pages->setDuplicateChecking(true); if(count($pagesIDs)) $pages->_lazy(true); unset($template, $templatesByID); @@ -934,6 +945,7 @@ class PagesLoader extends Wire { /** @var WireDatabasePDO $database */ $database = $this->wire('database'); $idsByTemplate = array(); + $loading = $this->loading; if(is_array($template)) { // $template property specifies an array of options @@ -1045,6 +1057,7 @@ class PagesLoader extends Wire { return $pages; } + if(!$loading) $this->loading = true; if(count($idsByTemplate)) { // ok @@ -1205,11 +1218,16 @@ class PagesLoader extends Wire { $template = null; } - if($options['getOne']) return count($loaded) ? reset($loaded) : $this->pages->newNullPage(); + if($options['getOne']) { + if(!$loading) $this->loading = false; + return count($loaded) ? reset($loaded) : $this->pages->newNullPage(); + } + $pages = $this->pages->newPageArray($options); $pages->setDuplicateChecking(false); $pages->import($loaded); $pages->setDuplicateChecking(true); + if(!$loading) $this->loading = false; // debug mode only if($this->debug) { @@ -1230,6 +1248,7 @@ class PagesLoader extends Wire { } } } + return $pages; } @@ -1921,5 +1940,17 @@ class PagesLoader extends Wire { public function getLastPageFinder() { return $this->lastPageFinder; } + + /** + * Are we currently loading pages? + * + * @return bool + * @since 3.0.195 + * + * + */ + public function isLoading() { + return $this->loading; + } } \ No newline at end of file