Jump to content


Photo

extending inputfields


  • Please log in to reply
5 replies to this topic

#1 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 301 posts
  • 13

  • LocationBratislava

Posted 08 September 2011 - 01:58 AM

It's possible to extend an inputfield without replacing the entire module?
I want to add some functionalities to all the inputfields but i don't want to recreate all, i just want to create a single module that add this function to the existing fields.

#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3380

  • LocationAtlanta, GA

Posted 08 September 2011 - 08:41 AM

It sounds like you want to add a hook. Can you provide more context? I think once I know what you are trying to do, I should be able to tell you how to do it.

#3 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 301 posts
  • 13

  • LocationBratislava

Posted 08 September 2011 - 09:06 AM

I'm building a form builder based on the original inputfileds of PW. Now i'm at the point in which i have the same form in the admin and in the frontend, changing a field in the admin my form change in the same way, same for the post action. Now i want to add the automatic validation for every field and for making this i need to add a select near every inputfield in the admin for choose the right validator.

#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3380

  • LocationAtlanta, GA

Posted 08 September 2011 - 09:59 AM

I think I understand. I should say though that PW's fields and inputs are designed for administrative use rather than front end use. If I'm building something for the front end, I might still use PW pages to store the data, but I'm handling the input on my own and populating fields into page objects. That's what I recommend because you'll be able to do whatever you want without limitation and use PW's API in the manner it was built for.  But if the Inputfields are suiting your need, then here's what you'd do to extend all of them. In the example, we add a hook to the field rendering (to add a select box with each one) and another hook to the input processing.

This example module should be pasted into a file called /site/modules/InputfieldHookTest.module

<?php

class InputfieldHookTest extends WireData implements Module {

	public static function getModuleInfo() {	
		return array(
			'title' => 'Inputfield Hook Test',
			'version' => 100,
			'summary' => 'Just a test.',
			'singular' => true,
			'autoload' => true,
			);
	}
	
	public function init() {
		$this->addHookAfter('Inputfield::render', $this, 'render'); 
		$this->addHookAfter('Inputfield::processInput', $this, 'processInput'); 
	}
	
	public function render(HookEvent $event) {
		$inputfield = $event->object;
		
		// optional, but better to limit to those you want
		if(!$inputfield instanceof InputfieldText && !$inputfield instanceof InputfieldTextarea) return; 
		
		$name = $inputfield->name;  
		$event->return .= "<p><select name='test_$name'><option></option><option>test</option><option>test2</option></select></p>";
	}
	
	public function processInput(HookEvent $event) {
		$inputfield = $event->object; 
		$name = $inputfield->name; 
		$post = $event->arguments[0];
		$value = $post["test_$name"]; 
		if($value) $this->message("You selected '$value' for '$name'"); 
	}

}


#5 Soma

Soma

    Hero Member

  • Moderators
  • 3,397 posts
  • 1931

  • LocationSH, Switzerland

Posted 08 September 2011 - 01:38 PM

that kinda was what I was asking for here http://processwire.c...opic,465.0.html :)

I already was this far to have the right hook on inputfields and all but missed the obvious, that playing with str_replace I replaced itself each recursion... was late night. 

Thanks for the example that helped and made some things clearer.

@somartist | modules created | support me, flattr my work flattr.com


#6 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 301 posts
  • 13

  • LocationBratislava

Posted 08 September 2011 - 01:55 PM

Thanks so much




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users