Jump to content

FieldType Image and creating new multi-part repeatable fields


Rob
 Share

Recommended Posts

Hi all, new to the community but a CMS old hand!!

We're seriously considering PW for building a new site as it covers the 90% of scenarios pretty well, and the API hopefully gives enough freedom for the remaining 10%.

One thing I am a little unsure about (although I've only been looking at PW for a few days) is how I might go about creating new field types.  For example, I have been looking at the code trying to work out how the Image field works as this is a multi-part field with both an image and a description.

So lets say, for example, that I wanted to create a field type that had 3 simple text fields (and I could add multiples like the image field) - how would I go about doing that?  The documentation is strong in some areas but lacking in others, and I have yet to be able to find any tutorials for developing new field types.  The code strikes me as being well-architected and intelligent (the hooks are far more flexible than most other CMS') but still complex enough to be tricky for a newcomer to the CMS.

Thanks in advance, looking forward to getting stuck in!

Link to comment
Share on other sites

Welcome to the forums Tall (for abbreviation) :)

Repeatable fields are on the roadmap only for August http://processwire.com/about/roadmap/

The subject is being discussed here http://processwire.com/talk/index.php/topic,53.0.html, maybe you can also throw some ideas.

UPDATE: I would bet that there will be a module even before August

Link to comment
Share on other sites

Welcome to the forums Tall (for abbreviation) :)

Repeatable fields are on the roadmap only for August http://processwire.com/about/roadmap/

The subject is being discussed here http://processwire.com/talk/index.php/topic,53.0.html, maybe you can also throw some ideas.

UPDATE: I would bet that there will be a module even before August

I think there may be a misunderstanding (and please, call me Rob!).

As far as I can tell there is already fields, like Image, where I can add multiples.  I jsut want to know how I can mimic this behaviour, and indeed how I can create my own custom field types.

I'll try again: -

At the moment, I can add an image field type, and when I am editing my page, I can add multiple sets of images each of which consists of both an image and a description.  So in essence this is a multi-part field as there are two elements - image and description.  I can add multiple "sets " of these two bits of data to my page.

Does this make more sense?

Link to comment
Share on other sites

Rob, welcome to the forums! Diogo is right that we'll be building a module to handle this natively without having to create your own Fieldtype. But for now you would have to create your own Fieldtype. The image fieldtype may be a bit much to look at because there's so much stuff in there specific to images as opposed to the actual functionality you are looking for. If I'm understanding correctly, you are wanting a field that supports any number of entries with 3 text fields each.

Regarding documentation, ProcessWire is still kind of a new project and so I've tried to cover the areas that would be helpful to the most people, and am going back and filling in the rest of the gaps over time. Documentation on how to do things like create your own Fieldtype is something that will be coming. It's basically just a matter of time and resources. We're just 1 year old project and the documentation will continue to grow with the age of the project. So I'm glad to guide people through how to do everything as much as possible, and I'll be glad to assist you with creating the fieldtype you want. My experiences here help me to write documentation as well.

Fieldtype vs. Inputfield

Creating your own Fieldtype will be a matter of working in PHP and extending ProcessWire's classes. You will need both a Fieldtype and an Inputfield. The two work together. It's good to understand what both are. The Fieldtype defines the data's type and handles loading, saving and sanitizing the data that is added to a page. Whereas an Inputfield does nothing other than provide the HTML form inputs to collect the data, and then retrieve the posted data when it's been submitted. As a result, an Inputfield only comes into play in the admin when you are entering data. Whereas the Fieldtype is always active any time the data is loaded. To put it another way, if you only ever populated data in ProcessWire with the API, then you wouldn't even need an Inputfield.

In many cases there is a specific Inputfield for each Fieldtype. But there are also many Fieldtypes that can work with an existing Inputfield too. For instance, the FieldtypeTextarea can utilize InputfieldTextarea or InputfieldTinyMCE, according to your need. In your case, I think you'll want to create both a Fieldtype and an Inputfield specific to it.

Fieldtype vs. FieldtypeMulti

In ProcessWire Fieldtypes fall into a class of either Fieldtype or FieldtypeMulti. The only difference between the two is that Fieldtype supports one entry per field per page and FieldtypeMulti supports any number of entries per field per page. An example of a regular Fieldtype is FieldtypeTextarea, which would simply be one textarea input. Examples of FieldtypeMulti are FieldtypeFile, FieldtypeImage, FieldtypePage, and FieldtypeComments.

Regardless of whether a Fieldtype or a FieldtypeMulti, ProcessWire reserves one database table per field. This means you can build complex Fieldtypes containing multiple fields of data per entry. While not many fieldtypes demonstrate this, Fieldtypes are specifically designed to represent complex data types in the database and in the API. FieldtypeComments is currently the most complex public example as it's schema contains many fields per entry.

Creating your own FieldtypeMulti

If you are still interested, here are some links to get started. Let me know if this stuff makes some sense to you (you don't need to understand it all), and then we'll use this thread to start building a simple FieldtypeMulti. Once we've got something working, hopefully we can take parts of this and turn it into a Fieldtype tutorial.

Discussion and example module of how to expand the schema of the Image fieldtype

http://processwire.com/talk/index.php/topic,466.0.html

The Fieldtype base class (read the comments for each function):

https://github.com/ryancramerdesign/P21/blob/master/wire/core/Fieldtype.php

The FieldtypeMulti base class (read comments, but not as important as Fieldtype):

https://github.com/ryancramerdesign/P21/blob/master/wire/core/FieldtypeMulti.php

(ignore the commented out code, I've just kept some things commented out for future reference)

The Inputfield base class (don't spend too much time, just take a brief look):

https://github.com/ryancramerdesign/P21/blob/master/wire/core/Inputfield.php

Simple Inputfield example implementation (integer field):

https://github.com/ryancramerdesign/P21/blob/master/wire/modules/Inputfield/InputfieldInteger.module

  • Like 2
Link to comment
Share on other sites

  • 5 years later...

Although this topic is nearly 6 years old, I wish I had seen it sooner. It took me about a week just to realize what the difference between Fieldtype and FieldtypeMulti was, and exactly (in my situation) which class (FieldtypeMulti) I wanted to extend for the Fieldtype I am trying to build, such as Fieldtype, FieldtypeMulti, or WireArray. I still don't entirely know exactly which methods are required, or why, though the code documentation in the base classes is quite awesome! I can copy and edit from what others have done, but I'm just hacking scripts together at this point, really. It may work in the end, but it also may not be the right way about doing things.

If there's still any interest (and/or time!) in expanding upon this, Ryan, I know that I'd absolutely love 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...