1

I am working on a plugin which adds a custom field type.

The field type should allow the upload of a file, which is then sent to another server (thus not a normal asset, as it is only temporarily stored on the server). My goal is to save the file and store the path and some metadata as JSON in field type column.

I have made a plugin with a custom field type which works when I am using a text input field.

But changing the type to a file upload does nothing.

I've seen the following solution on SE: https://craftcms.stackexchange.com/a/28253/11621 but the UploadedFile Class always returns "null".

public function serializeValue($value, ElementInterface $element = null)
{
    $namespace = $element? $element->getFieldParamNamespace() . '.' : '';
    $fullhandle = $namespace.$this->handle;

    $file = UploadedFile::getInstanceByName($fullhandle);
    dd($file);
}

The variable $fullhandle contains the value fields.introductionVideo which matches the form input's name of fields[introductionVideo]. The form has the attribute for file uploads enctype="multipart/form-data" for file uploads.

Does anybody have a clue what might be wrong or where to find more information/working example about how to do it correctly?

1 Answer 1

1

After some more digging I think I found the problem with my approach:

Craft sends all changes in a form directly after editing via AJAX to save as a draft and on save only publishes the last draft. That's the reason why $_FILES or $_POST for that matter does not contain all information of the form.

Every "normal" field which can be added to a backend form has this built in and will send this ajax-request to save the change one made. The file field however does not. I guess this has to do with the way Craft uses their own buttons for uploading assets.

The problem is not solved, but I've since switched to a different approach where I use a custom volume and asset field for my upload and another field which can react to the saved asset in the other field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.