Problem/Motivation

In InlineEntityFormMediaWidget::submit() we have:

  /**
   * {@inheritdoc}
   */
  public function submit(array &$element, array &$form, FormStateInterface $form_state) {
    $media_entities = $this->prepareEntitiesFromForm($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();
      $media_entity->save();
    }

    if (!empty(array_filter($media_entities))) {
      $this->selectEntities($media_entities, $form_state);
      $this->clearFormValues($element, $form_state);
    }
  }

which is dispatching a MEDIA_ENTITY_CREATE event which allows the entity to be modified. We get the entity back from the event and save it, but we fail to keep this new value when passing the entities into the selection widget, because the for loop isn't looping by reference.

Proposed resolution

Fix this by passing to ::selectEntities() the modified entity, instead of the original one.

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

marcoscano created an issue. See original summary.

marcoscano’s picture

Assigned: marcoscano » Unassigned
Status: Active » Needs review
FileSize
1.02 KB
1.04 KB
Berdir’s picture

Not too happy about a by-reference variable assignment in a foreach loop like that.

What about explicitly assigning it back? Change the loop to as $id => $media_entity and then $media_entity[$id] = $media_entity;.

That's additional line but IMHO easier to read.

marcoscano’s picture

FileSize
2.47 KB
2.49 KB

No problems, changed the other widget as well to be consistent.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Thanks. Not sure what the problem with the 8.x-2.x branch test is.

  • Primsi committed bd85dfc on 8.x-2.x authored by marcoscano
    Issue #2956659 by marcoscano, Berdir: InlineEntityFormMediaWidget...

  • Primsi committed 3d71af4 on 8.x-1.x authored by marcoscano
    Issue #2956659 by marcoscano, Berdir: InlineEntityFormMediaWidget...
Primsi’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

Status: Fixed » Closed (fixed)

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