Handle file usage with drupal 8 | Drupal 8

Handle file usage with drupal 8

Submitted by editor on Wed, 12/16/2015 - 19:36
Question

How to set file usage on drupal 8 ?
file_usage_add, file_usage_delete, file_usage_list

Equivalent syntax Drupal 7 -> Drupal 8

D7 D8
file_usage_add \Drupal::service('file.usage')->add
file_usage_delete \Drupal::service('file.usage')->delete
file_usage_list \Drupal::service('file.usage')->listUsage

Example :

//Add / Delete file usage
$entity_type = 'node';
$entity_id = 1;//Node ID
$fid = 1; // The file ID
$file = \Drupal\file\Entity\File::load($fid);
$file_usage = \Drupal::service('file.usage');
$file_usage->add($file, 'mymodule', $entity_type, $entity_id);
$list = $file_usage->listUsage($file);
$file_usage->delete($file, 'mymodule', $entity_type, $entity_id);

//Get usage of a file
$file = \Drupal\file\Entity\File::load($fid);
$file_usage = \Drupal::service('file.usage');
$list = $file_usage->listUsage($file);

Get Tempory Directory:
file_directory_temp();

See also :
file_managed_file_save_upload() , file_managed_file_submit(), file_unmanaged_*()

 

More : File usage is pluggable

Tags

Comments

Add new comment