file.install 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for File module.
  5. */
  6. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  7. /**
  8. * Implements hook_schema().
  9. */
  10. function file_schema() {
  11. $schema['file_usage'] = [
  12. 'description' => 'Track where a file is used.',
  13. 'fields' => [
  14. 'fid' => [
  15. 'description' => 'File ID.',
  16. 'type' => 'int',
  17. 'unsigned' => TRUE,
  18. 'not null' => TRUE,
  19. ],
  20. 'module' => [
  21. 'description' => 'The name of the module that is using the file.',
  22. 'type' => 'varchar_ascii',
  23. 'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
  24. 'not null' => TRUE,
  25. 'default' => '',
  26. ],
  27. 'type' => [
  28. 'description' => 'The name of the object type in which the file is used.',
  29. 'type' => 'varchar_ascii',
  30. 'length' => 64,
  31. 'not null' => TRUE,
  32. 'default' => '',
  33. ],
  34. 'id' => [
  35. 'description' => 'The primary key of the object using the file.',
  36. 'type' => 'varchar_ascii',
  37. 'length' => 64,
  38. 'not null' => TRUE,
  39. 'default' => 0,
  40. ],
  41. 'count' => [
  42. 'description' => 'The number of times this file is used by this object.',
  43. 'type' => 'int',
  44. 'unsigned' => TRUE,
  45. 'not null' => TRUE,
  46. 'default' => 0,
  47. ],
  48. ],
  49. 'primary key' => ['fid', 'type', 'id', 'module'],
  50. 'indexes' => [
  51. 'type_id' => ['type', 'id'],
  52. 'fid_count' => ['fid', 'count'],
  53. 'fid_module' => ['fid', 'module'],
  54. ],
  55. ];
  56. return $schema;
  57. }
  58. /**
  59. * Implements hook_requirements().
  60. *
  61. * Display information about getting upload progress bars working.
  62. */
  63. function file_requirements($phase) {
  64. $requirements = [];
  65. // Check the server's ability to indicate upload progress.
  66. if ($phase == 'runtime') {
  67. $description = NULL;
  68. $implementation = file_progress_implementation();
  69. $server_software = \Drupal::request()->server->get('SERVER_SOFTWARE');
  70. // Test the web server identity.
  71. if (preg_match("/Nginx/i", $server_software)) {
  72. $is_nginx = TRUE;
  73. $is_apache = FALSE;
  74. $fastcgi = FALSE;
  75. }
  76. elseif (preg_match("/Apache/i", $server_software)) {
  77. $is_nginx = FALSE;
  78. $is_apache = TRUE;
  79. $fastcgi = strpos($server_software, 'mod_fastcgi') !== FALSE || strpos($server_software, 'mod_fcgi') !== FALSE;
  80. }
  81. else {
  82. $is_nginx = FALSE;
  83. $is_apache = FALSE;
  84. $fastcgi = FALSE;
  85. }
  86. if (!$is_apache && !$is_nginx) {
  87. $value = t('Not enabled');
  88. $description = t('Your server is not capable of displaying file upload progress. File upload progress requires an Apache server running PHP with mod_php or Nginx with PHP-FPM.');
  89. }
  90. elseif ($fastcgi) {
  91. $value = t('Not enabled');
  92. $description = t('Your server is not capable of displaying file upload progress. File upload progress requires PHP be run with mod_php or PHP-FPM and not as FastCGI.');
  93. }
  94. elseif (!$implementation) {
  95. $value = t('Not enabled');
  96. $description = t('Your server is capable of displaying file upload progress, but does not have the required libraries. It is recommended to install the <a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress library</a>.');
  97. }
  98. elseif ($implementation == 'apc') {
  99. $value = t('Enabled (<a href="http://php.net/manual/apcu.configuration.php#ini.apcu.rfc1867">APC RFC1867</a>)');
  100. $description = t('Your server is capable of displaying file upload progress using APC RFC1867. Note that only one upload at a time is supported. It is recommended to use the <a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress library</a> if possible.');
  101. }
  102. elseif ($implementation == 'uploadprogress') {
  103. $value = t('Enabled (<a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress</a>)');
  104. }
  105. $requirements['file_progress'] = [
  106. 'title' => t('Upload progress'),
  107. 'value' => $value,
  108. 'description' => $description,
  109. ];
  110. }
  111. return $requirements;
  112. }
  113. /**
  114. * Prevent unused files from being deleted.
  115. */
  116. function file_update_8300() {
  117. // Disable deletion of unused permanent files.
  118. \Drupal::configFactory()->getEditable('file.settings')
  119. ->set('make_unused_managed_files_temporary', FALSE)
  120. ->save();
  121. return t('Files that have no remaining usages are no longer deleted by default.');
  122. }
  123. /**
  124. * Add 'use_description_as_link_text' setting to file field formatters.
  125. */
  126. function file_update_8001() {
  127. $displays = EntityViewDisplay::loadMultiple();
  128. foreach ($displays as $display) {
  129. /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
  130. $fields_settings = $display->get('content');
  131. $changed = FALSE;
  132. foreach ($fields_settings as $field_name => $settings) {
  133. if (!empty($settings['type'])) {
  134. switch ($settings['type']) {
  135. // The file_table formatter never displayed available descriptions
  136. // before, so we disable this option to ensure backward compatibility.
  137. case 'file_table':
  138. $fields_settings[$field_name]['settings']['use_description_as_link_text'] = FALSE;
  139. $changed = TRUE;
  140. break;
  141. // The file_default formatter always displayed available descriptions
  142. // before, so we enable this option to ensure backward compatibility.
  143. case 'file_default':
  144. $fields_settings[$field_name]['settings']['use_description_as_link_text'] = TRUE;
  145. $changed = TRUE;
  146. break;
  147. }
  148. }
  149. }
  150. if ($changed === TRUE) {
  151. $display->set('content', $fields_settings)->save();
  152. }
  153. }
  154. }