From e610d863e785fcfeedcb26317495967285b61c2b Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 9 Jul 2021 15:09:51 -0400 Subject: [PATCH] Update for multi-instance to cover the case where someone might do something like `new User()` (or Permission, Role, Language) and have its template instance be from the wrong ProcessWire instance. This ensures that gets corrected as soon as the new object is wired to the instance. But we also have to account for the case where they never end up wiring the object for whatever reason, which is why there is some redundant code between construct() and wired() --- wire/core/Permission.php | 15 +++++++++++++-- wire/core/Role.php | 12 ++++++++++-- wire/core/User.php | 19 +++++++++++++++---- wire/modules/LanguageSupport/Language.php | 14 +++++++++++--- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/wire/core/Permission.php b/wire/core/Permission.php index 637a30e8..b9767fa3 100644 --- a/wire/core/Permission.php +++ b/wire/core/Permission.php @@ -52,8 +52,19 @@ class Permission extends Page { */ public function __construct(Template $tpl = null) { parent::__construct($tpl); - if(is_null($tpl)) $this->template = $this->wire('templates')->get('permission'); - $this->parent = $this->wire('pages')->get($this->wire('config')->permissionsPageID); + if(!$tpl) $this->template = $this->wire()->templates->get('permission'); + $this->_parent_id = $this->wire()->config->permissionsPageID; + } + + /** + * Wired to API + * + */ + public function wired() { + parent::wired(); + $template = $this->wire()->templates->get('permission'); + if($template !== $this->template && (!$this->template || $this->template->name === 'permission')) $this->template = $template; + $this->_parent_id = $this->wire()->config->permissionsPageID; } /** diff --git a/wire/core/Role.php b/wire/core/Role.php index 806f26f0..4b659088 100644 --- a/wire/core/Role.php +++ b/wire/core/Role.php @@ -30,8 +30,16 @@ class Role extends Page { */ public function __construct(Template $tpl = null) { parent::__construct($tpl); - if(is_null($tpl)) $this->template = $this->getPredefinedTemplate(); - $this->parent = $this->getPredefinedParent(); + } + + /** + * Wired to API + * + */ + public function wired() { + parent::wired(); + if(!$this->template) $this->template = $this->getPredefinedTemplate(); + if(!$this->_parent) $this->setParent($this->getPredefinedParent()); } /** diff --git a/wire/core/User.php b/wire/core/User.php index ec210892..ba3a63a3 100644 --- a/wire/core/User.php +++ b/wire/core/User.php @@ -45,11 +45,22 @@ class User extends Page { * */ public function __construct(Template $tpl = null) { + if(!$tpl) $this->template = $this->wire()->templates->get('user'); + $this->_parent_id = $this->wire()->config->usersPageID; parent::__construct($tpl); - if(is_null($tpl)) { - $this->template = $this->wire('templates')->get('user'); - } - if(!$this->parent_id) $this->set('parent_id', $this->wire('config')->usersPageID); + } + + /** + * Wired to API + * + * @throws WireException + * + */ + public function wired() { + parent::wired(); + $template = $this->wire()->templates->get('user'); + if($template !== $this->template && (!$this->template || $this->template->name === 'user')) $this->template = $template; + $this->_parent_id = $this->wire()->config->usersPageID; } /** diff --git a/wire/modules/LanguageSupport/Language.php b/wire/modules/LanguageSupport/Language.php index 74ff9dcd..f45fed3b 100644 --- a/wire/modules/LanguageSupport/Language.php +++ b/wire/modules/LanguageSupport/Language.php @@ -30,9 +30,17 @@ class Language extends Page { */ public function __construct(Template $tpl = null) { parent::__construct($tpl); - if(is_null($tpl)) { - $this->template = $this->wire('templates')->get('language'); - } + if(!$tpl) $this->template = $this->wire()->templates->get('language'); + } + + /** + * Wired to API + * + */ + public function wired() { + parent::wired(); + $template = $this->wire()->templates->get('language'); + if($template !== $this->template && (!$this->template || $this->template->name === 'language')) $this->template = $template; } /**