1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-08 07:47:00 +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:
Ryan Cramer
2023-02-24 10:15:05 -05:00
parent 91f4b7cd6e
commit b3913a8791
4 changed files with 109 additions and 1 deletions

View File

@@ -251,6 +251,18 @@ class Field extends WireData implements Saveable, Exportable {
*/
protected $tagList = null;
/**
* Setup name to apply when field is saved
*
* Set via $field->type = 'FieldtypeName.setupName';
* or applySetup() method
*
* @var string
* @since 3.0.213
*
*/
protected $setupName = '';
/**
* True if lowercase tables should be enforce, false if not (null = unset). Cached from $config
*
@@ -680,6 +692,11 @@ class Field extends WireData implements Saveable, Exportable {
// good for you
} else if(is_string($type)) {
if(strpos($type, '.')) {
// FieldtypeName.setupName
list($type, $setupName) = explode('.', $type, 2);
$this->setSetupName($setupName);
}
$typeStr = $type;
$type = $this->wire()->fieldtypes->get($type);
if(!$type) {
@@ -1546,6 +1563,18 @@ class Field extends WireData implements Saveable, Exportable {
return $url;
}
/**
* Set setup name from Fieldtype to apply when field is saved
*
* @param string $setupName Setup name or omit to instead get the current value
* @return string Returns current value
*
*/
public function setSetupName($setupName = null) {
if($setupName !== null) $this->setupName = $setupName;
return $this->setupName;
}
/**
* debugInfo PHP 5.6+ magic method
*

View File

@@ -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 Fields fieldtype
*
* If no setupName is provided then this method doesnt 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
*

View File

@@ -50,6 +50,7 @@
* @method void saveFieldReady(Field $field)
* @method void install()
* @method void uninstall()
* @method array getFieldSetups()
*
* @property bool $_exportMode True when Fieldtype is exporting config data, false otherwise. #pw-internal
* @property string $name Name of Fieldtype module. #pw-group-other
@@ -177,6 +178,40 @@ abstract class Fieldtype extends WireData implements Module {
return $inputfield;
}
/**
* Get predefined setups for newly created fields of this type
*
* ~~~~~
* // Example that returns 2 setup options "foo" and "bar"
* return array(
* 'foo' => array(
* 'title' => 'Foo',
* 'any_setting' => 'any_value',
* 'setup' => function(Field $field) {
* // optional code to setup $field when 'foo' is selected
* }
* ),
* 'bar' => array(
* 'title' => 'Bar',
* 'hello' => 'world', // example of any setting
* 'setup' => function(Field $field) {
* // optional code to setup $field when 'bar' is selected
* }
* )
* );
* ~~~~~
*
* #pw-internal
* #pw-hooker
*
* @return array
* @since 3.0.213
*
*/
public function ___getFieldSetups() {
return array();
}
/**
* Get any Inputfields used for configuration of this Fieldtype.
*

View File

@@ -585,7 +585,7 @@ class PageValues extends Wire {
} else {
// name being set while page is loading
if($charset === 'UTF8' && strpos($value, 'xn-') === 0) {
if($charset === 'UTF8' && strpos("$value", 'xn-') === 0) {
// allow decode of UTF8 name while page is loading
$value = $sanitizer->pageName($value, Sanitizer::toUTF8);
} else {