Updating Media dependent modules to versions compatible with core Media.
[yaffs-website] / web / modules / contrib / media_entity_document / src / Plugin / EntityBrowser / Widget / Upload.php
1 <?php
2
3 namespace Drupal\media_entity_document\Plugin\EntityBrowser\Widget;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\entity_browser\Plugin\EntityBrowser\Widget\Upload as FileUpload;
7 use Drupal\Core\Url;
8
9 /**
10  * Uses upload to create media entity documents.
11  *
12  * @EntityBrowserWidget(
13  *   id = "media_entity_document_upload",
14  *   label = @Translation("Upload documents"),
15  *   description = @Translation("Upload widget that creates media entity documents.")
16  * )
17  */
18 class Upload extends FileUpload {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function defaultConfiguration() {
24     return [
25       'extensions' => 'txt, pdf',
26       'media bundle' => NULL,
27     ] + parent::defaultConfiguration();
28   }
29
30   /**
31    * {@inheritdoc}
32    */
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.')];
37     }
38
39     if ($bundle->getType()->getPluginId() != 'document') {
40       return ['#markup' => $this->t('The configured bundle is not using document plugin.')];
41     }
42
43     $form = parent::getForm($original_form, $form_state, $aditional_widget_parameters);
44     $form['upload']['upload_validators']['file_validate_extensions'] = [$this->configuration['extensions']];
45
46     return $form;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function submit(array &$element, array &$form, FormStateInterface $form_state) {
53     $documents = [];
54
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);
60
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,
66       ]);
67
68       $filename = $file->filename->value;
69       if ($filename) {
70         $document->set('name', $filename);
71       }
72
73       $document->save();
74       $documents[] = $document;
75     }
76
77     $this->selectEntities($documents, $form_state);
78     $this->clearFormValues($element, $form_state);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
85     $bundle_options = [];
86     $form = parent::buildConfigurationForm($form, $form_state);
87
88     $form['extensions'] = [
89       '#type' => 'textfield',
90       '#title' => $this->t('Allowed extensions'),
91       '#default_value' => $this->configuration['extensions'],
92       '#required' => TRUE,
93     ];
94
95     $bundles = $this->entityManager
96       ->getStorage('media_bundle')
97       ->loadByProperties(['type' => 'document']);
98
99     foreach ($bundles as $bundle) {
100       $bundle_options[$bundle->id()] = $bundle->label();
101     }
102
103     switch (count($bundle_options)) {
104       case 0:
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]),
108         ];
109         break;
110
111       case 1:
112         $form['media bundle'] = [
113           '#value' => key($bundle_options),
114           '#type' => 'value',
115         ];
116         break;
117
118       default:
119         $form['media bundle'] = [
120           '#type' => 'select',
121           '#title' => $this->t('Media bundle'),
122           '#default_value' => $this->configuration['media bundle'],
123           '#options' => $bundle_options,
124         ];
125     }
126
127     return $form;
128   }
129
130 }