Jump to content

Custom Fieldtype and Inputfield module can only edit but not add value


KarlvonKarton
 Share

Recommended Posts

The custom fieldtype / inputfield can not add a record to the inputfield table...
But if I add a record manually to the inputfield table, then I can edit the value with the module.  As long as I don't empty the inputfield via the module and save, because then the record is deleted by the module.

I don't know what I'm doing wrong.  Can someone help me?

FieldtypeZabun.module :

<?php namespace ProcessWire;

class FieldtypeZabun  extends Fieldtype implements ConfigurableModule {
	
	public static function getModuleInfo() {

		return array(
			'title' => 'Zabun Fieldtype', 
			'version' => 1, 
			'summary' => 'Zabun selector.',
			'installs' => 'InputfieldZabun',
			'icon' => 'home', 
			);
	}

    public function __construct() {
		parent::__construct();
    }


	public function sanitizeValue(Page $page, Field $field, $value) {
        return $value;  
    }

	public function ___wakeupValue(Page $page, Field $field, $value){
        return $value;
    }

	public function ___sleepValue(Page $page, Field $field, $value) {
		return $value;
	}
	

	public function getInputfield(Page $page, Field $field) {
		$inputfield = $this->modules->get('InputfieldZabun');
		$inputfield->set('zabundatabase', $this->get('zabundatabase'));
		$inputfield->set('zabunlocation', $this->get('zabunlocation'));
		$inputfield->set('zabunusername', $this->get('zabunusername'));
		$inputfield->set('zabunpass', $this->get('zabunpass'));
		return $inputfield; 
	}

	public function getCompatibleFieldtypes(Field $field){
        return null;
    }

	public function getDatabaseSchema(Field $field) {
		$schema = parent::getDatabaseSchema($field);
		$schema['data'] = "TEXT NOT NULL DEFAULT ''";
		$schema['keys']['data'] = 'FULLTEXT KEY `data` (`data`)';

		return $schema;
	}
	
	
	public static function getModuleConfigInputfields(array $data) {

		$inputfields = new InputfieldWrapper();
		$f = wire('modules')->get('InputfieldText');
		$f->attr('name', 'zabundatabase'); 
		$f->label = __('Zabun database'); 
		$f->icon = 'database';
		$f->description = 'Zabun database';
		$f->attr('value', isset($data['zabundatabase']) ? $data['zabundatabase'] : '');
		$inputfields->add($f);

		$f = wire('modules')->get('InputfieldText');
		$f->attr('name', 'zabunlocation'); 
		$f->label = __('Zabun location'); 
		$f->icon = 'server';
		$f->description = 'Zabun location';
		$f->attr('value', isset($data['zabunlocation']) ? $data['zabunlocation'] : '');
		$inputfields->add($f);

		$f = wire('modules')->get('InputfieldText');
		$f->attr('name', 'zabunusername'); 
		$f->label = __('Zabun username'); 
		$f->icon = 'user';
		$f->description = 'Zabun username';
		$f->attr('value', isset($data['zabunusername']) ? $data['zabunusername'] : '');
		$inputfields->add($f);

		$f = wire('modules')->get('InputfieldText');
		$f->attr('name', 'zabunpass'); 
		$f->label = __('Zabun password'); 
		$f->icon = 'key';
		$f->description = 'Zabun password';
		$f->attr('value', isset($data['zabunpass']) ? $data['zabunpass'] : '');
		$inputfields->add($f);

		return $inputfields;
	}
	
	
}

InputfieldZabun.module :

<?php namespace ProcessWire;

class InputfieldZabun extends Inputfield {

    public static function getModuleInfo() {
		return array(
			'title' => 'Zabun Inputfield',
			'version' => 1,
			'summary' => "Provides input for the Zabun Fieldtype",
			'requires' => 'FieldtypeZabun',
			'icon' => 'home', 
			);
	}

    public function __construct() {
		//require_once(dirname(__FILE__) . '/ZabunClass.php');
		$this->zabundatabase = $this->get('zabundatabase');
        $this->zabunlocation = $this->get('zabunlocation');
        $this->zabunusername = $this->get('zabunusername');
        $this->zabunpass = $this->get('zabunpass');
		parent::__construct();
	}


	public function renderReady(Inputfield $parent = null, $renderValueMode = false) {
		$url = 'https://unpkg.com/vue@3.2.22/dist/vue.global.prod.js';
		$this->config->scripts->add($url);
		return parent::renderReady($parent, $renderValueMode);
	}

    public function ___render() {

		$this->setAttribute('type', 'text');
		$this->setAttribute('id', 'zabunselector');
		$this->setAttribute('name', 'zabunselector');
		
		$out = '';
        $out .= '<div id="App"><input '.$this->getAttributesString().' /></div>';

        return $out;
    }

	public function ___processInput(WireInputData $input) {
		parent::___processInput($input);
		$this->value = $input['zabunselector'];
        return $this;
	}
}

 

Link to comment
Share on other sites

I suspected that the route I was crawling was not the correct one, so I ended up changing the course... with more succes ?

field type

<?php namespace ProcessWire;

class FieldtypeTextZabun extends FieldtypeText implements ConfigurableModule {
    
    public static function getModuleInfo() {
		return array(
			'title' => 'Zabun FieldtypeText', 
			'version' => 1, 
			'summary' => 'Zabun selector.',
			'installs' => 'InputfieldTextZabun',
			'icon' => 'home', 
			);
	}

    public function getInputfield(Page $page, Field $field) {
		$inputfield = $this->modules->get('InputfieldTextZabun');
        /*$inputfield->set('zabundatabase', $this->get('zabundatabase'));
		$inputfield->set('zabunlocation', $this->get('zabunlocation'));
		$inputfield->set('zabunusername', $this->get('zabunusername'));
		$inputfield->set('zabunpass', $this->get('zabunpass'));*/
		return $inputfield; 
	}

    public static function getModuleConfigInputfields(array $data) {

		$inputfields = new InputfieldWrapper();
		$f = wire('modules')->get('InputfieldText');
		$f->attr('name', 'zabundatabase'); 
		$f->label = __('Zabun database'); 
		$f->icon = 'database';
		$f->description = 'Zabun database';
		$f->attr('value', isset($data['zabundatabase']) ? $data['zabundatabase'] : '');
		$inputfields->add($f);

		$f = wire('modules')->get('InputfieldText');
		$f->attr('name', 'zabunlocation'); 
		$f->label = __('Zabun location'); 
		$f->icon = 'server';
		$f->description = 'Zabun location';
		$f->attr('value', isset($data['zabunlocation']) ? $data['zabunlocation'] : '');
		$inputfields->add($f);

		$f = wire('modules')->get('InputfieldText');
		$f->attr('name', 'zabunusername'); 
		$f->label = __('Zabun username'); 
		$f->icon = 'user';
		$f->description = 'Zabun username';
		$f->attr('value', isset($data['zabunusername']) ? $data['zabunusername'] : '');
		$inputfields->add($f);

		$f = wire('modules')->get('InputfieldText');
		$f->attr('name', 'zabunpass'); 
		$f->label = __('Zabun password'); 
		$f->icon = 'key';
		$f->description = 'Zabun password';
		$f->attr('value', isset($data['zabunpass']) ? $data['zabunpass'] : '');
		$inputfields->add($f);

		return $inputfields;
	}

    public function ___install() {
        // add dependencies
        copy( dirname(__FILE__).'/src/ZabunApi-master.php', $_SERVER['DOCUMENT_ROOT'].'/zabunapi.php' );
    }
    public function ___uninstall() {
        // remove dependencies
        unlink( $_SERVER['DOCUMENT_ROOT'].'/zabunapi.php' );
    }
}

 

input field:

 

<?php namespace ProcessWire;

class InputfieldTextZabun extends InputfieldText {

    public static function getModuleInfo() {
		return array(
			'title' => 'Zabun InputfieldText',
			'version' => 1,
			'summary' => "Provides input for the Zabun Fieldtype",
			'requires' => 'FieldtypeTextZabun',
			'icon' => 'home', 
			);
	}

    public function __construct() {
		parent::__construct();
	}

    public function renderReady(Inputfield $parent = null, $renderValueMode = false) {
		$url = 'https://unpkg.com/vue@3.2.22/dist/vue.global.prod.js';
		$this->config->scripts->add($url);
		return parent::renderReady($parent, $renderValueMode);
	}

    public function ___render() {

        // $database = $this->zabundatabase;
		
		$out = '<div id="App">';
        $out .= '<input '.$this->getAttributesString().' />';
        $out .= '<div v-html="avar"></div>';
        $out .= '</div>';

        return $out;
    }

}

Thanks for reading my ramblings!

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...