Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment | File | Size | Author |
---|---|---|---|
#4 | 2952501-4.patch | 1.21 KB | Berdir |
#2 | 2952501-2.patch | 1.2 KB | marcoscano |
Comments
Comment #2
marcoscanoComment #3
BerdirLooks good.
Comment #4
BerdirAnd the basically identical patch for 8.x-2.x.
Comment #8
Primsi CreditAttribution: Primsi at MD Systems GmbH for MD Systems GmbH commentedCommitted both. Thx.