class UploadForm in Filebrowser 3.x
Same name and namespace in other branches
- 8.2 src/Form/UploadForm.php \Drupal\filebrowser\Form\UploadForm
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\filebrowser\Form\UploadForm
Expanded class hierarchy of UploadForm
File
- src/
Form/ UploadForm.php, line 11
Namespace
Drupal\filebrowser\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
105 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
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. | |
UploadForm:: |
protected | property | ||
UploadForm:: |
protected | property | ||
UploadForm:: |
protected | property | ||
UploadForm:: |
protected | property | ||
UploadForm:: |
protected | property | ||
UploadForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
UploadForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
UploadForm:: |
public | function |
@inheritdoc Overrides FormInterface:: |
|
UploadForm:: |
public | function |
@inheritdoc Overrides FormBase:: |