1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 17:54:44 +02:00

Add support for Fieldtypes to specify that a Field should use a custom class that extends the Field class, rather than always using the "Field" class.

This commit is contained in:
Ryan Cramer
2019-11-12 11:06:02 -05:00
parent 4e4b3afdcb
commit bbddcf1ca0
7 changed files with 112 additions and 17 deletions

View File

@@ -155,6 +155,45 @@ class Fields extends WireSaveableItems {
return $this->wire(new Field());
}
/**
* Make an item and populate with given data
*
* @param array $a Associative array of data to populate
* @return Saveable|Wire
* @throws WireException
* @since 3.0.147
*
*/
public function makeItem(array $a = array()) {
if(empty($a['type'])) return parent::makeItem($a);
/** @var Fieldtypes $fieldtypes */
$fieldtypes = $this->wire('fieldtypes');
if(!$fieldtypes) return parent::makeItem($a);
/** @var Fieldtype $fieldtype */
$fieldtype = $fieldtypes->get($a['type']);
if(!$fieldtype) return parent::makeItem($a);
$class = $fieldtype->getFieldClass($a);
if(empty($class) || $class === 'Field') return parent::makeItem($a);
if(strpos($class, "\\") === false) $class = wireClassName($class, true);
if(!class_exists($class)) return parent::makeItem($a);
$field = new $class();
$this->wire($field);
foreach($a as $key => $value) {
$field->$key = $value;
}
$field->resetTrackChanges(true);
return $field;
}
/**
* Per WireSaveableItems interface, return all available Field instances
*