DirectoryFetcherForm.php in Feeds 8.3
File
src/Feeds/Fetcher/Form/DirectoryFetcherForm.php
View source
<?php
namespace Drupal\feeds\Feeds\Fetcher\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\feeds\Plugin\Type\ExternalPluginFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DirectoryFetcherForm extends ExternalPluginFormBase implements ContainerInjectionInterface {
protected $streamWrapperManager;
public function __construct(StreamWrapperManager $stream_wrapper_manager) {
$this->streamWrapperManager = $stream_wrapper_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('stream_wrapper_manager'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['allowed_extensions'] = [
'#type' => 'textfield',
'#title' => $this
->t('Allowed file extensions'),
'#description' => $this
->t('Allowed file extensions for upload.'),
'#default_value' => $this->plugin
->getConfiguration('allowed_extensions'),
];
$form['allowed_schemes'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Allowed schemes'),
'#default_value' => $this->plugin
->getConfiguration('allowed_schemes'),
'#options' => $this
->getSchemeOptions(),
'#description' => $this
->t('Select the schemes you want to allow for direct upload.'),
];
$form['recursive_scan'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Search recursively'),
'#default_value' => $this->plugin
->getConfiguration('recursive_scan'),
'#description' => $this
->t('Search through sub-directories.'),
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$form_state
->setValue('allowed_schemes', array_filter($form_state
->getValue('allowed_schemes', [])));
$extensions = preg_replace('/\\s+/', ' ', trim($form_state
->getValue('allowed_extensions', '')));
$form_state
->setValue('allowed_extensions', $extensions);
}
protected function getSchemeOptions() {
$options = [];
foreach ($this->streamWrapperManager
->getDescriptions(StreamWrapperInterface::WRITE_VISIBLE) as $scheme => $description) {
$options[$scheme] = Html::escape($scheme . ': ' . $description);
}
return $options;
}
}