Commit bd0f9de8 authored by Berdir's avatar Berdir Committed by Primsi

Issue #2959784 by Berdir: Add A PRECREATE event to allow to alter the media...

Issue #2959784 by Berdir: Add A PRECREATE event to allow to alter the media entities beore they are displayed in the Inline Entity Form Widget
parent 7af9fd05
......@@ -169,14 +169,18 @@ class InlineEntityFormMediaWidget extends MediaEntityDropzoneJsEbWidget {
public function submitEdit(array $form, FormStateInterface $form_state) {
$form_state->setRebuild(TRUE);
// Files have to saved before they can be viewed in the IEF form.
$media_entities = $this->prepareEntities($form, $form_state);
$source_field = $this->getType()->getSource()->getSourceFieldDefinition($this->getType())->getName();
foreach ($media_entities as $media_entity) {
foreach ($media_entities as $id => $media_entity) {
/** @var \Drupal\file\Entity\File $file */
// Files have to saved before they can be viewed in the IEF form.
$source_field = $this->getType()->getSource()->getSourceFieldDefinition($this->getType())->getName();
$file = $media_entity->$source_field->entity;
$file->save();
$media_entity->$source_field->target_id = $file->id();
/** @var \Drupal\dropzonejs\Events\DropzoneMediaEntityCreateEvent $event */
$event = $this->eventDispatcher->dispatch(Events::MEDIA_ENTITY_PRECREATE, new DropzoneMediaEntityCreateEvent($media_entity, $file, $form, $form_state, $form));
$media_entities[$id] = $event->getMediaEntity();
}
$form_state->set('uploaded_entities', $media_entities);
......@@ -231,9 +235,9 @@ class InlineEntityFormMediaWidget extends MediaEntityDropzoneJsEbWidget {
*/
public function submit(array &$element, array &$form, FormStateInterface $form_state) {
$media_entities = $this->prepareEntitiesFromForm($form, $form_state);
$source_field = $this->getType()->getSource()->getSourceFieldDefinition($this->getType())->getName();
foreach ($media_entities as $id => $media_entity) {
$source_field = $media_entity->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));
......
......@@ -17,4 +17,15 @@ final class Events {
*/
const MEDIA_ENTITY_CREATE = 'dropzonejs.media_entity_create';
/**
* The MEDIA_ENTITY_PRECREATE event.
*
* This event occurs when creating a new Media Entity,
* before it is displayed in the Inline Entity Form Widget (currently only
* used there)
*
* @var string
*/
const MEDIA_ENTITY_PRECREATE = 'dropzonejs.media_entity_precreate';
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment