Marc Posted January 15, 2017 Posted January 15, 2017 I'm messing around with the ProcessHello example module and I'm trying to add a page selector the the module's configuration page. I have added the input field but the value is not saved to the database and I'm trying to find out why that is. Here's my ProcessHello.config.php: class ProcessHelloConfig extends ModuleConfig { public function __construct() { $this->add(array( // Text field: greeting array( 'name' => 'greeting', // name of field 'type' => 'text', // type of field (any Inputfield module name) 'label' => $this->_('Hello Greeting'), // field label 'description' => $this->_('What would you like to say to people using this module?'), 'required' => true, 'value' => $this->_('A very happy hello world to you.'), // default value ), // Radio buttons: greetingType array( 'name' => 'greetingType', 'type' => 'radios', 'label' => $this->_('Greeting Type'), 'options' => array( // options array of value => label 'message' => $this->_('Message'), 'warning' => $this->_('Warning'), 'error' => $this->_('Error'), ), 'value' => 'warning', // default value 'optionColumns' => 1, // make options display on one line 'notes' => $this->_('Choose wisely'), // like description but appears under field ), // My custom test field array( 'name' => 'myTest', 'type' => 'page', 'label' => $this->_('Test'), 'labelFieldName' => 'title', 'inputfield' => 'InputfieldSelect', 'parent_id' => 1 ) )); } } I am probably missing something obvious here. The custom field is displayed but it won't save anything. Other field types like 'text' do save their value.
Robin S Posted January 15, 2017 Posted January 15, 2017 The inputfield value is not saved correctly because config you are trying creates an error: PHP Notice: Object of class ProcessWire\Page could not be converted to int in ...\modules\Process\ProcessModule\ProcessModule.module:1287 So it seems that the inputfield you have created is returning a page object when what is expected is a page id. One solution is to create the options you want to make available in the inputfield: class ProcessHelloConfig extends ModuleConfig { public function __construct() { $items = $this->pages->find("parent=1"); $page_options = []; foreach($items as $item) { $page_options[$item->id] = $item->title; } $this->add(array( // Text field: greeting array( 'name' => 'greeting', // name of field 'type' => 'text', // type of field (any Inputfield module name) 'label' => $this->_('Hello Greeting'), // field label 'description' => $this->_('What would you like to say to people using this module?'), 'required' => true, 'value' => $this->_('A very happy hello world to you.'), // default value ), // Radio buttons: greetingType array( 'name' => 'greetingType', 'type' => 'radios', 'label' => $this->_('Greeting Type'), 'options' => array( // options array of value => label 'message' => $this->_('Message'), 'warning' => $this->_('Warning'), 'error' => $this->_('Error'), ), 'value' => 'warning', // default value 'optionColumns' => 1, // make options display on one line 'notes' => $this->_('Choose wisely'), // like description but appears under field ), // My custom test field array( 'name' => 'myTest', 'type' => 'select', 'label' => $this->_('Test'), 'options' => $page_options, ), )); } } BTW, if you want to allow the user to select any page from the tree you can use the Page List Select inputfield: // My custom test field array( 'name' => 'myTest', 'type' => 'PageListSelect', 'label' => $this->_('Test') ) 1 1
Marc Posted January 16, 2017 Author Posted January 16, 2017 On 1/15/2017 at 10:32 PM, Robin S said: The inputfield value is not saved correctly because config you are trying creates an error: PHP Notice: Object of class ProcessWire\Page could not be converted to int in ...\modules\Process\ProcessModule\ProcessModule.module:1287 Where does this error show up? I have debug mode activated but this error is not in my logs and it is not displayed on screen. I arrived at the same conclusion of creating the options for the field manually and have basically the exact same code as you posted above, however I like your alternative approach much better and it is what I was after in the first place: Quote // My custom test field array( 'name' => 'myTest', 'type' => 'PageListSelect', 'label' => $this->_('Test') ) So thank you for that
Robin S Posted January 16, 2017 Posted January 16, 2017 8 minutes ago, Marc said: Where does this error show up? I have debug mode activated but this error is not in my logs and it is not displayed on screen. The error is visible if you install Tracy Debugger. 2
bernhard Posted July 6, 2022 Posted July 6, 2022 I've hit the same problem. I can add a config inputfield of type "text", but when I change it to "page" the selected page is not saved: <?php namespace ProcessWire; /** * @author Bernhard Baumrock, 06.07.2022 * @license Licensed under MIT * @link https://www.baumrock.com */ class FieldtypePageTest extends Fieldtype { public static function getModuleInfo() { return [ 'title' => 'PageTest', 'version' => '1.0.0', 'icon' => 'link', 'requires' => [], ]; } public function init() { parent::init(); } /** FIELDTYPE METHODS */ /** * Return the fields required to configure an instance of FieldtypeText * * @param Field $field * @return InputfieldWrapper * */ public function ___getConfigInputfields(Field $field) { $inputfields = parent::___getConfigInputfields($field); // this does not save the value $inputfields->add([ // change type to text and it works 'type' => 'page', 'name' => 'sourcepage', 'label' => 'Source Page', 'icon' => 'sitemap', 'derefAsPage' => FieldtypePage::derefAsPageOrFalse, 'inputfield' => 'InputfieldPageListSelect', 'findPagesSelector' => 'id>0', 'labelFieldName' => 'title', 'value' => $field->get('sourcepage'), ]); // this field saves properly $inputfields->add([ 'type' => 'text', 'name' => 'myfieldname', 'value' => $field->get('myfieldname'), ]); return $inputfields; } /** * Sanitize value for storage * * @param Page $page * @param Field $field * @param string $value * @return string */ public function sanitizeValue(Page $page, Field $field, $value) { return $value; } /** HELPER METHODS */ } The field "myfieldname" saves correctly, but the page field does not. Any help would be highly appreciated ?
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