You are here

class UploadForm in Filebrowser 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/UploadForm.php \Drupal\filebrowser\Form\UploadForm

Hierarchy

Expanded class hierarchy of UploadForm

File

src/Form/UploadForm.php, line 11

Namespace

Drupal\filebrowser\Form
View source
class UploadForm extends FormBase {

  /**
   * @var int
   */
  protected $queryFid;

  /**
   * @var string
   */
  protected $relativeRoot;

  /**
   * @var NodeInterface
   */
  protected $node;

  /**
   * @var integer
   */
  protected $nid;

  /**
   * @var \Drupal\filebrowser\Services\Common
   */
  protected $common;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'upload_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $nid = null, $query_fid = null, $fids = null, $ajax = null) {
    $this->common = \Drupal::service('filebrowser.common');
    $this->relativeRoot = $this->common
      ->relativePath($query_fid);
    $this->node = Node::load($nid);
    $this->queryFid = $query_fid;
    $this->nid = $nid;
    $accepted = $this->node->filebrowser->accepted;

    // if this form is opened by ajax add a close link.
    if ($ajax) {
      $form['#attributes'] = [
        'class' => [
          'form-in-slide-down',
        ],
      ];
      $form['close'] = $this->common
        ->closeButtonMarkup();
    }

    // Set upload location, Replace "//" with "/" when needed.
    if ($this->node->filebrowser->folderPath && $this->relativeRoot) {
      $upload_location = preg_replace('/\\/\\/$/', '/', $this->node->filebrowser->folderPath) . $this->relativeRoot;
    }
    else {
      $upload_location = $this->node->filebrowser->folderPath . $this->relativeRoot;
    }
    $form['u_file'] = [
      '#title' => $this
        ->t('Upload file'),
      '#type' => 'filebrowser_managed_file',
      '#description' => $this
        ->t('File types accepted: @accepted', [
        '@accepted' => $accepted,
      ]) . '<br>' . $this
        ->t('You can upload multiple files.'),
      '#upload_validators' => [
        'file_validate_extensions' => [
          $this->node->filebrowser->accepted,
        ],
      ],
      '#upload_location' => $upload_location,
      '#progress_indicator' => 'bar',
      '#progress_message' => $this
        ->t('Please wait...'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save Upload'),
    ];
    return $form;
  }

  /**
   * @inheritdoc
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // all required validation is done in filebrowser_managed_file form element
  }

  /**
   * @inheritdoc
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // While we are using the managed_file widget (for convenience), we don't
    // want to save the file in the file_managed table, so we will delete it
    // here.
    // A bit hackish, but it works.
    $file_ids = $form_state
      ->getValue('u_file');
    if (count($file_ids)) {
      $success = \Drupal::service('filebrowser.storage')
        ->genericDeleteMultiple('file_managed', 'fid', join(',', $file_ids));
      if ($success) {
        \Drupal::messenger()
          ->addMessage($this
          ->t("Your filebrowser upload is completed successfully!"));
      }
      else {
        \Drupal::messenger()
          ->addError($this
          ->t('Your upload completed successfully, but file_managed clean-up failed'));
      }
    }

    // invalidate the cache for this node
    Cache::invalidateTags([
      'filebrowser:node:' . $this->nid,
    ]);
    $route = $this->common
      ->redirectRoute($this->queryFid, $this->node
      ->id());
    $form_state
      ->setRedirect($route['name'], $route['node'], $route['query']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 105
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.
UploadForm::$common protected property
UploadForm::$nid protected property
UploadForm::$node protected property
UploadForm::$queryFid protected property
UploadForm::$relativeRoot protected property
UploadForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
UploadForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
UploadForm::submitForm public function @inheritdoc Overrides FormInterface::submitForm
UploadForm::validateForm public function @inheritdoc Overrides FormBase::validateForm