Jump to content

Field Type Json


dhruba
 Share

Recommended Posts

Hi

I want to develop a new field type to store more complex but lightweight data as json. I could do that using text field but I m loosing the cool way (means object oriented way like $object->property1->property2->property3 )  to add and edit those fields. So I want a new field type to do that and it should support the admin also. How to write a module for that I need a help THANKS.

Link to comment
Share on other sites

You might want to start by reading these...

http://www.flamingruby.com/blog/anatomy-of-fields-in-processwire/

http://wiki.processwire.com/index.php/Module_Creation

http://processwire.com/talk/topic/4420-page-list-migrator/  - not directly related, but gives you some ideas about working with json in PW

http://processwire.com/talk/topic/2394-how-to-present-your-module/

http://processwire.com/api/modules/

https://github.com/ryancramerdesign/ProcessHello

https://github.com/ryancramerdesign/ProcessWire/blob/master/site-default/modules/Helloworld.module

http://processwire.com/talk/topic/4410-storing-lists-in-pw-fields/ - not directly related; storing serialized field data in db...

I'm not sure what you mean by "it should support the admin also". If you mean you want a GUI for it in the admin, then you are looking at a Process Module, most likely. I suggest to have a look at some of the code of the available modules as well.

As for JSON, you can either use PW's wireEncodeJSON method or PHP's native json_encode. The difference between the two is that PW's removes empty vaues.

  • Like 3
Link to comment
Share on other sites

Good links from Kongondo there. I also want to mention that if you like to learn by example, have a look at Adrian's FieldtypePhone and/or FieldtypeMapMarker modules in the modules directory (among others). These are Fieldtype and Inputfield combinations that present objects as their value, similar to what you are talking about. The big difference in your case is that your need is simpler (though not as DB friendly), because you'll be bundling it all up into a single JSON string. The place where you would convert your object into a JSON string is in your Fieldtype's sleepValue() method, and the place where you'd convert it from a JSON string to an object is in your Fieldtype's wakeupValue() method. 

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