Jump to content

[solved] How do you set a default Fieldtype config value?


Recommended Posts

Posted

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.

Posted

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()
  • Like 3
Posted

Thanks for the suggestion @BitPoet. But these config settings are for fields using the Fieldtype (and stored in the "fields" table) rather than part of the module config (stored in the "modules" table).

I did get to the bottom of why the problem exists and how to work around it:

You would think that when setting a default value you could check if the config setting is null (or not part of the data array for the Field) - if it's null then it means it's never been set, and if it's an empty string then it means the user has deliberately cleared the value. But saving a field goes via WireSaveableItems::save() -> WireSaveableItems::encodeData() -> wireEncodeJSON(), and the last is used without $allowEmpty set to true. So any empty value is removed from the saved data and therefore the setting will be null rather than an empty string.

I saw a solution in FieldtypeText::saveFieldReady(). In your Fieldtype module you do this to check if a Field is newly created and if so set any default config values:

/**
 * Hook called when field is about to be saved
 *
 * @param Field $field
 * @since 3.0.212
 */
public function ___saveFieldReady(Field $field) {
    parent::___saveFieldReady($field);
    // If the field is new (it has no ID) and is being saved in ProcessField...
    if(!$field->id && $this->wire()->page->process == 'ProcessField') {
        // Set some default field config values
        $field->set('myFieldtypeConfigSetting', 'foo');
        // ...
    }
}

This method is only available in PW >= 3.0.212 so I guess prior to that you only had the unappealing option of creating an additional autoload module bundled with your Fieldtype module and hooking Fields::save().

  • Like 3
  • Robin S changed the title to [solved] How do you set a default Fieldtype config value?

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...