You are here

public function MediaEntityDropzoneJsEbWidget::submit in DropzoneJS 8.2

Same name and namespace in other branches
  1. 8 modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php \Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\MediaEntityDropzoneJsEbWidget::submit()

Overrides DropzoneJsEbWidget::submit

1 method overrides MediaEntityDropzoneJsEbWidget::submit()
InlineEntityFormMediaWidget::submit in modules/eb_widget/src/Plugin/EntityBrowser/Widget/InlineEntityFormMediaWidget.php

File

modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php, line 152

Class

MediaEntityDropzoneJsEbWidget
Provides an Entity Browser widget that uploads media entities.

Namespace

Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget

Code

public function submit(array &$element, array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\media\MediaInterface[] $media_entities */
  $media_entities = $this
    ->prepareEntities($form, $form_state);
  foreach ($media_entities as $id => $media_entity) {
    $source_field = $this
      ->getType()
      ->getSource()
      ->getConfiguration()['source_field'];
    $file = $media_entity->{$source_field}->entity;

    /** @var \Drupal\dropzonejs\Events\DropzoneMediaEntityCreateEvent $event */
    $event = $this->eventDispatcher
      ->dispatch(Events::MEDIA_ENTITY_CREATE, new DropzoneMediaEntityCreateEvent($media_entity, $file, $form, $form_state, $element));
    $media_entity = $event
      ->getMediaEntity();
    $source_field = $media_entity
      ->getSource()
      ->getConfiguration()['source_field'];

    // If we don't save file at this point Media entity creates another file
    // entity with same uri for the thumbnail. That should probably be fixed
    // in Media entity, but this workaround should work for now.
    $media_entity->{$source_field}->entity
      ->save();
    $media_entity
      ->save();
    $media_entities[$id] = $media_entity;
  }
  $this
    ->selectEntities($media_entities, $form_state);
  $this
    ->clearFormValues($element, $form_state);
}