Problem/Motivation

The media upload widget for Entity Browser has the following code when saving a media entity:

  /**
   * {@inheritdoc}
   */
  public function submit(array &$element, array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\media_entity\MediaInterface[] $media_entities */
    $media_entities = $this->prepareEntities($form, $form_state);
    $source_field = $this->getBundle()->getTypeConfiguration()['source_field'];

    foreach ($media_entities as &$media_entity) {
      $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->get('bundle')->entity->getTypeConfiguration()['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();
    }

    $this->selectEntities($media_entities, $form_state);
    $this->clearFormValues($element, $form_state);
  }

However, if the $media_entity received from the event changed the $source_field value, the next entity in the set (for example in a bulk upload) will try to use this modified value as $source_field, resulting in a fatal.

Proposed resolution

Reset the $source_field name from the bundle config at the beginning of each loop.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#4 2952501-4.patch1.21 KBBerdir
#2 2952501-2.patch1.2 KBmarcoscano
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

marcoscano created an issue. See original summary.

marcoscano’s picture

Status: Active » Needs review
FileSize
1.2 KB
Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Looks good.

Berdir’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
FileSize
1.21 KB

And the basically identical patch for 8.x-2.x.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 4: 2952501-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Primsi committed a9f141b on 8.x-1.x authored by marcoscano
    Issue #2952501 by marcoscano: EB widget should reset $source_field name...

  • Primsi committed ef24d75 on 8.x-2.x authored by Berdir
    Issue #2952501 by marcoscano, Berdir, Primsi: EB widget should reset $...
Primsi’s picture

Status: Needs work » Fixed

Committed both. Thx.

Status: Fixed » Closed (fixed)

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