Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions Classes/Finishers/BrevoFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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;
}
}

Expand Down
7 changes: 7 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down