Thanks a lot! Below code that works for me. It returns file content coded to Base64. Hope it will be usefull for other.
.../espo/custom/Espo/Custom/FormulaFunctions/FileContent.php
.../espo/custom/Espo/Custom/Resources/metadata/app/formula.json
.../espo/custom/Espo/Custom/FormulaFunctions/FileContent.php
PHP Code:
<?php
namespace Espo\Custom\FormulaFunctions;
use Espo\Core\Formula\Exceptions\TooFewArguments;
use Espo\Core\Formula\Func;
use Espo\Core\Formula\EvaluatedArgumentList;
use Espo\Core\FileStorage\Manager;
use Espo\ORM\EntityManager;
use Espo\Entities\Attachment;
class FileContent implements Func
{
public function __construct(
private Manager $fileStorageManager,
private EntityManager $entityManager,
) {}
public function process(EvaluatedArgumentList $arguments): string
{
if (count($arguments) < 1) {
throw TooFewArguments::create(1);
}
$id = $arguments[0];
$attachment = $this->entityManager->getRDBRepositoryByClass(Attachment::class)->getById($id);
if (!$attachment) {
throw new \RuntimeException("No attachment $id.");
}
$content = $this->fileStorageManager->getContents($attachment);
return base64_encode($content);
}
}
.../espo/custom/Espo/Custom/Resources/metadata/app/formula.json
PHP Code:
{
"functionClassNameMap": {
"fileContent": "Espo\\Custom\\FormulaFunctions\\FileContent"
},
"functionList": [
"__APPEND__",
{
"name": "fileContent",
"insertText": "fileContent(ID)"
}
]
}
Comment