Jump to content

Custom field from External API


David Beesley
 Share

Recommended Posts

Hi All,

Second time poster, so please continue to go easy on me.

I have a requirement to store some minimal data from an external API within ProcessWire, alongside some page data.

Lets say I want to create a page of "Offers" but my products were stored externally and exposed to me via a REST API with the endpoint /products/all (assume we are authenticated)
I would like to access the API from a repeater field in ProcessWire, show a list of "ID - NAME" and then store the external ID alongside some manually inputted data. This ID could then be used at a later date for any other sort of work with the external API.

What would be the best approach to create a field for this? If it is even possible?

Thanks,

Dave

Link to comment
Share on other sites

If I understand you right, you simply need to create all fields you need. (In PW, all fields are custom fields!)

Put your desired fields together in a template, and use this template when importing external data into pages.

imported-data:
    - import 1: extid = 1, text = bla, amount = 10, ...
    - import 2: extid = 7, text = blu, amount = 7, ...
    - import 3: extid = 21, text = blo, amount = 0, ...

Does this help?

Link to comment
Share on other sites

Simply populating a select field seems like an easy idea, but you'll still store the selection in processwire, so what happens if that element won't be available anymore by your external api? ProcessWire's db will still have the selection saved until the page will be saved the next time. Also – as the select field options are generated from your api – you won't even notice, that there's still a previous state in the db when looking in the backend because that selected option is no longer part of the select field and therefore invisible.

If you've that case covered it shouldn't be to hard to simply extend Fieldtype/InputfieldSelect and just add in your custom option retrieval.

Link to comment
Share on other sites

In this scenario. The offers are short lived, and regularly replaced. So it hopefully shouldn't be an issue.

That said, it's a valid consideration. I had considered a batch import of product data into PW via CRON. But that, I suppose, would have a similar issue if the product is removed it will be removed from PW.

It's always the danger I suppose, when creating a replication of a data source.

I'll take a look at creating an extension of Fieldtype/InputfieldSelect that get's its data from the API. Although perhaps an extension of a fieldtype with pagination may be a more user friendly solution when the product amounts get into the hundreds or more.

Link to comment
Share on other sites

There's currently only a single type of Inputfield / Fieldtype that does support the pagination feature and that's InputfieldTable (since about a week). But the support for it is in the latest pw version, if you'd like to go for it.

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