file.install 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 == 'uploadprogress') {
  99. $value = t('Enabled (<a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress</a>)');
  100. }
  101. $requirements['file_progress'] = [
  102. 'title' => t('Upload progress'),
  103. 'value' => $value,
  104. 'description' => $description,
  105. ];
  106. }
  107. return $requirements;
  108. }
  109. /**
  110. * Prevent unused files from being deleted.
  111. */
  112. function file_update_8300() {
  113. // Disable deletion of unused permanent files.
  114. \Drupal::configFactory()->getEditable('file.settings')
  115. ->set('make_unused_managed_files_temporary', FALSE)
  116. ->save();
  117. return t('Files that have no remaining usages are no longer deleted by default.');
  118. }
  119. /**
  120. * Add 'use_description_as_link_text' setting to file field formatters.
  121. */
  122. function file_update_8001() {
  123. $displays = EntityViewDisplay::loadMultiple();
  124. foreach ($displays as $display) {
  125. /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
  126. $fields_settings = $display->get('content');
  127. $changed = FALSE;
  128. foreach ($fields_settings as $field_name => $settings) {
  129. if (!empty($settings['type'])) {
  130. switch ($settings['type']) {
  131. // The file_table formatter never displayed available descriptions
  132. // before, so we disable this option to ensure backward compatibility.
  133. case 'file_table':
  134. $fields_settings[$field_name]['settings']['use_description_as_link_text'] = FALSE;
  135. $changed = TRUE;
  136. break;
  137. // The file_default formatter always displayed available descriptions
  138. // before, so we enable this option to ensure backward compatibility.
  139. case 'file_default':
  140. $fields_settings[$field_name]['settings']['use_description_as_link_text'] = TRUE;
  141. $changed = TRUE;
  142. break;
  143. }
  144. }
  145. }
  146. if ($changed === TRUE) {
  147. $display->set('content', $fields_settings)->save();
  148. }
  149. }
  150. }
  151. /**
  152. * Set the 'owner' entity key and update the field.
  153. */
  154. function file_update_8700() {
  155. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  156. $entity_type = $definition_update_manager->getEntityType('file');
  157. $keys = $entity_type->getKeys();
  158. $keys['owner'] = 'uid';
  159. $entity_type->set('entity_keys', $keys);
  160. $definition_update_manager->updateEntityType($entity_type);
  161. $definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition('uid', 'file'));
  162. }