You are here

class FolderForm in Filebrowser 3.x

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

Hierarchy

Expanded class hierarchy of FolderForm

File

src/Form/FolderForm.php, line 11

Namespace

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

  /**
   * @var int
   * If we create a folder in a sub folder this is the fid
   * of the subfolder. If we want to redirect to the node page we can use
   * the url query /node/{nid}?fid=$relativeFid
   */
  protected $relativeFid;

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

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

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

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

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

    // If this form is to be presented in a slide-down window we
    // will set the attributes and at a close-window link
    if ($ajax) {
      $form['#attributes'] = [
        'class' => [
          'form-in-slide-down',
        ],
      ];
      $form['close-window'] = $this->common
        ->closeButtonMarkup();
    }
    $form['#tree'] = true;
    $form['folder_name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Folder Name'),
      '#size' => 40,
      '#description' => $this
        ->t('This folder will be created within the current directory.'),
      '#required' => true,
    ];
    $form['create'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Create'),
      '#name' => 'create',
    ];
    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $form['#attached']['library'][] = 'filebrowser/filebrowser-styles';
    return $form;
  }

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $folder_uri = $this->node->filebrowser->folderPath . $this->relativeRoot . '/' . $form_state
      ->getValue('folder_name');
    $success = \Drupal::service('file_system')
      ->prepareDirectory($folder_uri, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
    if (!$success) {
      \Drupal::messenger()
        ->addError($this
        ->t('Unable to create this folder, do you have filesystem right to do that ?'));
    }
    else {
      Cache::invalidateTags([
        'filebrowser:node:' . $this->node
          ->id(),
      ]);
    }
    $route = $this->common
      ->redirectRoute($this->relativeFid, $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
FolderForm::$common protected property
FolderForm::$node protected property
FolderForm::$relativeFid protected property If we create a folder in a sub folder this is the fid of the subfolder. If we want to redirect to the node page we can use the url query /node/{nid}?fid=$relativeFid
FolderForm::$relativeRoot protected property
FolderForm::buildForm public function Overrides FormInterface::buildForm
FolderForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
FolderForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
FolderForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
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.