hook_file_move

hook_file_move(Drupal\file\FileInterface $file, Drupal\file\FileInterface $source)

Respond to a file that has been moved.

Parameters

\Drupal\file\FileInterface $file: The updated file entity after the move.

\Drupal\file\FileInterface $source: The original file entity before the move.

See also

file_move()

Related topics

Hooks
Define functions that alter the behavior of Drupal core.

File

core/modules/file/file.api.php, line 70
Hooks for file module.

Code

function hook_file_move(Drupal\file\FileInterface $file, Drupal\file\FileInterface $source) {
  // Make sure that the file name starts with the owner's user name.
  if (strpos($file->getFilename(), $file->getOwner()->name) !== 0) {
    $file->setFilename($file->getOwner()->name . '_' . $file->getFilename());
    $file->save();

    \Drupal::logger('file')->notice('Moved file %source has been renamed to %destination', array('%source' => $source->filename, '%destination' => $file->getFilename()));
  }
}
doc_Drupal
2016-10-29 09:18:13
Comments
Leave a Comment

Please login to continue.