1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 18:24:57 +02:00

Add support for custom type-specific Inputfield classes and attributes to be added via markup array in InputfieldWrapper

This commit is contained in:
Ryan Cramer
2019-10-25 10:06:18 -04:00
parent a3251d8571
commit 1d0dc756b6

View File

@@ -424,6 +424,34 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
return $children;
}
/**
* Cached class parents indexed by Inputfield class name
*
* @var array
*
*/
static protected $classParents = array();
/**
* Get array of parent Inputfield classes for given Inputfield (excluding the base Inputfield class)
*
* @param Inputfield|string $inputfield
* @return array
*
*/
protected function classParents($inputfield) {
$p = &self::$classParents;
$c = is_object($inputfield) ? $inputfield->className() : $inputfield;
if(!isset($p[$c])) {
$p[$c] = array();
foreach(wireClassParents($inputfield) as $parentClass) {
if(strpos($parentClass, 'Inputfield') !== 0 || $parentClass === 'Inputfield') break;
$p[$c][] = $parentClass;
}
}
return $p[$c];
}
/**
* Render this Inputfield and the output of its children.
*
@@ -483,6 +511,24 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
if($collapsed == Inputfield::collapsedHidden) continue;
if($collapsed == Inputfield::collapsedNoLocked || $collapsed == Inputfield::collapsedYesLocked) $renderValueMode = true;
// allow adding custom classes and/or attributes at runtime (since 3.0.143)
$classParents = $this->classParents($inputfield);
$classParents[] = $inputfieldClass;
foreach($classParents as $classParent) {
if(!isset($_markup[$classParent])) continue;
$markupParent = $_markup[$classParent];
foreach(array('class', 'wrapClass', 'headerClass', 'contentClass') as $classKey) {
if(!empty($markupParent[$classKey])) $inputfield->addClass($markupParent[$classKey], $classKey);
}
foreach(array('attr', 'wrapAttr') as $attrKey) {
if(!empty($markupParent[$attrKey]) && is_array($markupParent[$attrKey])) {
foreach($markupParent[$attrKey] as $k => $v) {
$inputfield->$attrKey($k, $v);
}
}
}
}
$ffOut = $this->renderInputfield($inputfield, $renderValueMode);
if(!strlen($ffOut)) continue;