class UploadFetcherFeedForm in Feeds 8.3
Provides a form on the feed edit page for the UploadFetcher.
Hierarchy
- class \Drupal\feeds\Plugin\Type\ExternalPluginFormBase implements PluginFormInterface, PluginAwareInterface uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\feeds\Feeds\Fetcher\Form\UploadFetcherFeedForm implements ContainerInjectionInterface
Expanded class hierarchy of UploadFetcherFeedForm
1 file declares its use of UploadFetcherFeedForm
- UploadFetcherFeedFormTest.php in tests/
src/ Unit/ Feeds/ Fetcher/ Form/ UploadFetcherFeedFormTest.php
File
- src/
Feeds/ Fetcher/ Form/ UploadFetcherFeedForm.php, line 17
Namespace
Drupal\feeds\Feeds\Fetcher\FormView source
class UploadFetcherFeedForm extends ExternalPluginFormBase implements ContainerInjectionInterface {
/**
* The file storage backend.
*
* @var \Drupal\file\FileStorageInterface
*/
protected $fileStorage;
/**
* The file usage backend.
*
* @var \Drupal\file\FileUsage\FileUsageInterface
*/
protected $fileUsage;
/**
* The UUID generator.
*
* @var \Drupal\Component\Uuid\UuidInterface
*/
protected $uuid;
/**
* Constructs an HttpFeedForm object.
*
* @param \Drupal\file\FileStorageInterface $file_storage
* The file storage backend.
* @param \Drupal\file\FileUsage\FileUsageInterface $file_usage
* The file usage backend.
* @param \Drupal\Component\Uuid\UuidInterface $uuid
* The UUID generator.
*/
public function __construct(FileStorageInterface $file_storage, FileUsageInterface $file_usage, UuidInterface $uuid) {
$this->fileStorage = $file_storage;
$this->fileUsage = $file_usage;
$this->uuid = $uuid;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('file'), $container
->get('file.usage'), $container
->get('uuid'));
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FeedInterface $feed = NULL) {
$form['source'] = [
'#type' => 'managed_file',
'#title' => $this
->t('File'),
'#description' => $this
->t('Select a file from your local system.'),
'#default_value' => [
$feed
->getConfigurationFor($this->plugin)['fid'],
],
'#upload_validators' => [
'file_validate_extensions' => [
$this->plugin
->getConfiguration('allowed_extensions'),
],
],
'#upload_location' => $this->plugin
->getConfiguration('directory'),
'#required' => TRUE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state, FeedInterface $feed = NULL) {
// We need to store this for later so that we have the feed id.
$new_fid = reset($form_state
->getValue('source'));
$feed_config = $feed
->getConfigurationFor($this->plugin);
// Generate a UUID that maps to this feed for file usage. We can't depend
// on the feed id since this could be called before an id is assigned.
$feed_config['usage_id'] = $feed_config['usage_id'] ?: $this->uuid
->generate();
if ($new_fid != $feed_config['fid']) {
$this
->deleteFile($feed_config['fid'], $feed_config['usage_id']);
if ($new_fid) {
$file = $this->fileStorage
->load($new_fid);
$this->fileUsage
->add($file, 'feeds', $this->plugin
->pluginType(), $feed_config['usage_id']);
$file
->setPermanent();
$file
->save();
$feed_config['fid'] = $new_fid;
$feed
->setSource($file
->getFileUri());
}
}
$feed
->setConfigurationFor($this->plugin, $feed_config);
}
/**
* 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->plugin
->pluginType(), $uuid);
}
}
}
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 | |
ExternalPluginFormBase:: |
protected | property | The Feeds plugin. | |
ExternalPluginFormBase:: |
public | function |
Sets the plugin for this object. Overrides PluginAwareInterface:: |
|
ExternalPluginFormBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
5 |
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. | |
UploadFetcherFeedForm:: |
protected | property | The file storage backend. | |
UploadFetcherFeedForm:: |
protected | property | The file usage backend. | |
UploadFetcherFeedForm:: |
protected | property | The UUID generator. | |
UploadFetcherFeedForm:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
UploadFetcherFeedForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
UploadFetcherFeedForm:: |
protected | function | Deletes a file. | |
UploadFetcherFeedForm:: |
public | function |
Form submission handler. Overrides ExternalPluginFormBase:: |
|
UploadFetcherFeedForm:: |
public | function | Constructs an HttpFeedForm object. |