Jump to content

Fieldtype idea to store serialized array


tpr
 Share

Recommended Posts

I'm thinking about creating a new module to store form submissions quickly using serialized arrays. The idea is not to create separate fields for each submitted input but only one field. On the backend, the values would displayed in a human readable format, and the field itself won't be editable.

Is there any existing solution for this? And, in general, any thoughts on this idea?

  • Like 1
Link to comment
Share on other sites

Unfortunately I don't have access to them. Even if they are doing the same, they display the submitted values on the backend like real fields, aren't they? My idea is to display values as a list (key-value), and make them read-only.

I'm using a 3rd party form solution and this would simplify saving submissions. I was creating fields on the fly so far but that's very error-prone, plus I feel it's too much for the purpose. What I would need is to store submissions as simply as possible, and make submissions readable in the backend too.

Link to comment
Share on other sites

Maybe have a look into the FieldtypeYaml module. It's saving data in a textual representation and has multiple optional ways to access those data.

As aside: There's no inherently "real" field when we're talking about UI. It's just a matter of having an inputfield and some save method to make a UI field real. E.g. all the module settings' forms have their values stored as json blobs and the forms still look the same as others. If you only need a readonly display it's just less work (no save method) and different markup.

Link to comment
Share on other sites

Yes, MenuBuilder too :-)...(but uses JSON)

IIRC, somebody already did this? i.e. there's a fieldtype around here somewhere...

Edit 1:

Yeah, what @LostKobrakai said...

Edit 2:

@tpr: Moving this topic to plugin development. The current board (ProcessWire Support Forums  → Community Support  → Modules/Plugins) is for supporting released modules :-)

Edited by kongondo
Link to comment
Share on other sites

Gave this thing a go and it seems I will take a simpler approach: using a textarea and a InputfieldTextarea::render hook to display the array in a HTML list format in the admin. Works like a charm, only the admin display should be tweaked a bit to make it nicer.

I've also changed the storage type from serialized array to json. Of course field value has to be json_encoded before save but that's a minor issue.

Link to comment
Share on other sites

I've also changed the storage type from serialized array to json. Of course field value has to be json_encoded before save but that's a minor issue.

You could hook FieldtypeTextarea::sanitizeValue for that. If it's not a string with [ or { in the beginning, then encode it.

  • Like 1
Link to comment
Share on other sites

Just a note: overwriting the render value with custom html output (getting rid of the input field) is not a good idea. The field will have no value in the admin page (only the custom html, no form input) and hitting Save will null out the original value.

This could be possibly eliminated keeping the original input for the field and hiding them, and appending the custom html. As for now, I made the field Locked so the field will use the renderValue to display its contents. Doing so hitting Save will not change the original value.

Here's how the current admin page looks like. The "Submission details" is one textarea with json contents, using a renderValue hook to make nicer:

post-3156-0-70633000-1450389429_thumb.pn

  • Like 1
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...