Commit f39bed05 authored by chr.fritsch's avatar chr.fritsch Committed by chr.fritsch

Issue #2897255 by mtodor, chr.fritsch, peter.keppert: Support Media core...

Issue #2897255 by mtodor, chr.fritsch, peter.keppert: Support Media core module (Drupal core 8.4.x-dev)
parent 68e83524
......@@ -25,7 +25,7 @@ entity_browser.browser.widget.dropzonejs_media_entity:
type: mapping
label: 'Media Entity DropzoneJS widget configuration'
mapping:
media_entity_bundle:
media_type:
type: string
label: 'Media type'
submit_text:
......@@ -51,7 +51,7 @@ entity_browser.browser.widget.dropzonejs_media_entity_inline_entity_form:
type: mapping
label: 'Media Entity DropzoneJS with edit widget configuration'
mapping:
media_entity_bundle:
media_type:
type: string
label: 'Media type'
submit_text:
......
<?php
/**
* @file
* Install function hooks for the DropzoneJS entity browser widget module.
*/
use Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\MediaEntityDropzoneJsEbWidget;
use Drupal\entity_browser\Entity\EntityBrowser;
/**
* Update configurations for configuration key renaming.
*/
function dropzonejs_eb_widget_update_8201() {
$logger = \Drupal::logger('dropzonejs');
/** @var \Drupal\entity_browser\Entity\EntityBrowser[] $entity_browsers */
$entity_browsers = EntityBrowser::loadMultiple();
foreach ($entity_browsers as $entity_browser) {
/** @var \Drupal\entity_browser\WidgetInterface[] $widgets */
$widgets = $entity_browser->getWidgets();
$has_config_changes = FALSE;
foreach ($widgets as $widget) {
if ($widget instanceof MediaEntityDropzoneJsEbWidget) {
$config = $widget->getConfiguration();
if (!empty($config['settings']['media_entity_bundle']) && empty($config['settings']['media_type'])) {
$config['settings']['media_type'] = $config['settings']['media_entity_bundle'];
unset($config['settings']['media_entity_bundle']);
$widget->setConfiguration($config);
$has_config_changes = TRUE;
$logger->info(sprintf('Configuration changed for widget (%s) of entity browser (%s)', $widget->label(), $entity_browser->label()));
}
else {
$logger->warning(sprintf('Unable to change configuration for widget (%s) of entity browser (%s)', $widget->label(), $entity_browser->label()));
}
}
}
if ($has_config_changes) {
$entity_browser->save();
}
}
}
......@@ -171,7 +171,7 @@ class InlineEntityFormMediaWidget extends MediaEntityDropzoneJsEbWidget {
// Files have to saved before they can be viewed in the IEF form.
$media_entities = $this->prepareEntities($form, $form_state);
$source_field = $this->getBundle()->getTypeConfiguration()['source_field'];
$source_field = $this->getType()->getSource()->getSourceFieldDefinition($this->getType())->getName();
foreach ($media_entities as $media_entity) {
/** @var \Drupal\file\Entity\File $file */
$file = $media_entity->$source_field->entity;
......@@ -211,7 +211,7 @@ class InlineEntityFormMediaWidget extends MediaEntityDropzoneJsEbWidget {
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return \Drupal\media_entity\MediaInterface[]
* @return \Drupal\media\MediaInterface[]
* The prepared media entities.
*/
protected function prepareEntitiesFromForm(array $form, FormStateInterface $form_state) {
......@@ -231,7 +231,7 @@ class InlineEntityFormMediaWidget extends MediaEntityDropzoneJsEbWidget {
*/
public function submit(array &$element, array &$form, FormStateInterface $form_state) {
$media_entities = $this->prepareEntitiesFromForm($form, $form_state);
$source_field = $this->getBundle()->getTypeConfiguration()['source_field'];
$source_field = $this->getType()->getSource()->getSourceFieldDefinition($this->getType())->getName();
foreach ($media_entities as $media_entity) {
$file = $media_entity->{$source_field}->entity;
......
......@@ -16,10 +16,10 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Provides an Entity Browser widget that uploads uploads media entities.
* Provides an Entity Browser widget that uploads media entities.
*
* Widget will upload files and attach them to the media entity of bundle that
* is defined in the configuration.
* Widget will upload files and attach them to the media entity of type that is
* defined in the configuration.
*
* @EntityBrowserWidget(
* id = "dropzonejs_media_entity",
......@@ -89,20 +89,20 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
*/
public function defaultConfiguration() {
return [
'media_entity_bundle' => '',
'media_type' => '',
] + parent::defaultConfiguration();
}
/**
* Returns the media bundle that this widget creates.
* Returns the media type that this widget creates.
*
* @return \Drupal\media_entity\MediaBundleInterface
* Media bundle.
* @return \Drupal\media\MediaTypeInterface
* Media type.
*/
protected function getBundle() {
protected function getType() {
return $this->entityTypeManager
->getStorage('media_bundle')
->load($this->configuration['media_entity_bundle']);
->getStorage('media_type')
->load($this->configuration['media_type']);
}
/**
......@@ -111,29 +111,29 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['media_entity_bundle'] = [
$form['media_type'] = [
'#type' => 'select',
'#title' => $this->t('Media type'),
'#required' => TRUE,
'#description' => $this->t('The type of media entity to create from the uploaded file(s).'),
];
$bundle = $this->getBundle();
if ($bundle) {
$form['media_entity_bundle']['#default_value'] = $bundle->id();
$media_type = $this->getType();
if ($media_type) {
$form['media_type']['#default_value'] = $media_type->id();
}
$bundles = $this->entityTypeManager->getStorage('media_bundle')->loadMultiple();
$media_types = $this->entityTypeManager->getStorage('media_type')->loadMultiple();
if (!empty($bundles)) {
foreach ($bundles as $bundle) {
$form['media_entity_bundle']['#options'][$bundle->id()] = $bundle->label();
if (!empty($media_types)) {
foreach ($media_types as $media_type) {
$form['media_type']['#options'][$media_type->id()] = $media_type->label();
}
}
else {
$form['media_entity_bundle']['#disabled'] = TRUE;
$form['media_entity_bundle']['#description'] = $this->t('You must @create_bundle before using this widget.', [
'@create_bundle' => Link::createFromRoute($this->t('create a media bundle'), 'media.bundle_add')->toString(),
$form['media_type']['#disabled'] = TRUE;
$form['media_type']['#description'] = $this->t('You must @create_media_type before using this widget.', [
'@create_media_type' => Link::createFromRoute($this->t('create a media type'), 'media.add')->toString(),
]);
}
......@@ -146,10 +146,10 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
// Depend on the media bundle this widget creates.
$bundle = $this->getBundle();
$dependencies[$bundle->getConfigDependencyKey()][] = $bundle->getConfigDependencyName();
$dependencies['module'][] = 'media_entity';
// Depend on the media type this widget creates.
$media_type = $this->getType();
$dependencies[$media_type->getConfigDependencyKey()][] = $media_type->getConfigDependencyName();
$dependencies['module'][] = 'media';
return $dependencies;
}
......@@ -159,15 +159,15 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
*/
public function prepareEntities(array $form, FormStateInterface $form_state) {
$entities = [];
$bundle = $this->getBundle();
$media_type = $this->getType();
foreach (parent::prepareEntities($form, $form_state) as $file) {
$entities[] = $this->entityTypeManager->getStorage('media')->create([
'bundle' => $bundle->id(),
$bundle->getTypeConfiguration()['source_field'] => $file,
'bundle' => $media_type->id(),
$media_type->getSource()->getConfiguration()['source_field'] => $file,
'uid' => $this->currentUser->id(),
'status' => TRUE,
'type' => $bundle->getType()->getPluginId(),
'type' => $media_type->getSource()->getPluginId(),
]);
}
......@@ -178,16 +178,16 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
* {@inheritdoc}
*/
public function submit(array &$element, array &$form, FormStateInterface $form_state) {
/** @var \Drupal\media_entity\MediaInterface[] $media_entities */
/** @var \Drupal\media\MediaInterface[] $media_entities */
$media_entities = $this->prepareEntities($form, $form_state);
$source_field = $this->getBundle()->getTypeConfiguration()['source_field'];
$source_field = $this->getType()->getSource()->getConfiguration()['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'];
$source_field = $media_entity->getSource()->getConfiguration()['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.
......
......@@ -4,7 +4,7 @@ namespace Drupal\dropzonejs\Events;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\FileInterface;
use Drupal\media_entity\MediaInterface;
use Drupal\media\MediaInterface;
use Symfony\Component\EventDispatcher\Event;
/**
......@@ -15,7 +15,7 @@ class DropzoneMediaEntityCreateEvent extends Event {
/**
* The media entity being created.
*
* @var \Drupal\media_entity\MediaInterface
* @var \Drupal\media\MediaInterface
*/
protected $mediaEntity;
......@@ -50,7 +50,7 @@ class DropzoneMediaEntityCreateEvent extends Event {
/**
* DropzoneMediaEntityCreateEvent constructor.
*
* @param \Drupal\media_entity\MediaInterface $media_entity
* @param \Drupal\media\MediaInterface $media_entity
* The media entity being created.
* @param \Drupal\file\FileInterface $file
* The file that will be used for the media entity.
......@@ -72,7 +72,7 @@ class DropzoneMediaEntityCreateEvent extends Event {
/**
* Get the media entity.
*
* @return \Drupal\media_entity\MediaInterface
* @return \Drupal\media\MediaInterface
* A media entity.
*/
public function getMediaEntity() {
......@@ -82,7 +82,7 @@ class DropzoneMediaEntityCreateEvent extends Event {
/**
* Set the media entity.
*
* @param \Drupal\media_entity\MediaInterface $media_entity
* @param \Drupal\media\MediaInterface $media_entity
* The updated media entity.
*/
public function setMediaEntity(MediaInterface $media_entity) {
......
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