diff --git a/Classes/Finishers/BrevoFinisher.php b/Classes/Finishers/BrevoFinisher.php index d5a8629..a700a40 100644 --- a/Classes/Finishers/BrevoFinisher.php +++ b/Classes/Finishers/BrevoFinisher.php @@ -23,6 +23,7 @@ use StudioMitte\Brevo\Events\FormFinisherAttributeEvent; use TYPO3\CMS\Core\EventDispatcher\EventDispatcher; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Core\Utility\MathUtility; use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher; /** @@ -51,7 +52,16 @@ protected function executeInternal(): void return; } - $this->addEntryToBrevo() ? $this->setFinisherSubscribedVariable(1) : $this->setFinisherSubscribedVariable(0); + $newContactId = $this->addEntryToBrevo(); + if (MathUtility::canBeInterpretedAsInteger($newContactId)) { + $this->setFinisherSubscribedVariable(1); + if ($newContactId > 0) { + $this->setFinisherNewContactIdVariable($newContactId); + } + return; + } + + $this->setFinisherSubscribedVariable(0); } protected function setFinisherSubscribedVariable(int $returnValue): void @@ -63,7 +73,22 @@ protected function setFinisherSubscribedVariable(int $returnValue): void ); } - protected function addEntryToBrevo(): bool + protected function setFinisherNewContactIdVariable(int $newContactId): void + { + $this->finisherContext->getFinisherVariableProvider()->add( + 'brevo', + 'data.newContactId', + $newContactId + ); + } + + /** + * Add the entry to Brevo. + * In case of active DOI, the contact is not created immediately, so no contact ID can be returned. + * + * @return int|null + */ + protected function addEntryToBrevo(): int|null { try { $apiInstance = $this->getApi(); @@ -76,20 +101,21 @@ protected function addEntryToBrevo(): bool ->setAttributes($this->getAttributes()) ->setRedirectionUrl($this->getRedirectionUrl()); $apiInstance->createDoiContact($createContact); - } else { - $createContact = new CreateContact(); - $createContact - ->setEmail($this->parseOption('email')) - ->setUpdateEnabled(true) - ->setListIds($this->getEnrichedListIds()) - ->setAttributes($this->getAttributes()); - $apiInstance->createContact($createContact); + return 0; // in case of DOI we cannot return the contact ID, because the contact is not created immediately } - return true; + + $createContact = new CreateContact(); + $createContact + ->setEmail($this->parseOption('email')) + ->setUpdateEnabled(true) + ->setListIds($this->getEnrichedListIds()) + ->setAttributes($this->getAttributes()); + return $apiInstance->createContact($createContact)?->getId() ?? 0; + } catch (\Exception $exception) { // todo: should we forward it to the user? $this->logger->error($exception->getMessage()); - return false; + return null; } } diff --git a/Readme.md b/Readme.md index b729546..d2d9fce 100644 --- a/Readme.md +++ b/Readme.md @@ -70,6 +70,13 @@ The following configurations can be used to override the global settings identifier: Brevo ``` +### Returned data from Finisher + +There are some additional data returned by the finisher to use in following finishers: +- **Brevo Contact ID**: The ID of the newly created contact in brevo. This can be used for example to trigger an automation workflow for this contact. (*brevo.data.newContactId*) +- **Subscription Status**: If the user has subscribed to the newsletter or not. (*brevo.data.subscribed*) +- + ## Author This extension has been created by [StudioMitte](https://studiomitte.com)