buildPayload(); if ([] === $payload) { $this->logWarning('Application form API finisher skipped: the mapping produced no values.'); return; } $formDefinition = $this->finisherContext->getFormRuntime()->getFormDefinition(); $formLabel = trim($formDefinition->getLabel()); $payload['form'] = $formDefinition->getIdentifier(); // The name the editor gave the form, so the receiving side can label a submission // without keeping a list of identifiers. Taken from the definition rather than // through TranslationService: a translated label would differ per site language. $payload['formLabel'] = '' !== $formLabel ? $formLabel : $formDefinition->getIdentifier(); $payload['submittedAt'] = (new \DateTimeImmutable())->format(\DateTimeInterface::ATOM); $payload = array_merge($payload, $this->resolvePageContext()); $payload['details'] = $this->buildLabeledValues(); $this->apiClient->submitApplicationForm($payload); } catch (\Throwable $e) { $this->logWarning('Application form API finisher request failed.', [ 'exception' => $e, ]); } } private function resolvePageContext(): array { $request = $this->finisherContext->getRequest(); $routing = $request->getAttribute('routing'); if (!$routing instanceof PageArguments) { return []; } $pageUid = $routing->getPageId(); $context = ['pageUid' => $pageUid]; try { /** @var Site $site */ $site = $request->getAttribute('site'); // The page itself, not the form page type the submission was posted to $context['pageUrl'] = (string)$site->getRouter()->generateUri($pageUid, [ '_language' => $request->getAttribute('language'), ]); } catch (\Throwable $e) { // An unroutable page must not cost the submission, so the uid is sent without a URL. $this->logWarning('Application form API finisher could not resolve the page URL.', [ 'pageUid' => $pageUid, 'exception' => $e, ]); } return $context; } }