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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\feeds\Plugin\Type\PluginBase implements FeedsPluginInterface uses DependencyTrait
- class \Drupal\feeds\Feeds\Fetcher\UploadFetcher implements ContainerFactoryPluginInterface, FetcherInterface
- class \Drupal\feeds\Plugin\Type\PluginBase implements FeedsPluginInterface uses DependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\FetcherView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The importer this plugin is working for. | |
PluginBase:: |
protected | property | The link generator. | |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
protected | property | The url generator. | |
PluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
2 |
PluginBase:: |
private | function | Returns the service container. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
PluginBase:: |
protected | function | Returns the link generator service. | |
PluginBase:: |
public | function | A feed is being saved. | |
PluginBase:: |
public | function | The feed type is being deleted. | 1 |
PluginBase:: |
public | function | The feed type is being saved. | 1 |
PluginBase:: |
public | function |
Returns the type of plugin. Overrides FeedsPluginInterface:: |
|
PluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
1 |
PluginBase:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. | |
PluginBase:: |
protected | function | Returns the URL generator service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UploadFetcher:: |
protected | property | The file storage backend. | |
UploadFetcher:: |
protected | property | The file usage backend. | |
UploadFetcher:: |
protected | property | The stream wrapper manager. | |
UploadFetcher:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
UploadFetcher:: |
public | function |
Gets default configuration for this plugin. Overrides PluginBase:: |
|
UploadFetcher:: |
public | function |
Returns default feed configuration. Overrides PluginBase:: |
|
UploadFetcher:: |
protected | function | Deletes a file. | |
UploadFetcher:: |
public | function |
Fetch content from a feed and return it. Overrides FetcherInterface:: |
|
UploadFetcher:: |
protected | function | Returns available schemes. | |
UploadFetcher:: |
public | function |
A feed is being deleted. Overrides PluginBase:: |
|
UploadFetcher:: |
public | function |
Constructs an UploadFetcher object. Overrides PluginBase:: |