Jump to content

Saving a custom text field within a Page field


Recommended Posts

Hello,

I am developing an Inputfield Module for a Page Field that has the user interact with a modified InputfieldSelector. I have added buttons on InputfieldSelector to modify a hidden HTML textfield that I have appended to the module rendering. I would like to save the textfield's contents to the database. My question is, how would I do that, given that the module is of type Page?

Let me know if clarification is needed.

Thanks.

Link to comment
Share on other sites

Short answer: you can't. Fields that save to the db each come with a defined database schema. In your case that would be FieldtypePage. As you can see, its db schema only saves integers , i.e. the ID of the pages selected in the page field and only has the one db column in the table it creates. Your best bet is probably to extend FieldtypePage and create your own schema. There's an example here by @Raymond Geerts. Have a look at the method getDatabaseSchema(). Of course in your case your db type will be something else, maybe varchar(255) or text...

 

Welcome to the forums btw.. :-)

  • Like 1
Link to comment
Share on other sites

7 minutes ago, kongondo said:

Short answer: you can't. Fields that save to the db each come with a defined database schema. In your case that would be FieldtypePage. As you can see, its db schema only saves integers , i.e. the ID of the pages selected in the page field and only has the one db column in the table it creates. Your best bet is probably to extend FieldtypePage and create your own schema. There's an example here by @Raymond Geerts. Have a look at the method getDatabaseSchema(). Of course in your case your db type will be something else, maybe varchar(255) or text...

 

Welcome to the forums btw.. :-)

Hi kongondo, thanks for the reply,

The data I would like to save is actually a list of the IDs that were selected from the InputfieldSelector. I was originally thinking of putting the selected items in a hidden textfield and save the IDs in the database that way. But if there is another way of saving a list of IDs, can you share?

Thanks.

Link to comment
Share on other sites

FieldtypePage will simply save all the page-id's which are in $_POST[$fieldname]. So you could use a hidden field for single values, but for multi values a <select> is probably easier to setup. But in the end ProcessWire won't care how the values got send, as long as the POST data are in the correct format.

Link to comment
Share on other sites

I think what you are getting at is a way to "field cache" the results of the selector field so that you don't have to evaluate it at runtime, and thereby allow that field to be used in selectors etc;

You can just add a hook to the saveReady and then evaluate the selector result, and save the pipe delimited ids into that field; very simple and effective way of field caching and i use it a lot.

The only snafu is that the beauty of the selector field is it's inherent dynamism, meaning you don't need to interact with the page anymore once the selector field is configured (new pages would 'auto-add' to the results).

But if you field cache those results, they will be frozen at the time you save that page. You'd need another element which would be like a cron job that updates the field once in a while or when other pages are saved.

Another option would be to wirecache those selector field results.

Link to comment
Share on other sites

If all you want is to save the page IDs, then maybe it is easiest just to just use a page field, the only difference being your Inputfield. So, you would create an inputfield that renders InputfieldSelector from which pages can searched for and selected. Like @LostKobrakai mentioned, all you would need is to make sure on hit save your selected pages are sent. PW will handle the rest. So, something like

class InputfieldYourModuleName extends InputfieldSelectMultiple implements InputfieldHasArrayValue {

}

In there your would have

  • ___render()// here render your inputfieldselector + your multiple selects (i.e. list or show and/or store selected IDs)
  • ___processInput()// for processing sent values...basically looping through an array and sending those to PW for saving...

@edit: thrice my code disappeared! New forum, grrr! I'm not typing again, sorry...Have a look at some custom Inputfields

 

 

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

×
×
  • Create New...