Comments

slashrsm created an issue. See original summary.

slashrsm’s picture

StatusFileSize
new5.83 KB
samuel.mortenson’s picture

StatusFileSize
new5.9 KB

Re-roll.

samuel.mortenson’s picture

Status: Active » Needs review
slashrsm’s picture

Status: Needs review » Reviewed & tested by the community
samuel.mortenson’s picture

Title: Stop using deprecated entity manager » Update to support recent WidgetBase.php changes
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new7.32 KB
new9.11 KB

Sorry to tack this on, but DropzoneJS will still have fatal errors now that we've committed #2366335: Support defining conditions. Here's a new patch.

The last submitted patch, 3: dropzonejs-2763529-3.patch, failed testing.

marcoscano’s picture

I can confirm there is a fatal in dropzonejs (or File Entity Browser) without the patch, and that the patch in #6 applies and functionally solves the problem (everything works fine again after applying it).

slashrsm’s picture

Status: Needs review » Needs work

Patch needs a re-roll. Entity browser changed signature of the prepareEntities() again.

samuel.mortenson’s picture

Status: Needs work » Needs review
StatusFileSize
new1.55 KB
new9.14 KB

Fixed per #9.

slashrsm’s picture

Status: Needs review » Reviewed & tested by the community
samuel.mortenson’s picture

Status: Reviewed & tested by the community » Needs work

This isn't quite right - prepareEntities needs to return entities and right now it returns an array type that DropzoneJS uses to load File Entities. Doing one more re-roll with more manual testing this time.

samuel.mortenson’s picture

Status: Needs work » Needs review
StatusFileSize
new8.72 KB
new1.78 KB

Last review, promise. :-)

dunebl’s picture

I confirm that this patch solve the 'prepareEntities' problem.
But I don't know if it is related to another change in entity browser but I get a new problem:
Fatal error: Call to a member function getStorage() on a non-object in .../modules/dropzonejs/modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php on line 100

This error appears at the last stage of the setting up of the browser:
admin/config/content/entity_browser/my_browser/widgets

If it is not related, I can create a new bug report.

Thanks you for your hard work!

dunebl’s picture

Some more info:

Notice: Undefined property: Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\MediaEntityDropzoneJsEbWidget::$entityManager in Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\MediaEntityDropzoneJsEbWidget->getBundle() (line 100 of modules/dropzonejs/modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php).

Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\MediaEntityDropzoneJsEbWidget->getBundle() (Line: 117)
Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\MediaEntityDropzoneJsEbWidget->buildConfigurationForm(Array, Object) (Line: 114)
Drupal\entity_browser\Form\WidgetsConfig->buildForm(Array, Object) (Line: 261)
leksat’s picture

StatusFileSize
new10.06 KB
new1.72 KB

Replaced entityManager with entityTypeManager to fix the error described in #14.

primsi’s picture

Tested manually and works fine. Will wait a bit more for additional feedback and then commit.

dunebl’s picture

marvelous, this is perfect! works fine!

slashrsm’s picture

Status: Needs review » Needs work

There are few other changes that happened in EB. We also need to update media entity widget.

#2767867: Chase changes in entity_browser could be used as the inspiration. Note that that patch doesn't save media entities to prevent duplicates from being created. Not sure if that is the right way to go. I'd love to hear @samuel.mortenson's opinion on this.

samuel.mortenson’s picture

The docs for "prepareEntities" describe the method as something that "Prepares the entities without saving them", which is why I wrote it the way I did for this patch. Otherwise you might see strange behavior with multiple calls to that function during one bootstrap or form "session" (i.e. multiple calls to validate over multiple bootstraps).

I'm updating DropzoneJsEbWidget::getForm to match the recent widget-button changes, and doing manual testing as well. Should have a new patch today.

samuel.mortenson’s picture

Status: Needs work » Needs review
StatusFileSize
new10.58 KB
new805 bytes

New patch.

dunebl’s picture

I have not made a deep testing, but before this patch, my select button was gone... Now it is ok!
Thank you for your hard work!!!!

slashrsm’s picture

Status: Needs review » Needs work

We should re-implement prepareEntities() in MediaEntityDropzoneJsEbWidget to return media entities and not files (which is what inherited function does at the moment).

samuel.mortenson’s picture

If we re-implement prepareEntities(), we would also have to re-implement the validation logic that the parent class uses, or would have to have two prepareEntities() functions - one for the submit callback and one for the validate callback. I'm not sure what the cleanest way to do this is. I guess the question here is - should the MediaEntityDropzoneJsEbWidget widget validate Files, Media Entities, or somehow both?

slashrsm’s picture

Validating media entities is the most obvious choice I think. If the entity type constraint is provided to the Entity browser then validation won't even pass if we validate files, right?

mtodor’s picture

I have looked also in this issue and it's a bit tricky to get clean solution for prepareEntities() that will return media entities.

Problem is related with 'media_entity' module dependancy, because when that module is not enabled, files will still be uploaded and saved. I don't know is that wanted functionality or not, but best would be that media entity drop zone can't work without that module at all. Then you have clean solution for prepareEntities() that always returns media entities and there is no need for check in submit(), etc.

Regarding validation, I think it should be sufficient to trigger media entity validation, file size/extensions will be anyway checked when file is dropped in zone, based on configuration defined for that widget in EB. So validate() should be re-implemented too and it can be same as in base widget, I guess.

btw. code in DropzoneJsEbWidget.php:
if ($trigger['#value'] == 'Select') {
will not work nice, it would be better to use something else, for example:
if ($trigger['#name'] == 'op') {

slashrsm’s picture

Yes, this widget doesn't make any sense if media_entity is disabled. Will it help if we update the plugin to declare the module as its dependency? I am not sure how exactly that works, but I think that this dependencies should bubble up to the main config entity which should then require the module.

primsi’s picture

The media entity eb widget should be a separate submodule, or handle the dependency in a different way. I am not sure why we didn't do it like that in the first place (probably because it was more of a quick and dirty thing we started with).

mtodor’s picture

I agree, cleanest solution would be separate submodule with properly defined dependencies.

slashrsm’s picture

Status: Needs work » Needs review
StatusFileSize
new10.92 KB
new746 bytes

I'd like to prevent further fragmentation. Having a gazillion of submodules will make the (already complex) ecosystem even harder to understand. I'd even go into the opposite direction. Ideally I'd like to see plugins moved into the main dropzone module and dropzone_eb_widgets being removed altogether.

If plugins correctly provide dependencies that they need (see patch) we can filter the ones with missing dependencies out before displaying them in the UI. Would that work?

I think that we should solve this in a separate issue.

mtodor’s picture

StatusFileSize
new859 bytes
new11.07 KB

That is possible solution too, only thing that I don't like is coupling of things that potentially will never be used, but that's meaning of plugins.

In that case two tickets are needed:
1. entity browser should be aware of widget dependencies
2. move plugins into dropzonejs module (remove submodule(s))

I have tested patch and it works.
More or less functionality is same as for #21 and I have just adjusted "if" statement otherwise we have dead code there.

slashrsm’s picture

StatusFileSize
new17.56 KB
new9.85 KB

Media entity widget needs to be updated to work with media entities instead of files. It turned out this was trickier than expected, but I think that I managed to make it work.

mtodor’s picture

I have tested changes from #32, it works for me.

Regarding code:

+++ b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php
@@ -150,44 +165,37 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
+      $file = $media_entity->$source_field->entity = $file;

Can $file be changed in process when it's assign to entity field?
If yes, then it would be nice to make that line more readable (split in 2 lines or assign result from file_move to entity field directly and then assign $file from it).

+++ b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php
@@ -114,8 +126,53 @@ class DropzoneJsEbWidget extends WidgetBase {
+  public function prepareEntities(array $form, FormStateInterface $form_state) {

Is there need for redeclare of prepareEntities() to be public (because it's defined protected in interface)?

And also code that is commented out should be removed.

Nice work!!!

royal121’s picture

Status: Needs review » Needs work

To test this, I installed the latest Entity Browser module and Dropzone. I tried adding the Media entity Dropzone widget to an Entity browser. I received the following error without the patch:

Fatal error: Class Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\DropzoneJsEbWidget contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Drupal\entity_browser\WidgetBase::prepareEntities) in /var/www/html/drupaltest/modules/dropzonejs/modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php on line 28

After applying the patch:

Uncaught PHP Exception Drupal\\Component\\Plugin\\Exception\\PluginNotFoundException: "The "media_bundle" entity type does not exist." at /var/www/html/drupaltest/core/lib/Drupal/Core/Entity/EntityTypeManager.php line 125, referer: http://localhost/drupaltest/admin/config/content/entity_browser/blah/wid...

After I installed the media entity module:

Uncaught PHP Exception Symfony\\Component\\Routing\\Exception\\RouteNotFoundException: "Route "media.bundle_add" does not exist." at /var/www/html/drupaltest/core/lib/Drupal/Core/Routing/RouteProvider.php line 187

I cleared the cache, rebuild the router and also re-installed all these modules but still received the same error.

mtodor’s picture

@royal121 - did you create any media bundle?
Because you need at least one to be available for selection when you create Media Entity DropzoneJS widget.

slashrsm’s picture

I was discussing this with @primsi today. It seems that we introduced a regression at some point. Dropzone should take care about moving files from temporary folder. We will look at this and update the patch.

slashrsm’s picture

Status: Needs work » Needs review
StatusFileSize
new26.05 KB
new19.23 KB

This should be much nicer solution. Also fixed few code sniffer's complains.

slashrsm’s picture

+++ b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php
@@ -114,37 +137,84 @@ class DropzoneJsEbWidget extends WidgetBase {
+      $files = $this->getFiles($form, $form_state);
+      if ($files === FALSE) {
+        // @todo Output the actual errors from validateFile.
+        $form_state->setError($form['widget']['upload'], t('Some files that you are trying to upload did not pass validation. Requirements are: max file %size, allowed extensions are %extensions', ['%size' => $this->getConfiguration()['settings']['max_filesize'], '%extensions' => $this->getConfiguration()['settings']['extensions']]));

To do this we need to change behaviour of upload save service. It currently returns FALSE if there is an error while it should throw exceptions IMO.

But this is not the scope of this issue.

mtodor’s picture

Status: Needs review » Needs work

I did quick check of patch (not full review):

  1. +++ b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php
    @@ -114,37 +137,84 @@ class DropzoneJsEbWidget extends WidgetBase {
    +      if (!file_prepare_directory($this->getUploadLocation(), FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    

    PHP complains in this case, because variable should be passed as reference.

  2. +++ b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php
    @@ -150,44 +165,35 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
    +      $media_entity->save();
    

     
    And execution of this line triggers warning for me:
    Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 227 of ../core/lib/Drupal/Core/Entity/EntityStorageBase.php).

    In this case $ids is array with one element and it's NULL. Here is shortened stack trace:

    ...
    24. Drupal\\entity_browser\\Form\\EntityBrowserForm->submitForm() ../core/lib/Drupal/Core/Form/FormSubmitter.php:111
    25. Drupal\\dropzonejs_eb_widget\\Plugin\\EntityBrowser\\Widget\\MediaEntityDropzoneJsEbWidget->submit() ../modules/entity_browser/src/Form/EntityBrowserForm.php:163
    26. Drupal\\Core\\Entity\\Entity->save() ../modules/dropzonejs/modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php:196
    ...
    32. Drupal\\media_entity\\Entity\\Media->automaticallySetThumbnail() ../modules/media_entity/src/Entity/Media.php:185
    33. Drupal\\media_entity_image\\Plugin\\MediaEntity\\Type\\Image->thumbnail() ../modules/media_entity/src/Entity/Media.php:220
    34. Drupal\\Core\\Entity\\EntityStorageBase->load() ../modules/media_entity_image/src/Plugin/MediaEntity/Type/Image.php:209
    35. Drupal\\Core\\Entity\\EntityStorageBase->loadMultiple()

And with same setup I don't get any errors with #32 patch.

slashrsm’s picture

StatusFileSize
new26.1 KB
new1.66 KB

Thanks!

#39.1 - Fixed
#39.2 - One possible source of this (and an actual bug in validation) fixed. However, I am not sure if this was the real source. Could you make sure to test this on a clean install with file systems permissions set correctly? Could you also provide more detailed steps to reproduce it?

dagmar’s picture

Status: Needs work » Needs review
primsi’s picture

I have tested this today and overall looks great. We can wait for additional feedback but I think we are almost done here.

berdir’s picture

Just some questions/thoughts, I don't know the surrounding code too well.

  1. +++ b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php
    @@ -114,37 +137,85 @@ class DropzoneJsEbWidget extends WidgetBase {
         $config = $this->getConfiguration();
    +    // @todo Check per user size allowance.
    +    $additional_validators = ['file_validate_size' => [Bytes::toInt($config['settings']['max_filesize']), 0]];
    

    I thought per-user size was removed in Drupal 6 or so, who would re-add this, dropzone specific?

  2. +++ b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php
    @@ -279,4 +344,12 @@ class DropzoneJsEbWidget extends WidgetBase {
    +   */
    +  public function __sleep() {
    +    return array_diff(parent::__sleep(), ['files']);
    +  }
    +
    

    storing those files makes things a bit more complicated, I guess that's needed because getFiles() is called more than once?

  3. +++ b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php
    @@ -195,4 +201,12 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
    +   * {@inheritdoc}
    +   */
    +  public function __sleep() {
    +    return array_diff(parent::__sleep(), ['bundle']);
    +  }
    

    hm, avoing the bundle property should be easier, though. is it really worth statically caching that?

    config entities do optionally support static cache, you just have to enable it. trying to enable it by default showed a few side effects though that we haven't yet figured out (that's why it didn't happen).

    also, the config objects themself are already statically cached.

  4. +++ b/src/DropzoneJsUploadSaveInterface.php
    @@ -27,32 +22,21 @@ interface DropzoneJsUploadSaveInterface {
    -   *   A new entity file entity object, not saved yet.
    -   */
    -  public function fileEntityFromUri($uri, AccountProxyInterface $user);
    +  public function saveFile($uri, $destination, $extensions, AccountProxyInterface $user, $validators = [], $save_file = TRUE);
    

    I haven't checked how often this is saved vs not, but maybe instead of an optional argument, saving could be done by the caller if it is only a few cases. a different name might also make sense then.

dawehner’s picture

I worked with this patch the entire day and it seemed to work quite well.

ada hernandez’s picture

Could @dawehner please share which versions about the dependencies of media entity are using, I want to use media entity, I have some errors with the combinations of modules, thks

slashrsm’s picture

StatusFileSize
new7.84 KB
new7.84 KB

Thank you for the review!

#43.1 - Removed @todo
#43.2 - Yes, I wanted to avoid validate callback in the widget and generic validators code in the base widget class to run on different entities. For paranoia sake mostly....
#43.3 - Refactored and bundle property is now gone.
#43.4 - Makes sense. It is used only here AFAIK. Lightning could potentially use it, but I checked and it seems that they do not. Removed save option and renamed the function.

I also figured out that now Media entity creates new file for thumbnail since the main file is not saved yet when thumbnail stuff happens. We end up with two file entities sharing the same URI (which is strange since there is a unique constraint on URI...). Added workaround for now. This should be fixed in ME, but the problem is that this logic happens in plugins (image, audio, document, ...).

@Adita: latest -dev of all relevant modules (Entity browser, Entity embed, Media entity, Media entity image, ...) should do the job.

Status: Needs review » Needs work

The last submitted patch, 46: 2763529_46.patch, failed testing.

The last submitted patch, 46: 2763529_46.patch, failed testing.

slashrsm’s picture

Status: Needs work » Needs review
StatusFileSize
new27.14 KB
new7.84 KB

Wrong patch.

berdir’s picture

+++ b/src/DropzoneJsUploadSave.php
@@ -158,30 +162,22 @@ class DropzoneJsUploadSave implements DropzoneJsUploadSaveInterface {
    */
-  public function fileEntityFromUri($uri, AccountProxyInterface $user) {
-    $uri = file_stream_wrapper_uri_normalize($uri);
-    $file_info = new \SplFileInfo($uri);
+  protected function fileEntityFromUri($uri, AccountProxyInterface $user) {
 
-    // Begin building file entity.
-    $values = [
-      'uid' => $user->id(),
-      'status' => 0,
-      'filename' => $file_info->getFilename(),
-      'uri' => $uri,
-      'filesize' => $file_info->getSize(),
-      'filemime' => $this->mimeTypeGuesser->guess($uri),
-    ];
-
-    /** @var \Drupal\file\FileInterface $file */
-    $file = $this->entityManager->getStorage('file')->create($values);
     return $file;
   }

And so God spoke: "Though shall be a file". And so it was.

I think you removed a bit too much here :) (or more likely, didn't replace with something else)

slashrsm’s picture

StatusFileSize
new26.78 KB
new812 bytes

It turns out I didn't remove enough. I moved entire body from fileEntityFromUri() to createFile() since it was the only caller.

rakesh falke’s picture

Facing issue WSOD while installing "File Browser" module.Confirm that 2763529_51.patch patch working properly to install module without any issue.

berdir’s picture

@slashrsm: Fine from my POV. I haven't tested it but I think others did :)

dagmar’s picture

I'm going to provide feedback about the behavior of this patch tomorrow if you want to wait for another review.

  • Primsi committed e28bb33 on 8.x-1.x authored by slashrsm
    Issue #2763529 by slashrsm, samuel.mortenson, mtodor, Leksat, DuneBL,...
primsi’s picture

Merged. Thanks to all for this awesome work!

primsi’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.