UploadFetcherForm.php in Feeds 8.3
File
src/Feeds/Fetcher/Form/UploadFetcherForm.php
View source
<?php
namespace Drupal\feeds\Feeds\Fetcher\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\feeds\Plugin\Type\ExternalPluginFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UploadFetcherForm extends ExternalPluginFormBase implements ContainerInjectionInterface {
protected $streamWrapperManager;
protected $fileSystem;
public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, FileSystemInterface $file_system) {
$this->streamWrapperManager = $stream_wrapper_manager;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('stream_wrapper_manager'), $container
->get('file_system'));
}
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['directory'] = [
'#type' => 'feeds_uri',
'#title' => $this
->t('Upload directory'),
'#description' => $this
->t('Directory where uploaded files get stored. Prefix the path with a scheme. Available schemes: @schemes.', [
'@schemes' => implode(', ', $this
->getSchemes()),
]),
'#default_value' => $this->plugin
->getConfiguration('directory'),
'#required' => TRUE,
'#allowed_schemes' => $this
->getSchemes(),
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$values =& $form_state
->getValues();
$values['allowed_extensions'] = preg_replace('/\\s+/', ' ', trim($values['allowed_extensions']));
if (!empty($form['directory']) && !$this->fileSystem
->prepareDirectory($values['directory'], FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
$form_state
->setError($form['directory'], $this
->t('The chosen directory does not exist and attempts to create it failed.'));
}
}
protected function getSchemeOptions() {
$options = [];
foreach ($this->streamWrapperManager
->getDescriptions(StreamWrapperInterface::WRITE_VISIBLE) as $scheme => $description) {
$options[$scheme] = Html::escape($scheme . ': ' . $description);
}
return $options;
}
protected function getSchemes() {
return array_keys($this->streamWrapperManager
->getWrappers(StreamWrapperInterface::WRITE_VISIBLE));
}
}