You are here

class UploadFetcherFeedForm in Feeds 8.3

Provides a form on the feed edit page for the UploadFetcher.

Hierarchy

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\Form
View 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

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::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 5
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.
UploadFetcherFeedForm::$fileStorage protected property The file storage backend.
UploadFetcherFeedForm::$fileUsage protected property The file usage backend.
UploadFetcherFeedForm::$uuid protected property The UUID generator.
UploadFetcherFeedForm::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
UploadFetcherFeedForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
UploadFetcherFeedForm::deleteFile protected function Deletes a file.
UploadFetcherFeedForm::submitConfigurationForm public function Form submission handler. Overrides ExternalPluginFormBase::submitConfigurationForm
UploadFetcherFeedForm::__construct public function Constructs an HttpFeedForm object.