bluellyr Posted February 21, 2019 Share Posted February 21, 2019 Hi, I want to extend the fields configuration and create a new tab to put them and also to use those configuration fields on the template context. Where can I find examples how to do this? Thank you Link to comment Share on other sites More sharing options...
Autofahrn Posted February 21, 2019 Share Posted February 21, 2019 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. 2 Link to comment Share on other sites More sharing options...
bluellyr Posted February 25, 2019 Author Share Posted February 25, 2019 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 More sharing options...
bluellyr Posted February 25, 2019 Author Share Posted February 25, 2019 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 More sharing options...
bluellyr Posted February 26, 2019 Author Share Posted February 26, 2019 Hi, I was missing the processInput hook. Thank you. Link to comment Share on other sites More sharing options...
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