Robin S Posted 10 hours ago Posted 10 hours ago In a Fieldtype module you can define config inputfields for a field that uses the Fieldtype in the ___getConfigInputfields($field) method. The saved values become properties of the Field object ($field). But how do you set a default value for a config inputfield in a way that doesn't prevent the user from clearing the default and leaving the inputfield empty? It's the first time I've had this need and I can't work out how to do it. I looked at the core Fieldtype modules but I couldn't find one that applies a default value that is allowed to be cleared. A simple module to explain it more clearly: <?php namespace ProcessWire; class FieldtypeTest extends FieldtypeText { public static function getModuleInfo() { return array( 'title' => 'Test Fieldtype', 'summary' => 'Test default config values.', 'version' => '0.1.0', ); } /** * Config inputfields * * @param Field $field * @return InputfieldWrapper */ public function ___getConfigInputfields(Field $field) { $inputfields = parent::___getConfigInputfields($field); /* * How can I set a default value for "greeting" in a way that * doesn't prevent the user from saving an empty value? * * The below doesn't work because it will apply the default value * when the user has attempted to clear it. * * */ if(is_null($field->get('greeting'))) $field->set('greeting', 'Hello'); $f = $this->wire()->modules->get('InputfieldText'); $f->label = 'Greeting'; $f->attr('name', 'greeting'); $f->attr('value', $field->get('greeting')); $inputfields->add($f); return $inputfields; } } Does anyone know the right way to do this? Maybe @ryan has a suggestion? Thanks in advance.
BitPoet Posted 8 hours ago Posted 8 hours ago Off the top of my head: In your module's install method, hook after modules::install() in that hook method, get the module config with modules::getConfig(), set your default value and do a modules::saveConfig() 2
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now