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

Issue #2671914: Implement EB configuration UI #25

Open
wants to merge 2 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the other options are mentioned in this method, and in my testing, any changes I made to them were not saved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be possible to rely on default submit implementation. See \Drupal\entity_browser\PluginConfigurationFormTrait

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Drupal\Core\Entity\EntityStorageInterface;
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;
Expand Down Expand Up @@ -117,9 +118,19 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
}

$bundles = $this->entityManager->getStorage('media_bundle')->loadMultiple();
foreach ($bundles as $bundle) {
$form['media_entity_bundle']['#options'][$bundle->id()] = $bundle->label();

if (!empty($bundles)) {
foreach ($bundles as $bundle) {
$form['media_entity_bundle']['#options'][$bundle->id()] = $bundle->label();
}
}
else {
$form['media_entity_bundle']['#disabled'] = TRUE;
$form['media_entity_bundle']['#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()
]);
}

return $form;
}

Expand Down Expand Up @@ -184,5 +195,4 @@ public function submit(array &$element, array &$form, FormStateInterface $form_s
$this->clearFormValues($element, $form_state);
}
}

}