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
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 2956659-4-2.x.patch | 2.49 KB | marcoscano |
| #4 | 2956659-4-1.x.patch | 2.47 KB | marcoscano |
| #2 | 2956659-2-2.x.patch | 1.04 KB | marcoscano |
| #2 | 2956659-2-1.x.patch | 1.02 KB | marcoscano |
Comments
Comment #2
marcoscanoComment #3
berdirNot 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.
Comment #4
marcoscanoNo problems, changed the other widget as well to be consistent.
Comment #5
berdirThanks. Not sure what the problem with the 8.x-2.x branch test is.
Comment #8
primsi commentedThanks!