You are here

class UploadFetcher in Feeds 8.3

Defines a file upload fetcher.

Plugin annotation


@FeedsFetcher(
  id = "upload",
  title = @Translation("Upload file"),
  description = @Translation("Upload content from a local file."),
  form = {
    "configuration" = "Drupal\feeds\Feeds\Fetcher\Form\UploadFetcherForm",
    "feed" = "Drupal\feeds\Feeds\Fetcher\Form\UploadFetcherFeedForm",
  },
)

Hierarchy

Expanded class hierarchy of UploadFetcher

1 file declares its use of UploadFetcher
UploadFetcherTest.php in tests/src/Unit/Feeds/Fetcher/UploadFetcherTest.php

File

src/Feeds/Fetcher/UploadFetcher.php, line 31

Namespace

Drupal\feeds\Feeds\Fetcher
View source
class UploadFetcher extends PluginBase implements FetcherInterface, ContainerFactoryPluginInterface {

  /**
   * The file usage backend.
   *
   * @var \Drupal\file\FileUsage\FileUsageInterface
   */
  protected $fileUsage;

  /**
   * The file storage backend.
   *
   * @var \Drupal\file\FileStorageInterface
   */
  protected $fileStorage;

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

  /**
   * Constructs an UploadFetcher object.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin id.
   * @param array $plugin_definition
   *   The plugin definition.
   * @param \Drupal\file\FileUsage\FileUsageInterface $file_usage
   *   The file usage backend.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
   *   The stream wrapper manager.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, FileUsageInterface $file_usage, EntityTypeManagerInterface $entity_type_manager, StreamWrapperManagerInterface $stream_wrapper_manager) {
    $this->fileUsage = $file_usage;
    $this->fileStorage = $entity_type_manager
      ->getStorage('file');
    $this->streamWrapperManager = $stream_wrapper_manager;
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('file.usage'), $container
      ->get('entity_type.manager'), $container
      ->get('stream_wrapper_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function fetch(FeedInterface $feed, StateInterface $state) {
    $file = $feed
      ->getSource();
    if (is_file($file) && is_readable($file)) {
      return new FetcherResult($file);
    }

    // File does not exist.
    throw new \RuntimeException(new FormattableMarkup('Resource is not a file: %source', [
      '%source' => $file,
    ]));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultFeedConfiguration() {
    return [
      'fid' => 0,
      'usage_id' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function onFeedDeleteMultiple(array $feeds) {
    foreach ($feeds as $feed) {
      $feed_config = $feed
        ->getConfigurationFor($this);
      if ($feed_config['fid']) {
        $this
          ->deleteFile($feed_config['fid'], $feed_config['usage_id']);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $schemes = $this
      ->getSchemes();
    $scheme = in_array('private', $schemes) ? 'private' : reset($schemes);
    return [
      'allowed_extensions' => 'txt csv tsv xml opml',
      'directory' => $scheme . '://feeds',
    ];
  }

  /**
   * Deletes a file.
   *
   * @param int $file_id
   *   The file id.
   * @param string $uuid
   *   The file UUID associated with this file.
   *
   * @see file_delete()
   */
  protected function deleteFile($file_id, $uuid) {
    if ($file = $this->fileStorage
      ->load($file_id)) {
      $this->fileUsage
        ->delete($file, 'feeds', $this
        ->pluginType(), $uuid);
    }
  }

  /**
   * 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
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$feedType protected property The importer this plugin is working for.
PluginBase::$linkGenerator protected property The link generator.
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::$urlGenerator protected property The url generator.
PluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 2
PluginBase::container private function Returns the service container.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::l protected function Renders a link to a route given a route name and its parameters.
PluginBase::linkGenerator protected function Returns the link generator service.
PluginBase::onFeedSave public function A feed is being saved.
PluginBase::onFeedTypeDelete public function The feed type is being deleted. 1
PluginBase::onFeedTypeSave public function The feed type is being saved. 1
PluginBase::pluginType public function Returns the type of plugin. Overrides FeedsPluginInterface::pluginType
PluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration 1
PluginBase::url protected function Generates a URL or path for a specific route based on the given parameters.
PluginBase::urlGenerator protected function Returns the URL generator service.
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.
UploadFetcher::$fileStorage protected property The file storage backend.
UploadFetcher::$fileUsage protected property The file usage backend.
UploadFetcher::$streamWrapperManager protected property The stream wrapper manager.
UploadFetcher::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
UploadFetcher::defaultConfiguration public function Gets default configuration for this plugin. Overrides PluginBase::defaultConfiguration
UploadFetcher::defaultFeedConfiguration public function Returns default feed configuration. Overrides PluginBase::defaultFeedConfiguration
UploadFetcher::deleteFile protected function Deletes a file.
UploadFetcher::fetch public function Fetch content from a feed and return it. Overrides FetcherInterface::fetch
UploadFetcher::getSchemes protected function Returns available schemes.
UploadFetcher::onFeedDeleteMultiple public function A feed is being deleted. Overrides PluginBase::onFeedDeleteMultiple
UploadFetcher::__construct public function Constructs an UploadFetcher object. Overrides PluginBase::__construct