Plupload Form element creates unmanaged files only. We should provide a helper function that will save that files to managed_file table. There would ideally be also a element_value callback that would use this helper function to create managed files and provide their fid's as values.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dagomar’s picture

It seems that the default way drupal handles this is in Drupal\file\Element\ManagedFile::valueCallback.

I'm wondering what the reasoning is to have this done by a helper function or method? Wouldn't it be better if this would be handled in Drupal\plupload\Element\PlUploadFile::valueCallback?

budalokko’s picture

Correct me if I am wrong, but all files uploaded with FileManaged element are stored both on disk and file_managed database table.

The PluploadFile element provided by the plupload module does not do any of these on it's own. It's more like the File FAPI element found at Drupal\Core\Render\Element\File which just renders a widget and takes care of files being available on the corresponding submit callback.

The responsibility for doing this kind of things (save on disk and store on database) is for the module calling it and the form's submit callback. At least it was like this in D7.

Actually there is still a "file_copy" method on D8, so this callback method could be based on it.

Task was created long time ago. Does it still make sense to write it?

veronicaSeveryn’s picture

I am not sure how to properly document my solution here or on related issues... I will just link all of them together and describe what I did to make things work with Plupload and Plupload Widget to have a solution to uploading a SINGLE file (SINGLE is tested for sure. Have not looked into MULTIPLE uploads).

I needed a Plupload Widget to be available as a Field Formatter on File Upload field. So, I utilized the DEV version of the code provided here: https://www.drupal.org/project/plupload_widget

I have a SINGLE file upload field on a content type. When using these 2 modules together as is, I was having issues with finalizing the upload and getting the file registered within managed table. So, for one of my projects purposes, I came up with a fix that does what I need. It might need to be tweaked or "prettified", but at least it works for me as of this moment with the 2 patches I have: 1 for Plupload and 1 for Plupload Widget.

For the Plupload patch, I did integrate the fix offered in this issue: https://www.drupal.org/node/2645658.

So, what I did to make it work for me:

  1. Installed and enabled Plupload and Plupload Widget (https://www.drupal.org/project/plupload_widget)
  2. Applied the patch uploaded here (plupload-save-file-managed-2245927-3.patch)
  3. Applied the patch located here: https://www.drupal.org/node/2697357
veronicaSeveryn’s picture

Status: Active » Needs review

Marking for review

RKopacz’s picture

Any news on this issue?

Gábor Hojtsy’s picture

Issue tags: -Media Initiative +D8Media
nicholas.alipaz’s picture

Chasing head

khiminrm’s picture

Hi!

Solution from #3 doesn't work for me in Drupal 8.4.2.
I use plupload widget for file field in media entity.
After saving file it has path http://mysite.loc/system/temporary?file=o_1c4ftk0m613t61okp8td1ufc16ja.tmp

Any suggestions?
Thanks!

khiminrm’s picture

I was able to upload file after changing public folder permission to 777 recursively.
But I've noticed that filename is not appeared near 'Remove' button and I upload new file, after removing uploaded one. Error occurs:
The website encountered an unexpected error. Please try again later.

Error: Call to a member function getValue() on null in Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator->validate() (line 18 of core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php).
Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator->validate(Object, Object) (Line: 185)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateConstraints(Object, '0000000059c86461000000000e57b7be', Array) (Line: 140)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode(Object) (Line: 147)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode(Object) (Line: 147)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode(Object, Array, 1) (Line: 99)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate(Object, NULL, NULL) (Line: 90)
Drupal\Core\TypedData\Validation\RecursiveValidator->validate(Object) (Line: 135)
Drupal\Core\TypedData\TypedData->validate() (Line: 411)
Drupal\Core\Entity\ContentEntityBase->validate() (Line: 272)
Drupal\media_entity\Entity\Media->validate() (Line: 180)
Drupal\Core\Entity\ContentEntityForm->validateForm(Array, Object)
call_user_func_array(Array, Array) (Line: 83)
Drupal\Core\Form\FormValidator->executeValidateHandlers(Array, Object) (Line: 274)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'media_release_edit_form') (Line: 119)
Drupal\Core\Form\FormValidator->validateForm('media_release_edit_form', Array, Object) (Line: 571)
Drupal\Core\Form\FormBuilder->processForm('media_release_edit_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('media_release_edit_form', Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 613)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 153)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 657)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
khiminrm’s picture

Errors in #9 have disappeared after flushing site's and browser's cache.
Still one bug: file is not removed from folder when it's removed in admin page from media entity or when whole media entity is removed. Cron also doesn't remove such file. How it can be fixed?

khiminrm’s picture

I've found answer for question in #10 about deleting files. It's related to https://www.drupal.org/node/2891902.

Patch from #https://www.drupal.org/project/plupload/issues/2245927#comment-11560277 works for me. Thanks!!!

Alex Zhulin’s picture

Using:
- Drupal 8.5.3;
- Plupload library 2.3.1;
- plupload 1.x-dev with applied patch from #7;
- plupload_widget 1.0 with applied patch from https://www.drupal.org/project/plupload_widget/issues/2697357#comment-11....

Some file uploads fail because the regular expression parsing element ID does not consider the underscores in the hash appended to the ID after AJAX.
Also extra records are created in the file_managed table for the same file with "temporary://..." uri.

Created a patch based on #7 to fix these issues.

Module plupload_widget requires a patch from https://www.drupal.org/project/plupload_widget/issues/2697357#comment-12... (#13).

Alex Zhulin’s picture

Updated patch from #12 to fixed file destination.

budalokko’s picture

A little part of this patch was commited in #2645658: Wrong regular expressions when processing file input. Attached patch is same as #13 for current dev.

init90’s picture

I use pluapods as a custom form field. To store the files I used the patch #14. Using this patch I found the problem with multi-file uploads, after uploads in $form_state, we have data only about the first file. The problem was in the following place:

  $fid = $form_state->get($state_key);
  if ($fid) {
    $file = File::load($fid);
    $file && $files[$i]['file'] = $file;
  }

As a result, I simply deleted the problem code snippet, because I did not understand it's purpose. After that, everything works fine. But, I am not sure about the fully correctness of this decision.

budalokko’s picture

Regarding last comment #2245927-15: Create helper function that will save uploaded files to file_managed table from @pavlo.dovhan, that patch is for compatibility between Plupload and Plupload widget module.

Most straightforward way to achieve what you wanted is do not apply the patch and simply do as in PluploadTestForm::submitForm().

dvmanjunath’s picture

Updated patch from #14 to fix deprecated errors and making it compatible with Drupal 9.