70d572774678cdf0835864abc933d7ed428856b9
[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\Core\Url;
7 use Drupal\entity_browser\Plugin\EntityBrowser\Widget\Upload as FileUpload;
8 use Drupal\media_entity\MediaInterface;
9
10 /**
11  * Uses upload to create media entity documents.
12  *
13  * @EntityBrowserWidget(
14  *   id = "media_entity_document_upload",
15  *   label = @Translation("Upload documents"),
16  *   description = @Translation("Upload widget that creates media entity documents.")
17  * )
18  */
19 class Upload extends FileUpload {
20
21   /**
22    * {@inheritdoc}
23    */
24   public function defaultConfiguration() {
25     return [
26       'extensions' => 'txt, pdf',
27       'media_bundle' => NULL,
28     ] + parent::defaultConfiguration();
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) {
35     /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
36     if (!$this->configuration['media_bundle'] || !($bundle = $this->entityTypeManager->getStorage('media_bundle')->load($this->configuration['media_bundle']))) {
37       return ['#markup' => $this->t('The media bundle is not configured correctly.')];
38     }
39
40     if ($bundle->getType()->getPluginId() != 'document') {
41       return ['#markup' => $this->t('The configured bundle is not using document plugin.')];
42     }
43
44     $form = parent::getForm($original_form, $form_state, $aditional_widget_parameters);
45     $form['upload']['#upload_validators']['file_validate_extensions'] = [$this->configuration['extensions']];
46
47     return $form;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function prepareEntities(array $form, FormStateInterface $form_state) {
54     $files = parent::prepareEntities($form, $form_state);
55
56     /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
57     $bundle = $this->entityTypeManager
58       ->getStorage('media_bundle')
59       ->load($this->configuration['media_bundle']);
60
61     $documents = [];
62     foreach ($files as $file) {
63       /** @var \Drupal\media_entity\MediaInterface $document */
64       $document = $this->entityTypeManager->getStorage('media')->create([
65         'bundle' => $bundle->id(),
66         $bundle->getTypeConfiguration()['source_field'] => $file,
67       ]);
68
69       $documents[] = $document;
70     }
71
72     return $documents;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function submit(array &$element, array &$form, FormStateInterface $form_state) {
79     if (!empty($form_state->getTriggeringElement()['#eb_widget_main_submit'])) {
80       $documents = $this->prepareEntities($form, $form_state);
81       array_walk(
82         $documents,
83         function (MediaInterface $media) { $media->save(); }
84       );
85
86       $this->selectEntities($documents, $form_state);
87       $this->clearFormValues($element, $form_state);
88     }
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
95     $bundle_options = [];
96     $form = parent::buildConfigurationForm($form, $form_state);
97
98     $form['extensions'] = [
99       '#type' => 'textfield',
100       '#title' => $this->t('Allowed extensions'),
101       '#default_value' => $this->configuration['extensions'],
102       '#required' => TRUE,
103     ];
104
105     $bundles = $this->entityTypeManager
106       ->getStorage('media_bundle')
107       ->loadByProperties(['type' => 'document']);
108
109     /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
110     foreach ($bundles as $bundle) {
111       $bundle_options[$bundle->id()] = $bundle->label();
112     }
113
114     if (empty($bundle_options)) {
115       $url = Url::fromRoute('media.bundle_add')->toString();
116       $form['media_bundle'] = [
117         '#markup' => $this->t("You don't have media bundle of the Document type. You should <a href='!link'>create one</a>", ['!link' => $url]),
118       ];
119     }
120     else {
121       $form['media_bundle'] = [
122         '#type' => 'select',
123         '#title' => $this->t('Media bundle'),
124         '#default_value' => $this->configuration['media_bundle'],
125         '#options' => $bundle_options,
126       ];
127     }
128
129     return $form;
130   }
131
132 }