Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Handle file usage with drupal 8

By editor , 16 December, 2015
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

  • Drupal 8
  • Code
  • Add new comment
CKEditor inline image files usage for custom forms.

Comments1

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Test2124323423423 (not verified)

8 years 9 months ago

Test

Test

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer