Commit ffc1160f authored by phenaproxima's avatar phenaproxima Committed by Primsi
Browse files

Issue #2722517 by phenaproxima, Primsi: EB widget has no way to configure the bundle

parent edbc6875
......@@ -9,6 +9,7 @@ namespace Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget;
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxyInterface;
......@@ -30,6 +31,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
* )
*/
class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
/**
* Module handler service.
*
......@@ -53,7 +55,7 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler intarface.
* The module handler service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityManagerInterface $entity_manager, DropzoneJsUploadSaveInterface $dropzonejs_upload_save, AccountProxyInterface $current_user, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_manager, $dropzonejs_upload_save, $current_user);
......@@ -86,6 +88,54 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
] + parent::defaultConfiguration();
}
/**
* Returns the media bundle that this widget creates.
*
* @return \Drupal\media_entity\MediaBundleInterface
*/
protected function getBundle() {
return $this->entityManager->getStorage('media_bundle')
->load($this->configuration['media_entity_bundle']);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['media_entity_bundle'] = [
'#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();
}
$bundles = $this->entityManager->getStorage('media_bundle')->loadMultiple();
foreach ($bundles as $bundle) {
$form['media_entity_bundle']['#options'][$bundle->id()] = $bundle->label();
}
return $form;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
// Depend on the media bundle this widget creates.
$bundle = $this->getBundle();
$dependencies[$bundle->getConfigDependencyKey()][] = $bundle->getConfigDependencyName();
return $dependencies;
}
/**
* {@inheritdoc}
*/
......@@ -96,10 +146,7 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
$config = $this->getConfiguration();
$user = $this->currentUser;
/** @var \Drupal\media_entity\MediaBundleInterface $bundle */
$bundle = $this->entityManager
->getStorage('media_bundle')
->load($this->configuration['media_entity_bundle']);
$bundle = $this->getBundle();
// First save the file.
foreach ($upload['uploaded_files'] as $uploaded_file) {
......@@ -137,4 +184,5 @@ class MediaEntityDropzoneJsEbWidget extends DropzoneJsEbWidget {
$this->clearFormValues($element, $form_state);
}
}
}
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