X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fmedia_entity_document%2Fsrc%2FPlugin%2FEntityBrowser%2FWidget%2FUpload.php;h=b377b6df26904732d8b0cf6d0ad501c90cef5dfe;hp=70d572774678cdf0835864abc933d7ed428856b9;hb=9e65bae52407293a5182f19dc57b5628b09e92f4;hpb=af6d1fb995500ae68849458ee10d66abbdcfb252 diff --git a/web/modules/contrib/media_entity_document/src/Plugin/EntityBrowser/Widget/Upload.php b/web/modules/contrib/media_entity_document/src/Plugin/EntityBrowser/Widget/Upload.php index 70d572774..b377b6df2 100644 --- a/web/modules/contrib/media_entity_document/src/Plugin/EntityBrowser/Widget/Upload.php +++ b/web/modules/contrib/media_entity_document/src/Plugin/EntityBrowser/Widget/Upload.php @@ -3,9 +3,8 @@ namespace Drupal\media_entity_document\Plugin\EntityBrowser\Widget; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; use Drupal\entity_browser\Plugin\EntityBrowser\Widget\Upload as FileUpload; -use Drupal\media_entity\MediaInterface; +use Drupal\Core\Url; /** * Uses upload to create media entity documents. @@ -24,7 +23,7 @@ class Upload extends FileUpload { public function defaultConfiguration() { return [ 'extensions' => 'txt, pdf', - 'media_bundle' => NULL, + 'media bundle' => NULL, ] + parent::defaultConfiguration(); } @@ -33,7 +32,7 @@ class Upload extends FileUpload { */ public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) { /** @var \Drupal\media_entity\MediaBundleInterface $bundle */ - if (!$this->configuration['media_bundle'] || !($bundle = $this->entityTypeManager->getStorage('media_bundle')->load($this->configuration['media_bundle']))) { + if (!$this->configuration['media bundle'] || !($bundle = $this->entityManager->getStorage('media_bundle')->load($this->configuration['media bundle']))) { return ['#markup' => $this->t('The media bundle is not configured correctly.')]; } @@ -42,7 +41,7 @@ class Upload extends FileUpload { } $form = parent::getForm($original_form, $form_state, $aditional_widget_parameters); - $form['upload']['#upload_validators']['file_validate_extensions'] = [$this->configuration['extensions']]; + $form['upload']['upload_validators']['file_validate_extensions'] = [$this->configuration['extensions']]; return $form; } @@ -50,42 +49,33 @@ class Upload extends FileUpload { /** * {@inheritdoc} */ - protected function prepareEntities(array $form, FormStateInterface $form_state) { - $files = parent::prepareEntities($form, $form_state); + public function submit(array &$element, array &$form, FormStateInterface $form_state) { + $documents = []; /** @var \Drupal\media_entity\MediaBundleInterface $bundle */ - $bundle = $this->entityTypeManager + $bundle = $this->entityManager ->getStorage('media_bundle') - ->load($this->configuration['media_bundle']); + ->load($this->configuration['media bundle']); + $files = $this->extractFiles($form_state); - $documents = []; foreach ($files as $file) { /** @var \Drupal\media_entity\MediaInterface $document */ - $document = $this->entityTypeManager->getStorage('media')->create([ + $document = $this->entityManager->getStorage('media')->create([ 'bundle' => $bundle->id(), $bundle->getTypeConfiguration()['source_field'] => $file, ]); + $filename = $file->filename->value; + if ($filename) { + $document->set('name', $filename); + } + + $document->save(); $documents[] = $document; } - return $documents; - } - - /** - * {@inheritdoc} - */ - public function submit(array &$element, array &$form, FormStateInterface $form_state) { - if (!empty($form_state->getTriggeringElement()['#eb_widget_main_submit'])) { - $documents = $this->prepareEntities($form, $form_state); - array_walk( - $documents, - function (MediaInterface $media) { $media->save(); } - ); - - $this->selectEntities($documents, $form_state); - $this->clearFormValues($element, $form_state); - } + $this->selectEntities($documents, $form_state); + $this->clearFormValues($element, $form_state); } /** @@ -102,28 +92,36 @@ class Upload extends FileUpload { '#required' => TRUE, ]; - $bundles = $this->entityTypeManager + $bundles = $this->entityManager ->getStorage('media_bundle') ->loadByProperties(['type' => 'document']); - /** @var \Drupal\media_entity\MediaBundleInterface $bundle */ foreach ($bundles as $bundle) { $bundle_options[$bundle->id()] = $bundle->label(); } - if (empty($bundle_options)) { - $url = Url::fromRoute('media.bundle_add')->toString(); - $form['media_bundle'] = [ - '#markup' => $this->t("You don't have media bundle of the Document type. You should create one", ['!link' => $url]), - ]; - } - else { - $form['media_bundle'] = [ - '#type' => 'select', - '#title' => $this->t('Media bundle'), - '#default_value' => $this->configuration['media_bundle'], - '#options' => $bundle_options, - ]; + switch (count($bundle_options)) { + case 0: + $url = Url::fromRoute('media.bundle_add')->toString(); + $form['media bundle'] = [ + '#markup' => $this->t("You don't have media bundle of the Document type. You should create one", ['!link' => $url]), + ]; + break; + + case 1: + $form['media bundle'] = [ + '#value' => key($bundle_options), + '#type' => 'value', + ]; + break; + + default: + $form['media bundle'] = [ + '#type' => 'select', + '#title' => $this->t('Media bundle'), + '#default_value' => $this->configuration['media bundle'], + '#options' => $bundle_options, + ]; } return $form;