From 2e7db9c40f2148b57dca4a7332ea7261c132290b Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Mon, 1 Apr 2019 11:49:10 -0400 Subject: [PATCH] Fix issue processwire/processwire-issues#778 --- wire/core/Templates.php | 15 +++++++++---- .../ProcessPageAdd/ProcessPageAdd.module | 22 +++++++++++-------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/wire/core/Templates.php b/wire/core/Templates.php index f8c4e419..101f5462 100644 --- a/wire/core/Templates.php +++ b/wire/core/Templates.php @@ -223,6 +223,7 @@ class Templates extends WireSaveableItems { public function ___clone(Saveable $item, $name = '') { $original = $item; + /** @var Template $item */ $item = clone $item; if($item->flags & Template::flagSystem) { @@ -479,6 +480,7 @@ class Templates extends WireSaveableItems { $foundParent = null; $foundParents = $getAll ? $this->wire('pages')->newPageArray() : null; + $foundParentQty = 0; if($template->noShortcut || !count($template->parentTemplates)) return $foundParents; if($template->noParents == -1) { @@ -508,11 +510,13 @@ class Templates extends WireSaveableItems { if(!$numParentPages) continue; if($getAll) { + // build list of all parents (will check access outside loop) if($numParentPages) $foundParents->add($parentPages); continue; } else if($numParentPages > 1) { - // multiple possible parents - $parentPage = $this->wire('pages')->newNullPage(); + // multiple possible parents, we can early-exit + $foundParentQty += $numParentPages; + break; } else { // one possible parent $parentPage = $parentPages->first(); @@ -529,11 +533,12 @@ class Templates extends WireSaveableItems { } } + if($parentPage && $parentPage->id) $foundParentQty++; $foundParent = $parentPage; - break; + if($foundParentQty > 1) break; } - if($checkAccess && $foundParents && $foundParents->count()) { + if($checkAccess && $getAll && $foundParents && $foundParents->count()) { $p = $this->wire('pages')->newPage(array('template' => $template)); foreach($foundParents as $parentPage) { if(!$parentPage->addable($p)) $foundParents->remove($parentPage); @@ -541,6 +546,8 @@ class Templates extends WireSaveableItems { } if($getAll) return $foundParents; + if($foundParentQty > 1) return $this->wire('pages')->newNullPage(); + return $foundParent; } diff --git a/wire/modules/Process/ProcessPageAdd/ProcessPageAdd.module b/wire/modules/Process/ProcessPageAdd/ProcessPageAdd.module index 53d82a23..98ef8e86 100644 --- a/wire/modules/Process/ProcessPageAdd/ProcessPageAdd.module +++ b/wire/modules/Process/ProcessPageAdd/ProcessPageAdd.module @@ -286,10 +286,14 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit if(!$templateID) throw new WireException("No template specified"); $template = $this->templates->get($templateID); if(!$template) throw new WireException("Unknown template"); - $parentTemplates = $template->parentTemplates; - $parentTemplate = $this->wire('templates')->get(reset($parentTemplates)); - if(!$parentTemplate) throw new WireException("Unable to locate parent template " . reset($parentTemplates)); - $parents = $this->wire('pages')->find("template=$parentTemplate, include=hidden, limit=500, sort=name"); + $parentTemplates = new TemplatesArray();; + foreach($template->parentTemplates as $templateID) { + $t = $this->templates->get((int) $templateID); + if($t) $parentTemplates->add($t); + } + if(!count($parentTemplates)) throw new WireException("No parent templates defined for $template->name"); + $parentTemplateIDs = $parentTemplates->implode('|', 'id'); + $parents = $this->wire('pages')->find("templates_id=$parentTemplateIDs, include=hidden, limit=500, sort=name"); if(!count($parents)) throw new WireException("No usable parents match this template"); if(count($parents) == 1) { $url = "./?parent_id=" . $parents->first()->id; @@ -298,7 +302,6 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit } $templateLabel = $this->getTemplateLabel($template); - $parentTemplateLabel = $this->getTemplateLabel($parentTemplate); $form = $this->wire('modules')->get('InputfieldForm'); $form->description = $this->getTemplateLabel($template); $form->method = 'get'; @@ -316,7 +319,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit $f->attr('name', 'parent_id'); $f->attr('id', 'select_parent_id'); $f->label = sprintf($this->_('Where do you want to add the new %s?'), "\"$templateLabel\""); - $f->description = sprintf($this->_('Please select a parent %s page below:'), "\"$parentTemplateLabel\""); + $f->description = sprintf($this->_('Please select a parent %s page below:'), ''); // Select parent label // you can omit the '%s' (no longer used) $options = array(); foreach($parents as $parent) { @@ -381,8 +384,9 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit $url = $item['url']; if(strpos($url, 'parent_id') === false) $url .= "&parent_id=$parent->id"; $out .= "
  • "; - foreach($parent->parents()->and($parent) as $p) { - if($p->id == 1) continue; + $parentParents = $parent->parents()->and($parent); + foreach($parentParents as $p) { + if($p->id == 1 && $parentParents->count() > 1) continue; $out .= "$p->title"; } $out .= "
  • "; @@ -479,7 +483,7 @@ class ProcessPageAdd extends Process implements ConfigurableModule, WirePageEdit if(!$this->parent_id && count($this->predefinedParents)) { $this->parent_id = $this->predefinedParents->first()->id; } - + if(!$this->parent_id) return $this->renderChooseTemplate(); $this->parent = $this->pages->get((int) $this->parent_id);