Form data for add-ons
Overview
Three services help an add-on that works with forms:
- a save preparer changes the form's data right before WebForms saves it;
- the live field catalog finds fields of given types on all live forms, for work outside a form request;
- the store view values hold what a store view overrides on a form, a section, a field or a logic rule.
Short names are relative to MageMe\WebForms\. A complete class starts with <?php; any other code block is an
excerpt.
Change a form before it's saved
A preparer runs right before WebForms saves a form, on every save: in the admin, on an import, on a duplicate and through the API.
Implement MageMe\WebForms\Api\FormSavePreparerInterface. It has one method, prepare(AbstractModel $form): void.
Register the class in the global etc/di.xml, as a proxy, because the list is built on every form load. The Zendesk
add-on keeps the type of each mapped Zendesk field next to the mapping:
<type name="MageMe\WebForms\Model\ResourceModel\Form">
<arguments>
<argument name="savePreparers" xsi:type="array">
<item name="zendesk" xsi:type="object">MageMe\WebFormsZendesk\Model\Integration\MappingTargetTypes\Proxy</item>
</argument>
</arguments>
</type>
public function prepare(AbstractModel $form): void
{
$rows = $form->getData(FormInterface::ZENDESK_MAP_FIELDS);
if (!is_array($rows) || $rows === [] || !$this->ticketFieldTypes->hasList()) {
return;
}
// … set or remove the target type of each row
if ($rows !== $original) {
$form->setData(FormInterface::ZENDESK_MAP_FIELDS, $rows);
}
}
What to keep in mind:
- Change only your own keys.
$form->getOrigData()holds the values as they were loaded. For a new form, a duplicate or an import it's empty: check$form->isObjectNew().- One action can save the form more than once, so your preparer may run several times. Make it give the same result each time.
- Preparers run in the order of their item names.
- A registered class that doesn't implement the interface stops the store with an
InvalidArgumentException.
Find fields on live forms
MageMe\WebForms\Api\LiveFieldCatalogInterface finds fields of given types on all live forms, not on drafts or
kept versions. Use it for work outside a form request, such as a cron job that refreshes data per field.
| Method | What it does |
|---|---|
findLiveFieldsByTypes(array $types, array $formColumns = []): array | Returns one row per field: field_id, form_id, is_active, type_attributes, form_is_active, and the form columns you asked for under form. |
findStoreValues(array $formIds, array $fieldIds): array | Returns the store view values of those forms and fields. |
saveTypeAttribute(int $fieldId, string $attribute, mixed $value): void | Writes one key of the field's type attributes, without touching the form's draft. null removes the key. |
Where it answers:
- in cron, without an admin role;
- in the admin and in web API calls, limited to the forms the admin role may see;
- anywhere else it finds nothing, and the other two methods throw
NoSuchEntityException.
The Zendesk add-on refreshes the options of its Group fields:
$rows = $this->catalog->findLiveFieldsByTypes([self::FIELD_TYPE], [ZendeskFormInterface::IS_ZENDESK_ENABLED]);
// … read the store view values with findStoreValues($formIds, $fieldIds), build the snapshot
$this->catalog->saveTypeAttribute($fieldId, GroupOptionsSnapshot::ATTRIBUTE, $snapshot ?: null);
Store view values
Api\Data\StoreInterface is one store view override of a form, a section, a field or a logic rule:
ENTITY_TYPE_FORM, ENTITY_TYPE_FIELDSET, ENTITY_TYPE_FIELD or ENTITY_TYPE_LOGIC. getStoreData() holds the
overridden keys. Despite the name, it isn't Magento's store.
Api\StoreRepositoryInterface::getListByEntity() lists the overrides of one entity. The Klaviyo add-on rewrites its
field mapping in every store view after a form import:
$stores = $this->storeRepository->getListByEntity(StoreInterface::ENTITY_TYPE_FORM, $formId)->getItems();
foreach ($stores as $store) {
$data = $store->getStoreData();
// … rewrite the Klaviyo mapping in $data
$store->setStoreData($data);
$this->storeRepository->save($store);
}
What's next
- Submission data for add-ons: data of your own on each submission.
- Add-on integration contract: send submissions to another service.
- Extension points: every other place your module can extend WebForms.