FolderForm.php in Filebrowser 3.x
File
src/Form/FolderForm.php
View source
<?php
namespace Drupal\filebrowser\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
class FolderForm extends FormBase {
protected $relativeFid;
protected $node;
protected $relativeRoot;
protected $common;
public function getFormId() {
return 'create_folder_form';
}
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 ($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;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
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']);
}
}