mirror of
https://github.com/processwire/processwire.git
synced 2025-08-12 17:54:44 +02:00
Add a new hookable Fieldtype::___getFieldSetups() that lets any Fieldtype specify different configurations available when creating new fields. These configurations will be selectable when creating a new field in the admin, or when setting the $field->type = 'FieldtypeName.setupName";
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
* @method void changeTypeReady(Saveable $item, Fieldtype $fromType, Fieldtype $toType) #pw-hooker
|
||||
* @method bool|Field clone(Field $item, $name = '') Clone a field and return it or return false on fail.
|
||||
* @method array getTags($getFieldNames = false) Get tags for all fields (3.0.179+) #pw-advanced
|
||||
* @method bool applySetupName(Field $field, $setupName = '')
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -361,6 +362,14 @@ class Fields extends WireSaveableItems {
|
||||
if(!parent::___save($item)) return false;
|
||||
if($isNew) $item->type->createField($item);
|
||||
|
||||
$setupName = $item->setSetupName();
|
||||
if($setupName || $isNew) {
|
||||
if($this->applySetupName($item, $setupName)) {
|
||||
$item->setSetupName('');
|
||||
parent::___save($item);
|
||||
}
|
||||
}
|
||||
|
||||
if($item->flags & Field::flagGlobal) {
|
||||
// make sure that all template fieldgroups contain this field and add to any that don't.
|
||||
foreach($this->wire()->templates as $template) {
|
||||
@@ -1421,6 +1430,41 @@ class Fields extends WireSaveableItems {
|
||||
return $getCount ? $count : $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup a new field using predefined setup name(s) from the Field’s fieldtype
|
||||
*
|
||||
* If no setupName is provided then this method doesn’t do anything, but hooks to it might.
|
||||
*
|
||||
* @param Field $field Newly created field
|
||||
* @param string $setupName Setup name to apply
|
||||
* @return bool True if setup was appled, false if not
|
||||
* @since 3.0.213
|
||||
*
|
||||
*/
|
||||
protected function ___applySetupName(Field $field, $setupName = '') {
|
||||
|
||||
$setups = $field->type->getFieldSetups();
|
||||
$setup = isset($setups[$setupName]) ? $setups[$setupName] : null;
|
||||
|
||||
if(!$setup) return false;
|
||||
|
||||
$title = isset($setup['title']) ? $setup['title'] : $setupName;
|
||||
$func = isset($setup['setup']) ? $setup['setup'] : null;
|
||||
|
||||
foreach($setup as $property => $value) {
|
||||
if($property === 'title' || $property === 'setup') continue;
|
||||
$field->set($property, $value);
|
||||
}
|
||||
|
||||
if($func && is_callable($func)) {
|
||||
$func($field);
|
||||
}
|
||||
|
||||
$this->message("Applied setup: $title", Notice::debug | Notice::noGroup);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return field ID for given value (Field, field name, field ID) or 0 if none
|
||||
*
|
||||
|
Reference in New Issue
Block a user