From 92ef3bbbc3ec33450cf506d43fc827224c656fce Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 31 Jan 2020 11:19:55 -0500 Subject: [PATCH] Update RepeaterPage class for secondary detection of 'forField' when page exists in unexpected location --- wire/core/PagesEditor.php | 5 ++- .../FieldtypeRepeater/RepeaterPage.php | 34 +++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/wire/core/PagesEditor.php b/wire/core/PagesEditor.php index 5f7cb533..4c401a35 100644 --- a/wire/core/PagesEditor.php +++ b/wire/core/PagesEditor.php @@ -685,7 +685,10 @@ class PagesEditor extends Wire { try { $field->type->savePageField($page, $field); } catch(\Exception $e) { - $error = sprintf($this->_('Error saving field "%s"'), $name) . ' - ' . $e->getMessage(); + $label = $field->getLabel(); + $message = $e->getMessage(); + if(strpos($message, $label) !== false) $label = $name; + $error = sprintf($this->_('Error saving field "%s"'), $label) . ' — ' . $message; $this->trackException($e, true, $error); if($this->wire('database')->inTransaction()) throw $e; } diff --git a/wire/modules/Fieldtype/FieldtypeRepeater/RepeaterPage.php b/wire/modules/Fieldtype/FieldtypeRepeater/RepeaterPage.php index 986cd270..48d32e50 100644 --- a/wire/modules/Fieldtype/FieldtypeRepeater/RepeaterPage.php +++ b/wire/modules/Fieldtype/FieldtypeRepeater/RepeaterPage.php @@ -3,7 +3,7 @@ /** * RepeaterPage represents an individual repeater page item * - * ProcessWire 3.x, Copyright 2016 by Ryan Cramer + * ProcessWire 3.x, Copyright 2020 by Ryan Cramer * https://processwire.com * */ @@ -12,13 +12,17 @@ class RepeaterPage extends Page { /** * Page instance that has this repeater item on it + * + * @var Page|null * */ protected $forPage = null; /** * Field instance that contains this repeater item - * + * + * @var Field|null + * */ protected $forField = null; @@ -95,24 +99,40 @@ class RepeaterPage extends Page { /** * Return the field that this repeater item belongs to + * + * Returns null only if $forField has not been set and cannot be determined from any other + * properties of this page. Meaning null return value is not likely. * - * @return Field + * @return Field|null * */ public function getForField() { - if(!is_null($this->forField)) return $this->forField; + if($this->forField !== null) return $this->forField; + // auto-detect forField from its location $grandparent = $this->parent()->parent(); $grandparentName = $grandparent->name; $prefix = FieldtypeRepeater::fieldPageNamePrefix; // for-field- + $forField = null; + $fields = $this->wire('fields'); /** @var Fields $fields */ if(strpos($grandparentName, $prefix) === 0) { // determine field from grandparent name in format: for-field-1234 $forID = (int) substr($grandparentName, strlen($prefix)); - $this->forField = $this->wire('fields')->get($forID); + $forField = $fields->get($forID); + } else { + // page must exist somewhere outside the expected location, so use template + // name as a secondary way to identify what the field is + $template = $this->template; + if($template && strpos($template->name, FieldtypeRepeater::templateNamePrefix) === 0) { + list(,$fieldName) = explode(FieldtypeRepeater::templateNamePrefix, $template->name, 2); + $forField = $fields->get($fieldName); + } } - - return $this->forField; + + if($forField) $this->forField = $forField; + + return $forField; } /**