Neos.Form.Builder Attachments

Hello,

has anyone experience with the Neos.Form.Builder? Because I want to send more then 1 Attachment with 1 file upload.
I tried it with multiple="multiple" in the Neos.Form FileUpload.html, but it only send the last Attachment.

What do I have to observe?

Thank you

Hi “Ho Bu”,

I’m afraid that won’t be as simple as that. The example FileUpload Form Element is built to convert a single persistent resource.
You probably have to implement a custom Form Element class (along the lines of https://github.com/neos/form/blob/master/Classes/FormElements/FileUpload.php) and invoke the conversion manually in onSubmit() and/or create a custom type converter.

Hello Ho Bu

I am trying to solve the same problem. Were you able to work this out somehow?

Best,
Jakob

I created a ticket for this: https://github.com/neos/form/issues/107 – feel free to comment your ideas there

Thank you!

FYI: I just created a little package providing an example MultiFile Upload Form Element: https://packagist.org/packages/wwwision/form-multifileupload

Hey Bastian

I had not seen this until now. This is great, I think with this I will be able to extend what I had so far. Thanks a lot!!

1 Like

We are using this in the form builder and thus also need to pass all attachments in the email finisher. For this to work, we had to extend a method in that class a bit:

protected function addAttachments(SwiftMailerMessage $mail)
    {
        $formValues = $this->finisherContext->getFormValues();
        if ($this->parseOption('attachAllPersistentResources')) {
            foreach ($formValues as $formValue) {
                if (is_array($formValue)) {
                    foreach ($formValue as $value) {
                        if ($value instanceof PersistentResource) {
                            $this->addAttachmentToMail($mail, $value);
                        }
                    }
                } elseif ($formValue instanceof PersistentResource) {
                    $this->addAttachmentToMail($mail, $formValue);
                } elseif ($formValue instanceof Image) {
                    $resource = $formValue->getResource();
                    $this->addAttachmentToMail($mail, $resource);
                }
            }
        }
        $attachmentConfigurations = $this->parseOption('attachments');
        if (is_array($attachmentConfigurations)) {
            foreach ($attachmentConfigurations as $attachmentConfiguration) {
                if (isset($attachmentConfiguration['resource'])) {
                    $mail->attach(\Swift_Attachment::fromPath($attachmentConfiguration['resource']));
                    continue;
                }
                if (!isset($attachmentConfiguration['formElement'])) {
                    throw new FinisherException('The "attachments" options need to specify a "resource" path or a "formElement" containing the resource to attach', 1503396636);
                }
                $resource = ObjectAccess::getPropertyPath($formValues, $attachmentConfiguration['formElement']);
                if (!$resource instanceof PersistentResource) {
                    continue;
                }
                $mail->attach(\Swift_Attachment::newInstance(stream_get_contents($resource->getStream()), $resource->getFilename(), $resource->getMediaType()));
            }
        }
    }

    protected function addAttachmentToMail(SwiftMailerMessage $mail, PersistentResource $resource)
    {
        $mail->attach(\Swift_Attachment::newInstance(stream_get_contents($resource->getStream()), $resource->getFilename(), $resource->getMediaType()));
    } 

We also allowed files from the image upload to get attached to the email.

Maybe this will help others too.

1 Like