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 = $this->modules->get("InputfieldSelect");
$field->attr('name', 'collapsed'); $field->attr('name', 'collapsed');
$field->label = $this->_('Presentation'); $field->label = $this->_('Presentation');
$field->icon = 'eye-slash';
$field->description = $this->_("How should this field be displayed in the editor?"); $field->description = $this->_("How should this field be displayed in the editor?");
$field->addOption(self::collapsedNo, $this->_('Open')); $field->addOption(self::collapsedNo, $this->_('Open'));
$field->addOption(self::collapsedNever, $this->_('Open + Cannot be closed')); $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->label = sprintf($this->_('Column width (%d%%)'), $value);
$field->icon = 'arrows-h'; $field->icon = 'arrows-h';
$field->attr('id+name', 'columnWidth'); $field->attr('id+name', 'columnWidth');
$field->addClass('columnWidthInput');
$field->attr('type', 'text'); $field->attr('type', 'text');
$field->attr('maxlength', 4); $field->attr('maxlength', 4);
$field->attr('size', 4); $field->attr('size', 4);

View File

@@ -57,7 +57,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
'item_toggle' => "<i class='toggle-icon fa fa-fw fa-angle-down' data-to='fa-angle-down fa-angle-right'></i>", 'item_toggle' => "<i class='toggle-icon fa fa-fw fa-angle-down' data-to='fa-angle-down fa-angle-right'></i>",
// ALSO: // ALSO:
// InputfieldAnything => array( any of the properties above to override on a per-Inputifeld basis) // InputfieldAnything => array( any of the properties above to override on a per-Inputifeld basis)
); );
static protected $markup = array(); static protected $markup = array();
@@ -81,15 +81,17 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
'item_required_if' => 'InputfieldStateRequiredIf' 'item_required_if' => 'InputfieldStateRequiredIf'
// ALSO: // ALSO:
// InputfieldAnything => array( any of the properties above to override on a per-Inputifeld basis) // InputfieldAnything => array( any of the properties above to override on a per-Inputifeld basis)
); );
static protected $classes = array(); static protected $classes = array();
/** /**
* Instance of InputfieldsArray, if this Inputfield contains child Inputfields * 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. * 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"); if(self::debugPropertyAccess) throw new WireException("Access of attribute or setting: $key");
$value = $this->wire($key); $value = $this->wire($key);
if($value) return $value; if($value) return $value;
if($key == 'children') return $this->children; if($key === 'children') return $this->children();
if(($value = parent::get($key)) !== null) return $value; if(($value = parent::get($key)) !== null) return $value;
return null; return null;
} }
@@ -194,7 +196,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
* *
*/ */
public function __get($key) { public function __get($key) {
if($key == 'children') return $this->children; if($key === 'children') return $this->children();
$value = parent::get($key); $value = parent::get($key);
if(is_null($value)) $value = $this->getChildByName($key); if(is_null($value)) $value = $this->getChildByName($key);
return $value; return $value;
@@ -219,7 +221,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
} else if(is_array($item)) { } else if(is_array($item)) {
$this->importArray($item); $this->importArray($item);
} else { } else {
$this->children->add($item); $this->children()->add($item);
$item->setParent($this); $item->setParent($this);
} }
return $this; return $this;
@@ -326,7 +328,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/ */
public function prepend(Inputfield $item) { public function prepend(Inputfield $item) {
$item->setParent($this); $item->setParent($this);
$this->children->prepend($item); $this->children()->prepend($item);
return $this; return $this;
} }
@@ -341,7 +343,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/ */
public function append(Inputfield $item) { public function append(Inputfield $item) {
$item->setParent($this); $item->setParent($this);
$this->children->append($item); $this->children()->append($item);
return $this; return $this;
} }
@@ -356,9 +358,9 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
* *
*/ */
public function insertBefore(Inputfield $item, Inputfield $existingItem) { public function insertBefore(Inputfield $item, Inputfield $existingItem) {
if($this->children->has($existingItem)) { if($this->children()->has($existingItem)) {
$item->setParent($this); $item->setParent($this);
$this->children->insertBefore($item, $existingItem); $this->children()->insertBefore($item, $existingItem);
} else if($this->getChildByName($existingItem->attr('name')) && $existingItem->parent) { } else if($this->getChildByName($existingItem->attr('name')) && $existingItem->parent) {
$existingItem->parent->insertBefore($item, $existingItem); $existingItem->parent->insertBefore($item, $existingItem);
} }
@@ -376,9 +378,9 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
* *
*/ */
public function insertAfter(Inputfield $item, Inputfield $existingItem) { public function insertAfter(Inputfield $item, Inputfield $existingItem) {
if($this->children->has($existingItem)) { if($this->children()->has($existingItem)) {
$item->setParent($this); $item->setParent($this);
$this->children->insertAfter($item, $existingItem); $this->children()->insertAfter($item, $existingItem);
} else if($this->getChildByName($existingItem->attr('name')) && $existingItem->parent) { } else if($this->getChildByName($existingItem->attr('name')) && $existingItem->parent) {
$existingItem->parent->insertAfter($item, $existingItem); $existingItem->parent->insertAfter($item, $existingItem);
} }
@@ -401,8 +403,8 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
$item = $this->getChildByName($item); $item = $this->getChildByName($item);
if(!$item) return $this; if(!$item) return $this;
} }
if($this->children->has($item)) { if($this->children()->has($item)) {
$this->children->remove($item); $this->children()->remove($item);
} if($this->getChildByName($item->attr('name')) && $item->parent) { } if($this->getChildByName($item->attr('name')) && $item->parent) {
$item->parent->remove($item); $item->parent->remove($item);
} }
@@ -415,12 +417,12 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/ */
protected function preRenderChildren() { protected function preRenderChildren() {
if($this->getSetting('InputfieldWrapper_isPreRendered')) return $this->children; if($this->getSetting('InputfieldWrapper_isPreRendered')) return $this->children();
$children = $this->wire(new InputfieldWrapper()); $children = $this->wire(new InputfieldWrapper());
$wrappers = array($children); $wrappers = array($children);
foreach($this->children as $inputfield) { foreach($this->children() as $inputfield) {
$wrapper = end($wrappers); $wrapper = end($wrappers);
@@ -816,7 +818,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
* *
*/ */
public function ___renderValue() { public function ___renderValue() {
if(!count($this->children)) return ''; if(!count($this->children())) return '';
$this->addClass('InputfieldRenderValueMode'); $this->addClass('InputfieldRenderValueMode');
$this->set('renderValueMode', true); $this->set('renderValueMode', true);
$out = $this->render(); $out = $this->render();
@@ -972,7 +974,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
if(!$this->children) return $this; if(!$this->children) return $this;
foreach($this->children as $key => $child) { foreach($this->children() as $key => $child) {
/** @var Inputfield $child */ /** @var Inputfield $child */
// skip over the field if it is not processable // skip over the field if it is not processable
@@ -1069,7 +1071,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/ */
public function isEmpty() { public function isEmpty() {
$empty = true; $empty = true;
foreach($this->children as $child) { foreach($this->children() as $child) {
if(!$child->isEmpty()) { if(!$child->isEmpty()) {
$empty = false; $empty = false;
break; break;
@@ -1092,7 +1094,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
public function getEmpty($required = true) { public function getEmpty($required = true) {
$a = array(); $a = array();
static $n = 0; static $n = 0;
foreach($this->children as $child) { foreach($this->children() as $child) {
if($child instanceof InputfieldWrapper) { if($child instanceof InputfieldWrapper) {
$a = array_merge($a, $child->getEmpty($required)); $a = array_merge($a, $child->getEmpty($required));
} else { } else {
@@ -1120,7 +1122,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/ */
public function getErrors($clear = false) { public function getErrors($clear = false) {
$errors = parent::getErrors($clear); $errors = parent::getErrors($clear);
foreach($this->children as $key => $child) { foreach($this->children() as $key => $child) {
foreach($child->getErrors($clear) as $e) { foreach($child->getErrors($clear) as $e) {
$label = $child->getSetting('label'); $label = $child->getSetting('label');
$msg = $label ? $label : $child->attr('name'); $msg = $label ? $label : $child->attr('name');
@@ -1164,22 +1166,23 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/ */
public function child($name = '', $recursive = true) { public function child($name = '', $recursive = true) {
$child = null; $child = null;
$children = $this->children();
if(!$this->children->count()) { if(!$children->count()) {
// no child possible // no child possible
} else if(empty($name)) { } else if(empty($name)) {
// first child // first child
$child = $this->children->first(); $child = $children->first();
} else if(is_int($name)) { } else if(is_int($name)) {
// number index // number index
$child = $this->children->eq($name); $child = $children->eq($name);
} else if($this->wire('sanitizer')->name($name) === $name) { } else if($this->wire('sanitizer')->name($name) === $name) {
// child by name // child by name
$wrappers = array(); $wrappers = array();
foreach($this->children as $f) { foreach($children as $f) {
if($f->getAttribute('name') === $name) { if($f->getAttribute('name') === $name) {
$child = $f; $child = $f;
break; break;
@@ -1196,7 +1199,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
} else if(Selectors::stringHasSelector($name)) { } else if(Selectors::stringHasSelector($name)) {
// first child matching selector string // first child matching selector string
$child = $this->children("$name, limit=1")->first(); $child = $children->find("$name, limit=1")->first();
} }
return $child; return $child;
@@ -1228,7 +1231,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/ */
public function _getDelayedChildren($clear = false) { public function _getDelayedChildren($clear = false) {
$a = $this->delayedChildren; $a = $this->delayedChildren;
foreach($this->children as $child) { foreach($this->children() as $child) {
if(!$child instanceof InputfieldWrapper) continue; if(!$child instanceof InputfieldWrapper) continue;
$a = array_merge($a, $child->_getDelayedChildren($clear)); $a = array_merge($a, $child->_getDelayedChildren($clear));
} }
@@ -1246,7 +1249,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
* *
*/ */
public function find($selector) { 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) { public function getChildByName($name) {
if(!strlen($name)) return null; if(!strlen($name)) return null;
$inputfield = null; $inputfield = null;
foreach($this->children as $child) { foreach($this->children() as $child) {
if($child->getAttribute('name') === $name) { if($child->getAttribute('name') === $name) {
$inputfield = $child; $inputfield = $child;
} else if($child instanceof InputfieldWrapper) { } else if($child instanceof InputfieldWrapper) {
@@ -1288,7 +1291,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
* *
*/ */
public function getIterator() { public function getIterator() {
return $this->children; return $this->children();
} }
/** /**
@@ -1300,7 +1303,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
* *
*/ */
public function count() { 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 * #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 * @return InputfieldWrapper
* *
*/ */
public function getAll() { public function getAll(array $options = array()) {
$all = $this->wire(new InputfieldsArray()); $all = $this->wire(new InputfieldsArray());
foreach($this->children as $child) { foreach($this->children() as $child) {
if($child instanceof InputfieldWrapper) { 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); $all->add($c);
} }
} else { } else {
@@ -1337,7 +1343,8 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
* *
*/ */
public function setTrackChanges($trackChanges = true) { 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); return parent::setTrackChanges($trackChanges);
} }
@@ -1351,7 +1358,8 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
* *
*/ */
public function resetTrackChanges($trackChanges = true) { 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); return parent::resetTrackChanges($trackChanges);
} }