3 namespace Drupal\media_entity_document\Plugin\EntityBrowser\Widget;
5 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\entity_browser\Plugin\EntityBrowser\Widget\Upload as FileUpload;
8 use Drupal\media_entity\MediaInterface;
11 * Uses upload to create media entity documents.
13 * @EntityBrowserWidget(
14 * id = "media_entity_document_upload",
15 * label = @Translation("Upload documents"),
16 * description = @Translation("Upload widget that creates media entity documents.")
19 class Upload extends FileUpload {
24 public function defaultConfiguration() {
26 'extensions' => 'txt, pdf',
27 'media_bundle' => NULL,
28 ] + parent::defaultConfiguration();
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.')];
40 if ($bundle->getType()->getPluginId() != 'document') {
41 return ['#markup' => $this->t('The configured bundle is not using document plugin.')];
44 $form = parent::getForm($original_form, $form_state, $aditional_widget_parameters);
45 $form['upload']['#upload_validators']['file_validate_extensions'] = [$this->configuration['extensions']];
53 protected function prepareEntities(array $form, FormStateInterface $form_state) {
54 $files = parent::prepareEntities($form, $form_state);
56 /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
57 $bundle = $this->entityTypeManager
58 ->getStorage('media_bundle')
59 ->load($this->configuration['media_bundle']);
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,
69 $documents[] = $document;
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);
83 function (MediaInterface $media) { $media->save(); }
86 $this->selectEntities($documents, $form_state);
87 $this->clearFormValues($element, $form_state);
94 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
96 $form = parent::buildConfigurationForm($form, $form_state);
98 $form['extensions'] = [
99 '#type' => 'textfield',
100 '#title' => $this->t('Allowed extensions'),
101 '#default_value' => $this->configuration['extensions'],
105 $bundles = $this->entityTypeManager
106 ->getStorage('media_bundle')
107 ->loadByProperties(['type' => 'document']);
109 /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
110 foreach ($bundles as $bundle) {
111 $bundle_options[$bundle->id()] = $bundle->label();
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]),
121 $form['media_bundle'] = [
123 '#title' => $this->t('Media bundle'),
124 '#default_value' => $this->configuration['media_bundle'],
125 '#options' => $bundle_options,