mirror of
https://github.com/processwire/processwire.git
synced 2025-08-08 15:57:01 +02:00
Add new InputfieldWrapper getByField() and getByProperty() methods
This commit is contained in:
@@ -1566,6 +1566,64 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
|
|||||||
return $inputfield;
|
return $inputfield;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Inputfield by Field (hasField)
|
||||||
|
*
|
||||||
|
* This is useful in cases where the input name may differ from the Field name
|
||||||
|
* that it represents, and you only know the field name. Applies only to
|
||||||
|
* Inputfields connected with a Page and Field (i.e. used for page editing).
|
||||||
|
*
|
||||||
|
* #pw-group-retrieval-and-traversal
|
||||||
|
*
|
||||||
|
* @param Field|string|int $field
|
||||||
|
* @return Inputfield|InputfieldWrapper|null
|
||||||
|
* @since 3.0.239
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function getByField($field) {
|
||||||
|
if(!$field instanceof Field) $field = $this->wire()->fields->get($field);
|
||||||
|
return $this->getByProperty('hasField', $field);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Inputfield by some other non-attribute property or setting
|
||||||
|
*
|
||||||
|
* #pw-group-retrieval-and-traversal
|
||||||
|
*
|
||||||
|
* @param string $property
|
||||||
|
* @param mixed $value
|
||||||
|
* @param bool $getAll Get array of all matching Inputfields rather than just first? (default=false)
|
||||||
|
* @return Inputfield|InputfieldWrapper|null|array
|
||||||
|
* @since 3.0.239
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function getByProperty($property, $value, $getAll = false) {
|
||||||
|
$inputfield = null;
|
||||||
|
$value = (string) $value;
|
||||||
|
$a = array();
|
||||||
|
|
||||||
|
foreach($this->children() as $child) {
|
||||||
|
/** @var Inputfield $child */
|
||||||
|
if((string) $child->getSetting($property) === $value) {
|
||||||
|
$inputfield = $child;
|
||||||
|
} else if($child instanceof InputfieldWrapper) {
|
||||||
|
if($getAll) {
|
||||||
|
$a = array_merge($a, $child->getByProperty($property, $value, true));
|
||||||
|
} else {
|
||||||
|
$inputfield = $child->getByProperty($property, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if($inputfield) {
|
||||||
|
if($getAll) {
|
||||||
|
$a[] = $inputfield;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $getAll ? $a : $inputfield;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get value of Inputfield by name
|
* Get value of Inputfield by name
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user