mirror of
https://github.com/processwire/processwire.git
synced 2025-08-09 00:06:55 +02:00
Update RepeaterPage class for secondary detection of 'forField' when page exists in unexpected location
This commit is contained in:
@@ -685,7 +685,10 @@ class PagesEditor extends Wire {
|
|||||||
try {
|
try {
|
||||||
$field->type->savePageField($page, $field);
|
$field->type->savePageField($page, $field);
|
||||||
} catch(\Exception $e) {
|
} 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);
|
$this->trackException($e, true, $error);
|
||||||
if($this->wire('database')->inTransaction()) throw $e;
|
if($this->wire('database')->inTransaction()) throw $e;
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
/**
|
/**
|
||||||
* RepeaterPage represents an individual repeater page item
|
* 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
|
* https://processwire.com
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@@ -13,12 +13,16 @@ class RepeaterPage extends Page {
|
|||||||
/**
|
/**
|
||||||
* Page instance that has this repeater item on it
|
* Page instance that has this repeater item on it
|
||||||
*
|
*
|
||||||
|
* @var Page|null
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
protected $forPage = null;
|
protected $forPage = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Field instance that contains this repeater item
|
* Field instance that contains this repeater item
|
||||||
*
|
*
|
||||||
|
* @var Field|null
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
protected $forField = null;
|
protected $forField = null;
|
||||||
|
|
||||||
@@ -96,23 +100,39 @@ class RepeaterPage extends Page {
|
|||||||
/**
|
/**
|
||||||
* Return the field that this repeater item belongs to
|
* Return the field that this repeater item belongs to
|
||||||
*
|
*
|
||||||
* @return Field
|
* 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|null
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function getForField() {
|
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();
|
$grandparent = $this->parent()->parent();
|
||||||
$grandparentName = $grandparent->name;
|
$grandparentName = $grandparent->name;
|
||||||
$prefix = FieldtypeRepeater::fieldPageNamePrefix; // for-field-
|
$prefix = FieldtypeRepeater::fieldPageNamePrefix; // for-field-
|
||||||
|
$forField = null;
|
||||||
|
$fields = $this->wire('fields'); /** @var Fields $fields */
|
||||||
|
|
||||||
if(strpos($grandparentName, $prefix) === 0) {
|
if(strpos($grandparentName, $prefix) === 0) {
|
||||||
// determine field from grandparent name in format: for-field-1234
|
// determine field from grandparent name in format: for-field-1234
|
||||||
$forID = (int) substr($grandparentName, strlen($prefix));
|
$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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user