Jump to content

Extending fields configuration and template context configuration


bluellyr
 Share

Recommended Posts

To have configuration fields for your fieldtype (displayed on the config tab) you'll need to implement ___getConfigInputfields(Field $field) in your Fieldtype class. To specify allowed overwrites you'll need to implement ___getConfigAllowContext($field) in your Inputfield class.

As examples you may look at wire/modules/Fieldtype/FieldtypeTextarea.module and wire/modules/Inputfield/InputfieldTextarea.module.

Edit: Please note that FieldtypeTextarea.module actually pulls additional configuration field from the InputfieldTextarea, so this is not a simple example for creating the config inputfields. A more straight forward example for ___getConfigInputfields would be wire/modules/Fieldtype/FieldtypeText.module.

  • Like 2
Link to comment
Share on other sites

To be more precise, I need a module that adds some extra configurations fields in all fields types, I do not want to create a new field type.

I am sorry, I am a newbie and at the moment I am lost on all the steps that I need to do to implement this
The research I have made in the forums I could not find a clean code example or a recipe for this.

I found the module "ImageExtra" that adds extra configuration fields to the "InputfieldImage".

The following code is as far I could go, but the field is not showing:

<?php namespace ProcessWire;

class HubExtendFieldsConfigurations extends WireData implements Module {

	public static function getModuleInfo() {
		return array(
			'title' => 'Extend Fields Configurations Options.',
			'version' => 100,
			'summary' => 'This module extends the fields configurations options.',
			'requires'  => 'ProcessWire>=2.7.2, PHP>=5.4.4',
			'singular' => true,
			'autoload' => true,
			'icon' => 'sliders',
			);
	}

	public function init() {
	}

	public function ready() {
		$admin = $this->config->urls->admin;
    	$backend = strpos($this->page->url, $admin);

		if (in_array($this->page->name, array('edit', 'field', 'module', 'users')) || $backend === false) {
				$this->addHookAfter('InputfieldText::getConfigInputfields', $this, 'addExtraFields');
		}
	}

	public function addExtraFields(HookEvent $event) {
    if (!$event->object instanceof InputfieldText) return;

		/** @var InputfieldWrapper $fields */
		$fields = $this->wire(new InputfieldWrapper());

		/** @var InputfieldFieldset $fieldset */
		$fieldset = $this->modules->get('InputfieldFieldset');
		$fieldset->label = $this->_('Hub extended configurations');
		$fieldset->attr('name', 'hub_extended_configurations');
		$fieldset->icon = 'sliders';

		/** @var InputfieldText $field */
		$field = $this->modules->get("InputfieldText");
		$field->label = $this->_('Field version');
		$field->description = $this->_('The field version.');
		$field->notes = 'Global field version.';
		$field->icon = 'sliders';
		$field->attr('name', 'hub_extended_field_version');
		$field->attr('value', $event->object->getSetting('hub_extended_field_version'));
		$fieldset->append($field);

		$fields->append($fieldset);

		return $fields;
	}
}

Thank you, all the best.

Link to comment
Share on other sites

Hi,

using the "ImageExtra" example I could show a extra config field on the "InputfieldText" but I am not able to save the value of the field when saving the field.
I am using the following code:

<?php namespace ProcessWire;

class HubExtendFieldsConfigurations extends WireData implements Module {

	public static function getModuleInfo() {
		return array(
			'title' => 'Extend Fields Configurations Options.',
			'version' => 100,
			'summary' => 'This module extends the fields configurations options.',
			'requires'  => 'ProcessWire>=3.0.0, PHP>=5.4.4',
			'singular' => true,
			'autoload' => true,
			'icon' => 'sliders',
			);
	}

	public function init() {
	}

	public function ready() {
		$admin = $this->config->urls->admin;
    $backend = strpos($this->page->url, $admin);

		if (in_array($this->page->name, array('edit', 'field', 'module', 'users')) || $backend === false) {
				// Add more InputfieldText config fields
				$this->addHookAfter('InputfieldText::getConfigInputfields', $this, 'addExtraFields');
				// Add more InputfieldText config fields to template context
				$this->addHookMethod('InputfieldText::getConfigAllowContext', $this, 'addConfigAllowContext');
		}
	}

	public function addExtraFields(HookEvent $event) {
    if (!$event->object instanceof InputfieldText) return;

		/** @var InputfieldFieldset $fieldset */
		$fieldset = $this->modules->get('InputfieldFieldset');
    	$fieldset->label = $this->_('Hub extended configurations');
    	$fieldset->collapsed = Inputfield::collapsedYes;
    	$fieldset->description = $this->_('Extended field configurations.');
		$fieldset->attr('name', 'hub_extended_configurations');
		$fieldset->icon = 'sliders';

		/** @var InputfieldText $field */
		$field = $this->modules->get("InputfieldText");
		$field->label = $this->_('Field version');
		$field->description = $this->_('The field version.');
		$field->notes = 'Global field version.';
		$field->icon = 'sliders';
		$field->attr('name', 'hub_extended_field_version');
		$field->attr('value', $event->object->getSetting('hub_extended_field_version'));
		$fieldset->append($field);

		$event->return->children->add($fieldset);
	}

	public function addConfigAllowContext(HookEvent $event){
		$allowables = ['hub_extended_configurations','hub_extended_field_version'];
		$event->return = array_merge($event->return, $allowables);
	}

}

 

It is needed to implement a hook to process and save the field value?

Thank you

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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