Jump to content

Adding Fields via API


Lance O.
 Share

Recommended Posts

I would like to create sets of custom fields that can be added using the API to sites as I need them.

For example, if one of my clients has an existing site developed in ProcessWire and needs a blog added, I would like to use the API to add a set of custom fields specific to a blog.

As another example, I may need to add a set of custom fields for social media links.

Unfortunately, I can't seem to find a resource that lists every property needed for each field type. How do I know which properties to set? Or is there a way to output fields and properties for an existing installation of ProcessWire?

Link to comment
Share on other sites

I am a bit confused why you would want to add the fields via the API? If a client wanted a new functionality that required adding new fields, why not just log in as admin and add the fields?

Anyway, adding fields of course implies not only creating them but adding them to a template. Adding fields to a template is a two step process. In the first instance, you create a fieldgroup and add fields to it. You then add the fieldgroup to a template. In your case, you would be adding to an existing template so you may not need to create the fieldgroup, rather to 'get' it. If you are not afraid of looking at code, have a look at BlogInstallWizard.php (part of the Blog module). There's examples for adding different types of fields as well as adding to an existing template. 

At the moment we don't have a dedicated resource that lists the properties of each field type. There are nuggets of knowledge here and there in the forums. My favourite (though probably incomplete and outdated for now) is the APIGen for PW. http://processwire.com/apigen/namespace-None.html. On my local setup, I generate apigen docs based on the latest PW and pulling not only .php files but also .module files. Btw, as part of my upcoming 'ProcessWire Modules' tutorials, I will be writing about this...

Link to comment
Share on other sites

Adding fields via the API is much faster than adding them using the Admin. If I can create a library of custom field sets, I can save a lot of time setting up a site or adding to an existing one.

Thanks for your response, kongondo. I'll check out the resources you mentioned.

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