Dynamic API Fetcher
Introduction
The main purpose of this element dynamic_api_fetcher is to facilitate the retrieval of data from external sources via HTTP GET requests. It serves as a element within your dynamic field to connect to other web services or APIs and retrieve information.
How to use it ?
Start by creating your widget :
name: "Test - Dynamic API Fetcher"multiple: falsecategory: 'API'enabled: TRUEfields: data: type: dynamic_api_fetch label: "API Fetcher"
Integrate the template into the intended page.
Inputs :
URL : The URL (Uniform Resource Locator) is the web address of the resource you want to retrieve data from.
Query Parameters : Query parameters allow you to send additional data or instructions to the API. They are often used to filter, sort, or customize the data you want to retrieve.
Headers : Headers are a way to include metadata about the request. They can be used to send information such as authentication tokens, and other instructions to the API.
Useful hooks
You've also provided two hooks for customization: hook_dynamic_api_fetch_config_alter and hook_dynamic_api_fetch_result. These hooks allow you to modify the request configuration and alter the results before they are presented to the front end.
- hook_dynamic_api_fetch_config_alter(&$api_config, $info) : This hook is used to modify the request configuration before making the GET request. You can alter the URL, query parameters, or headers as needed. For example, you might want to add authentication headers or change the URL based on some conditions.
./your_custom_module.module
/** * Alter DF dynamic_api_fetch config. * * @param $api_config * @param $info */function hook_dynamic_api_fetch_config_alter($api_config, $info) { if (isset($info['uuid']) && $info['uuid'] = 'vactory_news:fetch') { // Alter config here. $api_config['header']['api-key'] = '094bd386-a4af-492d-83dd-8c7deb02ba2e'; }}
- hook_dynamic_api_fetch_result(&$result, $info) : This hook is used to alter the results of the API request before they are presented to the front end. You can manipulate the data received from the API response, transform it, or perform any other necessary operations.
./your_custom_module.module
/** * Alter DF dynamic_api_fetch response. * * @param $result * @param $info */function hook_dynamic_api_fetch_result_alter($result, $info) { if (isset($info['uuid']) && $info['uuid'] = 'vactory_news:fetch') { // Alter response here. $result['news'] = isset($result['posts']) ? $result['posts'] : $result['error']; }}