Home Forums Gutenberg Dynamic ACF block select field choices based on an API call


Dynamic ACF block select field choices based on an API call

  • Hi there. I’m building a a site to render statistical graphs using ACF blocks. An example of the graph below.

    Population in Finland 1990-2019, a statistical graph

    Each of the blocks gets the statistics data from an API call, and the API URL is unique to each block. For selecting the data dimension to be used as X- and Y-axis of the graph, I have used select fields (with stylized UI and AJAX lazy-load). The dimensions vary for each graph (depend on the API response). Thus, I would need to make an API call first, and the populate the select field choices based on the response.

    Settings for the statistical graph block

    Select field’s documentation recommends using acf/load_value filter to dynamically populate the choices.

    Now, I’m not sure how to properly utilize the load_value filter to populate the choices. I found examples using load_field filter, but using that filter, the ACF block’s settings — block’s unique ID in particular — are not (yet) fully available. I can only get the field key, but that is same for all the block instances. Thus I cannot get the unique API URL for making the API call.

    Are the any examples using load_value filter for ACF block select field choices, or should I pursue some other kind of solution?

    – – – –

    Some background info on the project; the data comes from Statistics Finland‘s PxWeb API. With the PxWeb interface, you can create saved queries towards the data. Each saved query will be assigned a unique API URL you can refer to. I’m requesting the data in JSON-stat format and rendering the graphs using HighCharts library.

    An example of a saved query URL is Population in Finland for years 1990-2019. Response comes in JSON-stat format. It contains the dimensions inside the dataset > dimension structure.

    I’m using ACF PRO version 5.9.1.

  • This reply has been marked as private.
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.