Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add EEL helper for uploading files #2

Merged
merged 7 commits into from
Oct 10, 2024
Merged
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
80 changes: 80 additions & 0 deletions Classes/Eel/HubspotFileHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace FormatD\HubSpot\Eel;

use HubSpot\Discovery\Discovery;
use Neos\Eel\Helper\FileHelper;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ResourceManagement\PersistentResource;
use Psr\Log\LoggerInterface;

class HubspotFileHelper extends FileHelper
{
/**
* @var array
* @Flow\InjectConfiguration(path="api", package="FormatD.HubSpot")
*/
protected array $apiConfiguration;

/**
* @var Discovery
*/
protected Discovery $hubspotApi;

/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;

public function initializeObject(): void {
$this->hubspotApi = \HubSpot\Factory::createWithAccessToken($this->apiConfiguration['accessToken']);
}

public function uploadFileAndGetUrl($resource, ?string $folderId = null): string
{
$folderId = $folderId ?? $this->apiConfiguration['defaultUploadFolderId'];

if (!$folderId) {
$this->logger->warning('No folderId specified for uploading files to Hubspot. Returning empty URL for file upload field.');
return '';
}

if(!($resource instanceof PersistentResource)) {
$type = gettype($resource);
$type = $type === 'object' ? get_class($resource) : $type;
$this->logger->warning('Resource is not a PersistentResource, but of type ' . $type . '. Returning empty URL for file upload field.');
return '';
}

try {
$fileUri = $resource->createTemporaryLocalCopy();
$fileObject = new \SplFileObject($fileUri);

$response = $this->hubspotApi->files()->filesApi()->upload(
$fileObject,
$folderId,
null,
$resource->getFilename(),
null,
json_encode([
'access' => 'PUBLIC_NOT_INDEXABLE', // Private does not work in HS Tickets
'ttl' => 'P2W',
'overwrite' => false,
'duplicateValidationStrategy' => 'NONE', // do not run any duplicate validation
'duplicateValidationScope' => 'EXACT_FOLDER' // search for a duplicate file in the provided folder
])
);
} catch (\Exception $exception) {
$this->logger->error('Error during file uploading to Hubspot: ' . $exception->getMessage() . '. Returning empty URL for file upload field.');
return '';
}

if ($response instanceof \HubSpot\Client\Files\Model\Error) {
$this->logger->error('Error during file uploading to Hubspot: ' . $response->getMessage() . '. Returning empty URL for file upload field.');
return '';
}

return $response->getUrl();
}
}
10 changes: 8 additions & 2 deletions Classes/Service/HubSpotService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use HubSpot\Discovery\Discovery;
use Neos\Flow\Annotations as Flow;
use Psr\Log\LoggerInterface;

/**
* @Flow\Scope("singleton")
Expand All @@ -22,6 +23,12 @@ class HubSpotService
*/
protected Discovery $hubspotApi;

/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;

/**
* @return void
*/
Expand Down Expand Up @@ -52,8 +59,7 @@ public function submitForm(string $formGuid, array $formFields, array $context,
try {
$response = $this->hubspotApi->apiRequest($requestOptions);
} catch (\GuzzleHttp\Exception\ClientException $e) {
\Neos\Flow\var_dump((string) $e->getResponse()->getBody(), 'Error Response');
\Neos\Flow\var_dump($requestOptions, 'Request Data');
$this->logger->error('Error during form submit to HubSpot: '. $e->getMessage());
}

return $response;
Expand Down
4 changes: 3 additions & 1 deletion Configuration/Settings.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FormatD:
HubSpot:
mappingEelContext:
HFile: FormatD\HubSpot\Eel\HubspotFileHelper
String: Neos\Eel\Helper\StringHelper
Array: Neos\Eel\Helper\ArrayHelper
Date: Neos\Eel\Helper\DateHelper
Expand All @@ -12,6 +13,7 @@ FormatD:
api:
portalId: ''
accessToken: ''
defaultUploadFolderId: ''
formFinisherMappings:
default:
firstname:
Expand Down Expand Up @@ -41,4 +43,4 @@ FormatD:
content:
objectTypeId: '0-5'
name: 'content'
value: '${fieldValue}'
value: '${fieldValue}'
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Releases und compatibility:

Create API-Credentials in HubSpot and add them to neos settings:

```
```YAML
FormatD:
HubSpot:
api:
Expand All @@ -44,7 +44,7 @@ FormatD:
The package comes with the following default mapping. This mapping can be modified or additional mappings can be configured.
The objectTypeId is a sort of field-type defined by HubSpot.

```
```YAML
FormatD:
HubSpot:
formFinisherMappings:
Expand Down Expand Up @@ -79,6 +79,35 @@ FormatD:
value: '${fieldValue}'
```

## EEL-Helper for file upload

If you want to use a file upload field in your form you need to use the EEL-Helper `HFile.uploadFileAndGetUrl`. This
helper uploads the file to the file manager of HubSpot and returns the new URL as a field value, before the form is
submitted. For the file upload you need to define the folder ID of the folder in HubSpot. You do it either in the
setting:

```YAML
FormatD:
HubSpot:
api:
defaultUploadFolderId: '111111111111'
```

or you set the optional parameter in the helper: `${HFile.uploadFileAndGetUrl(fieldValue, 111111111111)}`.

Here is how you can include the file upload field in your finisher mapping:

```YAML
FormatD:
HubSpot:
formFinisherMappings:
default:
fileUpload:
objectTypeId: '0-5'
name: 'fileUpload'
value: '${HFile.uploadFileAndGetUrl(fieldValue, 111111111111)}'
```

## Extending

Eel expressions can be used in the mapping configuration. These variables form the context can be used in these eel expressions per default: `formValues`, `fieldValue`.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"license": "MIT",
"require": {
"neos/form-builder": "^2.3",
"hubspot/api-client": "^9.2"
"hubspot/api-client": "^9.2",
"ext-json": "*"
fdchriswaechter marked this conversation as resolved.
Show resolved Hide resolved
},
"autoload": {
"psr-4": {
Expand Down