Skip to content
Snippets Groups Projects
Commit 3a987bb4 authored by Nikolay Grachev's avatar Nikolay Grachev
Browse files

Issue #3394697 by granik, greenSkin: Add action link to rename file

parent 375303cc
No related branches found
No related tags found
No related merge requests found
name: File Rename
description: Adds a possibility to rename managed files.
core_version_requirement: ^8.7.7 || ^9 || ^10
core_version_requirement: ^8.9 || ^9 || ^10
type: module
package: File
dependencies:
......
......@@ -5,9 +5,12 @@
* Contains file_rename.module.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\file\FileInterface;
use Drupal\file\Plugin\Field\FieldWidget\FileWidget;
......@@ -224,3 +227,37 @@ function _file_rename_widget_link_is_enabled_globally() {
function _file_rename_link_is_enabled_for_widget(WidgetInterface $widget) {
return !empty($widget->getThirdPartySetting('file_rename', 'show_rename_link'));
}
/**
* Adds rename action link for file entities.
*
* Implements hook_entity_operation().
*/
function file_rename_entity_operation(EntityInterface $entity) {
$operations = [];
if ($entity instanceof FileInterface && $entity->access('rename') && $entity->isPermanent()) {
$operations['rename'] = [
'title' => t('Rename'),
'url' => Url::fromRoute('entity.file.rename_form', [
'file' => $entity->id(),
]),
'weight' => 100,
];
}
return $operations;
}
/**
* Checks user permission for 'rename' operation.
*
* Implements hook_entity_access().
*/
function file_rename_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
if ($entity instanceof FileInterface && $operation === 'rename') {
return $account->hasPermission('rename files')
? AccessResult::allowed()->cachePerPermissions()
: AccessResult::forbidden()->cachePerPermissions();
}
return AccessResult::neutral();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment