3 namespace Drupal\media_entity_document\Plugin\EntityBrowser\Widget;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\entity_browser\Plugin\EntityBrowser\Widget\Upload as FileUpload;
10 * Uses upload to create media entity documents.
12 * @EntityBrowserWidget(
13 * id = "media_entity_document_upload",
14 * label = @Translation("Upload documents"),
15 * description = @Translation("Upload widget that creates media entity documents.")
18 class Upload extends FileUpload {
23 public function defaultConfiguration() {
25 'extensions' => 'txt, pdf',
26 'media bundle' => NULL,
27 ] + parent::defaultConfiguration();
33 public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) {
34 /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
35 if (!$this->configuration['media bundle'] || !($bundle = $this->entityManager->getStorage('media_bundle')->load($this->configuration['media bundle']))) {
36 return ['#markup' => $this->t('The media bundle is not configured correctly.')];
39 if ($bundle->getType()->getPluginId() != 'document') {
40 return ['#markup' => $this->t('The configured bundle is not using document plugin.')];
43 $form = parent::getForm($original_form, $form_state, $aditional_widget_parameters);
44 $form['upload']['upload_validators']['file_validate_extensions'] = [$this->configuration['extensions']];
52 public function submit(array &$element, array &$form, FormStateInterface $form_state) {
55 /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
56 $bundle = $this->entityManager
57 ->getStorage('media_bundle')
58 ->load($this->configuration['media bundle']);
59 $files = $this->extractFiles($form_state);
61 foreach ($files as $file) {
62 /** @var \Drupal\media_entity\MediaInterface $document */
63 $document = $this->entityManager->getStorage('media')->create([
64 'bundle' => $bundle->id(),
65 $bundle->getTypeConfiguration()['source_field'] => $file,
68 $filename = $file->filename->value;
70 $document->set('name', $filename);
74 $documents[] = $document;
77 $this->selectEntities($documents, $form_state);
78 $this->clearFormValues($element, $form_state);
84 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
86 $form = parent::buildConfigurationForm($form, $form_state);
88 $form['extensions'] = [
89 '#type' => 'textfield',
90 '#title' => $this->t('Allowed extensions'),
91 '#default_value' => $this->configuration['extensions'],
95 $bundles = $this->entityManager
96 ->getStorage('media_bundle')
97 ->loadByProperties(['type' => 'document']);
99 foreach ($bundles as $bundle) {
100 $bundle_options[$bundle->id()] = $bundle->label();
103 switch (count($bundle_options)) {
105 $url = Url::fromRoute('media.bundle_add')->toString();
106 $form['media bundle'] = [
107 '#markup' => $this->t("You don't have media bundle of the Document type. You should <a href='!link'>create one</a>", ['!link' => $url]),
112 $form['media bundle'] = [
113 '#value' => key($bundle_options),
119 $form['media bundle'] = [
121 '#title' => $this->t('Media bundle'),
122 '#default_value' => $this->configuration['media bundle'],
123 '#options' => $bundle_options,