You are here

class UploadFetcherForm in Feeds 8.3

The configuration form for the upload fetcher.

Hierarchy

Expanded class hierarchy of UploadFetcherForm

1 file declares its use of UploadFetcherForm
UploadFetcherFormTest.php in tests/src/Unit/Feeds/Fetcher/Form/UploadFetcherFormTest.php

File

src/Feeds/Fetcher/Form/UploadFetcherForm.php, line 17

Namespace

Drupal\feeds\Feeds\Fetcher\Form
View source
class UploadFetcherForm extends ExternalPluginFormBase implements ContainerInjectionInterface {

  /**
   * The stream wrapper manager.
   *
   * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
   */
  protected $streamWrapperManager;

  /**
   * The file and stream wrapper helper.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Constructs a DirectoryFetcherForm object.
   *
   * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
   *   The stream wrapper manager.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file and stream wrapper helper.
   */
  public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, FileSystemInterface $file_system) {
    $this->streamWrapperManager = $stream_wrapper_manager;
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('stream_wrapper_manager'), $container
      ->get('file_system'));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values =& $form_state
      ->getValues();
    $values['allowed_extensions'] = preg_replace('/\\s+/', ' ', trim($values['allowed_extensions']));

    // Ensure that the upload directory exists.
    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.'));
    }
  }

  /**
   * Returns available scheme options for use in checkboxes or select list.
   *
   * @return array
   *   The available scheme array keyed scheme => description.
   */
  protected function getSchemeOptions() {
    $options = [];
    foreach ($this->streamWrapperManager
      ->getDescriptions(StreamWrapperInterface::WRITE_VISIBLE) as $scheme => $description) {
      $options[$scheme] = Html::escape($scheme . ': ' . $description);
    }
    return $options;
  }

  /**
   * Returns available schemes.
   *
   * @return string[]
   *   The available schemes.
   */
  protected function getSchemes() {
    return array_keys($this->streamWrapperManager
      ->getWrappers(StreamWrapperInterface::WRITE_VISIBLE));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
ExternalPluginFormBase::$plugin protected property The Feeds plugin.
ExternalPluginFormBase::setPlugin public function Sets the plugin for this object. Overrides PluginAwareInterface::setPlugin
ExternalPluginFormBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 4
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UploadFetcherForm::$fileSystem protected property The file and stream wrapper helper.
UploadFetcherForm::$streamWrapperManager protected property The stream wrapper manager.
UploadFetcherForm::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
UploadFetcherForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
UploadFetcherForm::getSchemeOptions protected function Returns available scheme options for use in checkboxes or select list.
UploadFetcherForm::getSchemes protected function Returns available schemes.
UploadFetcherForm::validateConfigurationForm public function Form validation handler. Overrides ExternalPluginFormBase::validateConfigurationForm
UploadFetcherForm::__construct public function Constructs a DirectoryFetcherForm object.