class Delete in IMCE 8
Same name and namespace in other branches
- 8.2 src/Plugin/ImcePlugin/Delete.php \Drupal\imce\Plugin\ImcePlugin\Delete
Defines Imce Delete plugin.
Plugin annotation
@ImcePlugin(
id = "delete",
label = "Delete",
weight = -5,
operations = {
"delete" = "opDelete"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\imce\ImcePluginBase implements ImcePluginInterface
- class \Drupal\imce\Plugin\ImcePlugin\Delete
- class \Drupal\imce\ImcePluginBase implements ImcePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Delete
1 file declares its use of Delete
- DeleteTest.php in tests/
src/ Kernel/ Plugin/ ImcePlugin/ DeleteTest.php
2 string references to 'Delete'
- ImceProfileDeleteForm::getConfirmText in src/
Form/ ImceProfileDeleteForm.php - Returns a caption for the button that confirms the action.
- ImceProfileDeleteFormTest::testConfirmText in tests/
src/ Kernel/ Form/ ImceProfileDeleteFormTest.php - Test the method getConfirmText().
File
- src/
Plugin/ ImcePlugin/ Delete.php, line 21
Namespace
Drupal\imce\Plugin\ImcePluginView source
class Delete extends ImcePluginBase {
/**
* {@inheritdoc}
*/
public function permissionInfo() {
return [
'delete_files' => $this
->t('Delete files'),
'delete_subfolders' => $this
->t('Delete subfolders'),
];
}
/**
* {@inheritdoc}
*/
public function buildPage(array &$page, ImceFM $fm) {
// Check if delete permission exists.
if ($fm
->hasPermission('delete_files') || $fm
->hasPermission('delete_subfolders')) {
$page['#attached']['library'][] = 'imce/drupal.imce.delete';
}
}
/**
* Operation handler: delete.
*/
public function opDelete(ImceFM $fm) {
$items = $fm
->getSelection();
if ($this
->validateDelete($fm, $items)) {
$this
->deleteItems($fm, $items);
}
}
/**
* Validates the deletion of the given items.
*/
public function validateDelete(ImceFM $fm, array $items) {
return $items && $fm
->validatePermissions($items, 'delete_files', 'delete_subfolders') && $fm
->validatePredefinedPath($items);
}
/**
* Deletes a list of imce items and returns succeeded ones.
*/
public function deleteItems(ImceFM $fm, array $items) {
$success = [];
$ignore_usage = $fm
->getConf('ignore_usage', FALSE);
foreach ($items as $item) {
if ($uri = $item
->getUri()) {
$result = $item->type === 'folder' ? $this
->deleteFolderUri($uri, $ignore_usage, !$item
->getPermission('delete_files')) : $this
->deleteFileUri($uri, $ignore_usage);
if ($result) {
$item
->removeFromJs();
$item
->remove();
$success[] = $item;
}
}
}
return $success;
}
/**
* Deletes a file by uri.
*/
public static function deleteFileUri($uri, $ignore_usage = FALSE) {
// Managed file.
if ($file = Imce::getFileEntity($uri)) {
if (!$ignore_usage && ($usage = \Drupal::service('file.usage')
->listUsage($file))) {
unset($usage['imce']);
if ($usage) {
\Drupal::messenger()
->addMessage(t('%filename is in use by another application.', [
'%filename' => $file
->getFilename(),
]), 'error');
return FALSE;
}
}
$file
->delete();
return TRUE;
}
// Unmanaged file.
return \Drupal::service('file_system')
->delete($uri);
}
/**
* Deletes a folder by uri.
*/
public static function deleteFolderUri($uri, $ignore_usage = FALSE, $check_files = FALSE) {
// Get folder content without any filtering.
$content = Imce::scanDir($uri);
if (!empty($content['error'])) {
return FALSE;
}
if ($check_files && !empty($content['files'])) {
\Drupal::messenger()
->addMessage(t('%folder contains files and can not be deleted.', [
'%folder' => \Drupal::service('file_system')
->basename($uri),
]), 'error');
return FALSE;
}
// Delete subfolders first.
foreach ($content['subfolders'] as $path) {
if (!static::deleteFolderUri($path, $ignore_usage, $check_files)) {
return FALSE;
}
}
// Delete files.
foreach ($content['files'] as $path) {
if (!static::deleteFileUri($path, $ignore_usage)) {
return FALSE;
}
}
// Recently emptied folders need some refreshing
// before the removal on windows.
if (strncasecmp(PHP_OS, 'WIN', 3) == 0) {
@closedir(@opendir($uri));
}
// Remove the folder.
return rmdir($uri);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Delete:: |
public | function |
Builds imce page by adding required libraries and elements. Overrides ImcePluginBase:: |
|
Delete:: |
public static | function | Deletes a file by uri. | |
Delete:: |
public static | function | Deletes a folder by uri. | |
Delete:: |
public | function | Deletes a list of imce items and returns succeeded ones. | |
Delete:: |
public | function | Operation handler: delete. | |
Delete:: |
public | function |
Returns folder permission definitions. Overrides ImcePluginBase:: |
|
Delete:: |
public | function | Validates the deletion of the given items. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
ImcePluginBase:: |
public | function |
Alters entity form of an Imce Profile. Overrides ImcePluginInterface:: |
|
ImcePluginBase:: |
public | function |
Processes profile configuration for a user. Overrides ImcePluginInterface:: |
|
ImcePluginBase:: |
public | function |
Validates entity form of an Imce Profile. Overrides ImcePluginInterface:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
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. |