Remote option lists
Overview
Your integration may need choices that live in the service, such as its agents, groups, ticket fields or interest categories. A remote option list fetches them for you. You tell WebForms which list it is and how to fetch it. WebForms does the rest:
- it keeps the list in the WebForms Integration Lists cache type, one per integration, account and list;
- it keeps serving the last good list when a refresh fails;
- it lets one process fetch at a time.
Class names are short: Api\… is MageMe\WebForms\Api\…, and the remote list classes are in
MageMe\WebForms\Model\Integration\RemoteList\.
Write the list
Extend AbstractRemoteOptions. Take RemoteOptions\Context as the first constructor argument and pass it to the
parent. The agents list of the Zendesk add-on is a complete example:
<?php
declare(strict_types=1);
namespace MageMe\WebFormsZendesk\Config\Options;
use MageMe\WebForms\Model\Integration\RemoteList\AbstractRemoteOptions;
use MageMe\WebForms\Model\Integration\RemoteList\FetchedList;
use MageMe\WebForms\Model\Integration\RemoteList\Pages;
use MageMe\WebForms\Model\Integration\RemoteList\RemoteOptions\Context;
use MageMe\WebFormsZendesk\Helper\ZendeskHelper;
use MageMe\WebFormsZendesk\Model\Integration\ZendeskHandler;
class Agents extends AbstractRemoteOptions
{
public const LIST = 'agents';
private ZendeskHelper $zendeskHelper;
public function __construct(Context $context, ZendeskHelper $zendeskHelper)
{
parent::__construct($context);
$this->zendeskHelper = $zendeskHelper;
}
protected function integrationCode(): string
{
return ZendeskHandler::CODE;
}
protected function accountKey(?int $storeId): ?string
{
return $this->zendeskHelper->accountKey($storeId);
}
protected function listName(): string
{
return self::LIST;
}
protected function fetch(): array|FetchedList
{
return $this->fetchFor(null);
}
protected function fetchFor(?int $storeId): FetchedList
{
$api = $this->zendeskHelper->getApiForStore($storeId);
$read = Pages::read(static fn(mixed $cursor): array => $api->agentsPage($cursor));
return $read->withItems(array_map(
static fn(array $row): array => ['label' => $row['name'], 'value' => (string)$row['id']],
array_values(array_column($read->getItems(), null, 'id'))
));
}
}
| Method | Return |
|---|---|
integrationCode(): string | Your integration's code. Your handler and your settings modifier return the same one. |
accountKey(?int $storeId): ?string | A stable key of the credentials in use. null while the integration isn't set up. |
listName(): string | A name unique within your integration. |
fetch(): array|FetchedList | The options from the service: [['value' => …, 'label' => …], …], plain scalars only. |
reauth(): ?callable | Optional. A function that authorizes again. The default is null. |
The class is an option source. Inject it wherever you need the list: your settings modifier, a field type or your handler. There is no registry to add it to.
Report a failed fetch
When fetch() can't get the list, throw the exception that says why. It decides what stops:
| Throw | When | What stops until the next try |
|---|---|---|
Api\Exception\IntegrationAuthExpiredException | The service refuses the credentials | Every list of the account |
Api\Exception\IntegrationUnavailableException | The service can't be reached | Every list of the account |
| Any other exception | Only this list failed | This list |
If reauth() returns a function, WebForms calls it after IntegrationAuthExpiredException and then repeats the
fetch once.
Read a paged API
Return a plain array when the service sends the whole list in one response. For a paged API, use
Pages::read(callable $page, int $maxRows = 2000, int $maxPages = 50):
- your callback gets the cursor and the page number. The cursor is
nullfor the first page, the number starts at 1; - it returns
[$rows, $nextCursor], withnullas the cursor after the last page; Pages::read()returns aFetchedList. The list is marked as cut when reading stopped at$maxRowsor$maxPages.
Different credentials per store view
Override fetchFor(?int $storeId), and return a different accountKey() for that store view. WebForms keeps lists
per account, not per store view: store views with the same key share one list.
When WebForms fetches a list
Opening a form never calls the service. WebForms fetches a list only at these moments:
| Moment | Which lists |
|---|---|
| An admin clicks Refresh Lists | The lists your settings modifier reads while it builds its fieldset. They are fetched at once. |
| A submission is sent to the service | The lists your handler reads |
Your own code runs inside FetchPolicy::allowWhile(), in a cron job for example | The lists that code reads |
Anywhere else, the list comes from the cache. Apart from Refresh Lists, a list is fetched at most once a day.
Two things must be in place for this to work:
- Refresh Lists shows only when your settings modifier extends
MageMe\WebForms\Ui\Component\Form\Form\Modifier\Integration\AbstractIntegrationSettingsand is in the form's modifier pool. - The modifier, the handler and the list return the same integration code and account key.
When a fetch fails
The code that reads the list never sees the exception. WebForms logs it, and toOptionArray() and listForStore()
return the last good list. That list is used for up to 30 days after its last successful fetch. With no list from
before, they return an empty list.
WebForms tries again after 15 minutes. After IntegrationUnavailableException, it waits longer if the service asks,
up to an hour.
When your code must tell an empty list from an unknown one, read the list with:
| Method | When the list isn't known |
|---|---|
findListForStore() | Returns null |
requireListForStore() | Throws Api\Exception\IntegrationListUnknownException |
Don't catch that exception in your handler. The queue retries the submission, no earlier than the list's next
allowed fetch. A handler that first collects what it can't send and throws afterwards builds the same exception
with listNotKnown($storeId).
What's next
- Add-on integration contract: the handler and the settings modifier that read the list.
- Extension points: every other hook.