Skip to content
This repository was archived by the owner on Jan 5, 2018. It is now read-only.

Commit b5c974f

Browse files
committed
Issue #2671914 by Primsi: Implement EB configuration UI
1 parent a0b198a commit b5c974f

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,72 @@ protected function validateExtension($filename, $extensions) {
211211
return TRUE;
212212
}
213213

214+
/**
215+
* {@inheritdoc}
216+
*/
217+
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
218+
$configuration = $this->configuration;
219+
220+
$form['upload_location'] = [
221+
'#type' => 'textfield',
222+
'#title' => $this->t('Upload location'),
223+
'#default_value' => $configuration['upload_location'],
224+
];
225+
226+
$form['dropzone_description'] = [
227+
'#type' => 'textfield',
228+
'#title' => $this->t('Dropzone drag-n-drop zone text'),
229+
'#default_value' => $configuration['dropzone_description'],
230+
];
231+
232+
preg_match('%\d+%', $configuration['max_filesize'], $matches);
233+
$max_filesize = !empty($matches) ? array_shift($matches) : '1';
234+
235+
$form['max_filesize'] = [
236+
'#type' => 'number',
237+
'#title' => $this->t('Maximum size of files'),
238+
'#min' => '0',
239+
'#field_suffix' => $this->t('MB'),
240+
'#default_value' => $max_filesize,
241+
];
242+
243+
$form['extensions'] = [
244+
'#type' => 'textfield',
245+
'#title' => $this->t('Allowed file extensions'),
246+
'#desciption' => $this->t('A space separated list of file extensions'),
247+
'#default_value' => $configuration['extensions'],
248+
];
249+
250+
return $form;
251+
}
252+
253+
/**
254+
* {@inheritdoc}
255+
*/
256+
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
257+
$values = $form_state->getValues()['table'][$this->uuid()]['form'];
258+
259+
if (!empty($values['extensions'])) {
260+
$extensions = explode(' ', $values['extensions']);
261+
$fail = FALSE;
262+
263+
foreach ($extensions as $extension) {
264+
if (preg_match('%^\w*$%', $extension) !== 1) {
265+
$fail = TRUE;
266+
}
267+
}
268+
269+
if ($fail) {
270+
$form_state->setErrorByName('extensions', $this->t('Invalid extension list format.'));
271+
}
272+
}
273+
}
274+
275+
/**
276+
* {@inheritdoc}
277+
*/
278+
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
279+
parent::submitConfigurationForm($form, $form_state);
280+
$this->configuration['max_filesize'] = $this->configuration['max_filesize'] . 'M';
281+
}
214282
}

modules/eb_widget/src/Plugin/EntityBrowser/Widget/MediaEntityDropzoneJsEbWidget.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Drupal\Core\Entity\EntityManagerInterface;
1212
use Drupal\Core\Extension\ModuleHandlerInterface;
1313
use Drupal\Core\Form\FormStateInterface;
14+
use Drupal\Core\Link;
1415
use Drupal\Core\Session\AccountProxyInterface;
1516
use Drupal\dropzonejs\DropzoneJsUploadSaveInterface;
1617
use Drupal\dropzonejs\Events\DropzoneMediaEntityCreateEvent;
@@ -137,4 +138,41 @@ public function submit(array &$element, array &$form, FormStateInterface $form_s
137138
$this->clearFormValues($element, $form_state);
138139
}
139140
}
141+
142+
/**
143+
* {@inheritdoc}
144+
*/
145+
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
146+
$configuration = $this->getConfiguration();
147+
$settings = $configuration['settings'];
148+
$form += parent::buildConfigurationForm($form, $form_state);
149+
150+
$options = [];
151+
$media_bundles = $this->entityManager->getBundleInfo('media');
152+
if (!empty($media_bundles)) {
153+
foreach ($media_bundles as $id => $bundle) {
154+
$options[$id] = $bundle['label'];
155+
}
156+
$disabled = FALSE;
157+
$description = $this->t('Select the type of media entity that you want to create from the uploaded files.');
158+
}
159+
else {
160+
$disabled = TRUE;
161+
$description = $this->t('You must @create_bundle before using this widget.', [
162+
'@create_bundle' => Link::createFromRoute($this->t('create a media bundle'), 'media.bundle_add')->toString()
163+
]);
164+
}
165+
166+
$form['media_entity_bundle'] = [
167+
'#type' => 'select',
168+
'#title' => $this->t('Media bundle to create'),
169+
'#default_value' => $settings['media_entity_bundle'],
170+
'#description' => $description,
171+
'#options' => $options,
172+
'#disabled' => $disabled,
173+
'#required' => TRUE,
174+
];
175+
176+
return $form;
177+
}
140178
}

0 commit comments

Comments
 (0)