From b5c974f15837d941f89cf9b5e2fe69861464e969 Mon Sep 17 00:00:00 2001 From: Primsi Date: Fri, 19 Feb 2016 16:23:18 +0100 Subject: [PATCH] Issue #2671914 by Primsi: Implement EB configuration UI --- .../Widget/DropzoneJsEbWidget.php | 68 +++++++++++++++++++ .../Widget/MediaEntityDropzoneJsEbWidget.php | 38 +++++++++++ 2 files changed, 106 insertions(+) diff --git a/modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php index 76a2947..a43cefe 100644 --- a/modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php +++ b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php @@ -211,4 +211,72 @@ protected function validateExtension($filename, $extensions) { return TRUE; } + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $configuration = $this->configuration; + + $form['upload_location'] = [ + '#type' => 'textfield', + '#title' => $this->t('Upload location'), + '#default_value' => $configuration['upload_location'], + ]; + + $form['dropzone_description'] = [ + '#type' => 'textfield', + '#title' => $this->t('Dropzone drag-n-drop zone text'), + '#default_value' => $configuration['dropzone_description'], + ]; + + preg_match('%\d+%', $configuration['max_filesize'], $matches); + $max_filesize = !empty($matches) ? array_shift($matches) : '1'; + + $form['max_filesize'] = [ + '#type' => 'number', + '#title' => $this->t('Maximum size of files'), + '#min' => '0', + '#field_suffix' => $this->t('MB'), + '#default_value' => $max_filesize, + ]; + + $form['extensions'] = [ + '#type' => 'textfield', + '#title' => $this->t('Allowed file extensions'), + '#desciption' => $this->t('A space separated list of file extensions'), + '#default_value' => $configuration['extensions'], + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + $values = $form_state->getValues()['table'][$this->uuid()]['form']; + + if (!empty($values['extensions'])) { + $extensions = explode(' ', $values['extensions']); + $fail = FALSE; + + foreach ($extensions as $extension) { + if (preg_match('%^\w*$%', $extension) !== 1) { + $fail = TRUE; + } + } + + if ($fail) { + $form_state->setErrorByName('extensions', $this->t('Invalid extension list format.')); + } + } + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + parent::submitConfigurationForm($form, $form_state); + $this->configuration['max_filesize'] = $this->configuration['max_filesize'] . 'M'; + } } diff --git a/modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php index 3d893c9..ce60691 100644 --- a/modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php +++ b/modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php @@ -11,6 +11,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Link; use Drupal\Core\Session\AccountProxyInterface; use Drupal\dropzonejs\DropzoneJsUploadSaveInterface; use Drupal\dropzonejs\Events\DropzoneMediaEntityCreateEvent; @@ -137,4 +138,41 @@ public function submit(array &$element, array &$form, FormStateInterface $form_s $this->clearFormValues($element, $form_state); } } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $configuration = $this->getConfiguration(); + $settings = $configuration['settings']; + $form += parent::buildConfigurationForm($form, $form_state); + + $options = []; + $media_bundles = $this->entityManager->getBundleInfo('media'); + if (!empty($media_bundles)) { + foreach ($media_bundles as $id => $bundle) { + $options[$id] = $bundle['label']; + } + $disabled = FALSE; + $description = $this->t('Select the type of media entity that you want to create from the uploaded files.'); + } + else { + $disabled = TRUE; + $description = $this->t('You must @create_bundle before using this widget.', [ + '@create_bundle' => Link::createFromRoute($this->t('create a media bundle'), 'media.bundle_add')->toString() + ]); + } + + $form['media_entity_bundle'] = [ + '#type' => 'select', + '#title' => $this->t('Media bundle to create'), + '#default_value' => $settings['media_entity_bundle'], + '#description' => $description, + '#options' => $options, + '#disabled' => $disabled, + '#required' => TRUE, + ]; + + return $form; + } }