Jump to content

Need help to create my own Fieldtype


Recommended Posts

Hi there,

my problem is to understand how i can create my own fieldtype. I look into the Events Fieldtype Topic but i don't find any tutorial like creating Processwire Modules. A look into the code dose not help me.

The field i want to create stores multiple informations like a address field. "IP, Connection, Hardware, Firmware, Lat, Long"

For this i need a FieldtypeNode.module and ImputfieldNode.module right? But what do i need insight this two modules? Where to set the database schema?

Much things are so easy in ProcessWire but this let me stuck.
 

 

Link to comment
Share on other sites

Are you looking to create a Fieldtype module to get a job done, or more as a learning exercise?

If it's the former and you place a value on your own time you will probably come out ahead by purchasing the ProFields package. I don't follow exactly what you are wanting to do but I think Table could be suitable, or possibly Multiplier or Textareas.

If it's more of a learning exercise then you can expect this to be somewhat challenging, but you have the Events modules as a starting point. Why do you say that the code for the Events modules does not help you? The code is well commented and it sounds like you want to do something quite similar to what the Events modules do.

 

Link to comment
Share on other sites

Hi Robin,

thanks for your answer. Sorry for my bad english it is not so easy to express my situation. The Code is really well commanded but i have problems to filter out the core parts that i need. On my page (it's a page for a unsalaried project) i need to save data for a page in a lot of text fields, no repeat, no multiple only 1 field for 1 information but this information are from the same group.

I'll try to explain. On the project we have a list of nodes (routers), each router are a page and now i get information to each node from a external api. This informations are all "nodeinfo" and contains IP addr, Lat, Long, Hardware, Firmware, Connection time.... At the moment each information is stored on a separate text field and in a separate database table.  I think that it is much more efficient when i store this information on one database table, is it?

I don't want to use profield ore get a job done. I ask for a learning exercise. I find it easier to read a manual as to analyse only the code.  For constructing modules there are many such introductions like this:  http://processwire.com/api/modules/

ore this:

 

Link to comment
Share on other sites

You should really try to read and understand the code of MapMarker.  I was in the same situation as you are, and finaly made my own fieldtype very easily. I implemented LeafletJS easily just by reading the comments and the code (and found that the module was already done in the forum lol...).

Take this module as tutorial, in this module you can find how to store and get data from.

14 hours ago, kreativmonkey said:

But what do i need insight this two modules? Where to set the database schema?

https://github.com/ryancramerdesign/FieldtypeMapMarker/blob/master/FieldtypeMapMarker.module#L142-L199

You will find an example there, and of course by looking at others module's code. Later, for exemple, you will certainly ask what are ___wakeupValue and ___sleepValue, and/or other methods. The step is to go through the forum and search. You will find good topics and hints here and there to understand.

Also, you should experiment a bit, try to modify the code/datas, check the DB wih PhpMyAdmin and learn from. You will see your module's table with datas.

And do not hesitate to ask help on the forum.

  • Like 1
Link to comment
Share on other sites

2 hours ago, kreativmonkey said:

I'll try to explain. On the project we have a list of nodes (routers), each router are a page and now i get information to each node from a external api. This informations are all "nodeinfo" and contains IP addr, Lat, Long, Hardware, Firmware, Connection time.... 

I think one solution which may work here is to add these as separate fields to your node/router template, using the fieldtypes that already exist. Or is there a specific reason you would want these in one field?

Proposed fields for your router template:

  • IP address: text.
  • Lat/Lon: MapMarker
  • Hardware: text?
  • Firmware: text?
  • Connection time: text/integer?

Your API "nodeinfo" data can hopefully be decoded and transformed so you can get the values easily - what format is it in? Plain text, or something like JSON or XML?

Here's some example code to update a router page that has separate fields for your data:

$router = $pages->get(1234);    // a router/node page by its ID
$router->ip_address = '127.0.0.1';
$router->geo->lat = 54.12;
$router->geo->lng = -1.2;
$router->hardware = 'cisco';
$router->firmware = 'cisco-ios-fw-100';
$router->connection_time = 42;

 

Link to comment
Share on other sites

@flydev,

that is exactly what i have done now. For a few minutes my module works and stores data on the table but now i have an issue and i don't find out what happens. I'll finish the job for now and see what tomorrow brings

 

@Craig A Rodway,

this is exactly what i'm doing currently. I have a field for each information on my router page but i think its much better for performance to bring this information in one database table. The second Idea is to publish my work for other communitys who want to do the same. At the moment i have a module that calls an external server and get a json with lot of data. I filter this data and store the information for each router/node in the page. That works fine and now i would like to complete my work and add one field for this information.

Link to comment
Share on other sites

I'm not sure about your requirements, but by using things like autojoin you should already have quite nice performance. I'm not sure if building a fieldtype for the sake of not joining a few tables would rather be considered premature optimisation. But I don't want to discourage you if you're also doing it for educational purposes.

To your issue: ___sleepValue() and ___wakeupValue() are your friend when debugging what going into the db and what comes out of it. 

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