1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 16:26:59 +02:00

Update InputfieldWrapper so that all direct references to $this->children (InputfieldsArray) are routed through the children() method, enabling descending modules to have more control over what are considered children.

This commit is contained in:
Ryan Cramer
2020-11-25 14:05:42 -05:00
parent 580da37a2f
commit 4c57f528f6
2 changed files with 46 additions and 36 deletions

View File

@@ -1444,6 +1444,7 @@ abstract class Inputfield extends WireData implements Module {
$field = $this->modules->get("InputfieldSelect");
$field->attr('name', 'collapsed');
$field->label = $this->_('Presentation');
$field->icon = 'eye-slash';
$field->description = $this->_("How should this field be displayed in the editor?");
$field->addOption(self::collapsedNo, $this->_('Open'));
$field->addOption(self::collapsedNever, $this->_('Open + Cannot be closed'));
@@ -1485,6 +1486,7 @@ abstract class Inputfield extends WireData implements Module {
$field->label = sprintf($this->_('Column width (%d%%)'), $value);
$field->icon = 'arrows-h';
$field->attr('id+name', 'columnWidth');
$field->addClass('columnWidthInput');
$field->attr('type', 'text');
$field->attr('maxlength', 4);
$field->attr('size', 4);

View File

@@ -88,8 +88,10 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
/**
* Instance of InputfieldsArray, if this Inputfield contains child Inputfields
*
* @var InputfieldsArray
*
*/
protected $children = null;
protected $children;
/**
* Array of Inputfields that had their processing delayed by dependencies.
@@ -179,7 +181,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
if(self::debugPropertyAccess) throw new WireException("Access of attribute or setting: $key");
$value = $this->wire($key);
if($value) return $value;
if($key == 'children') return $this->children;
if($key === 'children') return $this->children();
if(($value = parent::get($key)) !== null) return $value;
return null;
}
@@ -194,7 +196,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*
*/
public function __get($key) {
if($key == 'children') return $this->children;
if($key === 'children') return $this->children();
$value = parent::get($key);
if(is_null($value)) $value = $this->getChildByName($key);
return $value;
@@ -219,7 +221,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
} else if(is_array($item)) {
$this->importArray($item);
} else {
$this->children->add($item);
$this->children()->add($item);
$item->setParent($this);
}
return $this;
@@ -326,7 +328,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/
public function prepend(Inputfield $item) {
$item->setParent($this);
$this->children->prepend($item);
$this->children()->prepend($item);
return $this;
}
@@ -341,7 +343,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/
public function append(Inputfield $item) {
$item->setParent($this);
$this->children->append($item);
$this->children()->append($item);
return $this;
}
@@ -356,9 +358,9 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*
*/
public function insertBefore(Inputfield $item, Inputfield $existingItem) {
if($this->children->has($existingItem)) {
if($this->children()->has($existingItem)) {
$item->setParent($this);
$this->children->insertBefore($item, $existingItem);
$this->children()->insertBefore($item, $existingItem);
} else if($this->getChildByName($existingItem->attr('name')) && $existingItem->parent) {
$existingItem->parent->insertBefore($item, $existingItem);
}
@@ -376,9 +378,9 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*
*/
public function insertAfter(Inputfield $item, Inputfield $existingItem) {
if($this->children->has($existingItem)) {
if($this->children()->has($existingItem)) {
$item->setParent($this);
$this->children->insertAfter($item, $existingItem);
$this->children()->insertAfter($item, $existingItem);
} else if($this->getChildByName($existingItem->attr('name')) && $existingItem->parent) {
$existingItem->parent->insertAfter($item, $existingItem);
}
@@ -401,8 +403,8 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
$item = $this->getChildByName($item);
if(!$item) return $this;
}
if($this->children->has($item)) {
$this->children->remove($item);
if($this->children()->has($item)) {
$this->children()->remove($item);
} if($this->getChildByName($item->attr('name')) && $item->parent) {
$item->parent->remove($item);
}
@@ -415,12 +417,12 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/
protected function preRenderChildren() {
if($this->getSetting('InputfieldWrapper_isPreRendered')) return $this->children;
if($this->getSetting('InputfieldWrapper_isPreRendered')) return $this->children();
$children = $this->wire(new InputfieldWrapper());
$wrappers = array($children);
foreach($this->children as $inputfield) {
foreach($this->children() as $inputfield) {
$wrapper = end($wrappers);
@@ -816,7 +818,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*
*/
public function ___renderValue() {
if(!count($this->children)) return '';
if(!count($this->children())) return '';
$this->addClass('InputfieldRenderValueMode');
$this->set('renderValueMode', true);
$out = $this->render();
@@ -972,7 +974,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
if(!$this->children) return $this;
foreach($this->children as $key => $child) {
foreach($this->children() as $key => $child) {
/** @var Inputfield $child */
// skip over the field if it is not processable
@@ -1069,7 +1071,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/
public function isEmpty() {
$empty = true;
foreach($this->children as $child) {
foreach($this->children() as $child) {
if(!$child->isEmpty()) {
$empty = false;
break;
@@ -1092,7 +1094,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
public function getEmpty($required = true) {
$a = array();
static $n = 0;
foreach($this->children as $child) {
foreach($this->children() as $child) {
if($child instanceof InputfieldWrapper) {
$a = array_merge($a, $child->getEmpty($required));
} else {
@@ -1120,7 +1122,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/
public function getErrors($clear = false) {
$errors = parent::getErrors($clear);
foreach($this->children as $key => $child) {
foreach($this->children() as $key => $child) {
foreach($child->getErrors($clear) as $e) {
$label = $child->getSetting('label');
$msg = $label ? $label : $child->attr('name');
@@ -1164,22 +1166,23 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/
public function child($name = '', $recursive = true) {
$child = null;
$children = $this->children();
if(!$this->children->count()) {
if(!$children->count()) {
// no child possible
} else if(empty($name)) {
// first child
$child = $this->children->first();
$child = $children->first();
} else if(is_int($name)) {
// number index
$child = $this->children->eq($name);
$child = $children->eq($name);
} else if($this->wire('sanitizer')->name($name) === $name) {
// child by name
$wrappers = array();
foreach($this->children as $f) {
foreach($children as $f) {
if($f->getAttribute('name') === $name) {
$child = $f;
break;
@@ -1196,7 +1199,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
} else if(Selectors::stringHasSelector($name)) {
// first child matching selector string
$child = $this->children("$name, limit=1")->first();
$child = $children->find("$name, limit=1")->first();
}
return $child;
@@ -1228,7 +1231,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/
public function _getDelayedChildren($clear = false) {
$a = $this->delayedChildren;
foreach($this->children as $child) {
foreach($this->children() as $child) {
if(!$child instanceof InputfieldWrapper) continue;
$a = array_merge($a, $child->_getDelayedChildren($clear));
}
@@ -1246,7 +1249,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*
*/
public function find($selector) {
return $this->children->find($selector);
return $this->children()->find($selector);
}
/**
@@ -1266,7 +1269,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
public function getChildByName($name) {
if(!strlen($name)) return null;
$inputfield = null;
foreach($this->children as $child) {
foreach($this->children() as $child) {
if($child->getAttribute('name') === $name) {
$inputfield = $child;
} else if($child instanceof InputfieldWrapper) {
@@ -1288,7 +1291,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*
*/
public function getIterator() {
return $this->children;
return $this->children();
}
/**
@@ -1300,7 +1303,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*
*/
public function count() {
return count($this->children);
return count($this->children());
}
/**
@@ -1310,14 +1313,17 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*
* #pw-group-retrieval-and-traversal
*
* @param array $options Options to modify behavior (3.0.169+)
* - `getWrappers` (bool): Also include InputfieldWrapper objects? (default=false) 3.0.169+
* @return InputfieldWrapper
*
*/
public function getAll() {
public function getAll(array $options = array()) {
$all = $this->wire(new InputfieldsArray());
foreach($this->children as $child) {
foreach($this->children() as $child) {
if($child instanceof InputfieldWrapper) {
foreach($child->getAll() as $c) {
if(!empty($options['getWrappers'])) $all->add($child);
foreach($child->getAll($options) as $c) {
$all->add($c);
}
} else {
@@ -1337,7 +1343,8 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*
*/
public function setTrackChanges($trackChanges = true) {
if(count($this->children)) foreach($this->children as $child) $child->setTrackChanges($trackChanges);
$children = $this->children();
if(count($children)) foreach($children as $child) $child->setTrackChanges($trackChanges);
return parent::setTrackChanges($trackChanges);
}
@@ -1351,7 +1358,8 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*
*/
public function resetTrackChanges($trackChanges = true) {
if(count($this->children)) foreach($this->children as $child) $child->resetTrackChanges($trackChanges);
$children = $this->children();
if(count($children)) foreach($children as $child) $child->resetTrackChanges($trackChanges);
return parent::resetTrackChanges($trackChanges);
}