file.install 5.55 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Install, update and uninstall functions for File module.
 */

8 9
use Drupal\Core\Entity\Entity\EntityViewDisplay;

10 11 12 13
/**
 * Implements hook_schema().
 */
function file_schema() {
14
  $schema['file_usage'] = [
15
    'description' => 'Track where a file is used.',
16 17
    'fields' => [
      'fid' => [
18 19 20 21
        'description' => 'File ID.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
22 23
      ],
      'module' => [
24
        'description' => 'The name of the module that is using the file.',
25
        'type' => 'varchar_ascii',
26
        'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
27 28
        'not null' => TRUE,
        'default' => '',
29 30
      ],
      'type' => [
31
        'description' => 'The name of the object type in which the file is used.',
32
        'type' => 'varchar_ascii',
33 34 35
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
36 37
      ],
      'id' => [
38
        'description' => 'The primary key of the object using the file.',
39
        'type' => 'varchar_ascii',
40
        'length' => 64,
41 42
        'not null' => TRUE,
        'default' => 0,
43 44
      ],
      'count' => [
45 46 47 48 49
        'description' => 'The number of times this file is used by this object.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
50 51 52 53 54 55 56 57 58
      ],
    ],
    'primary key' => ['fid', 'type', 'id', 'module'],
    'indexes' => [
      'type_id' => ['type', 'id'],
      'fid_count' => ['fid', 'count'],
      'fid_module' => ['fid', 'module'],
    ],
  ];
59 60 61
  return $schema;
}

62
/**
63
 * Implements hook_requirements().
64 65 66 67
 *
 * Display information about getting upload progress bars working.
 */
function file_requirements($phase) {
68
  $requirements = [];
69 70 71

  // Check the server's ability to indicate upload progress.
  if ($phase == 'runtime') {
72
    $description = NULL;
73
    $implementation = file_progress_implementation();
74
    $server_software = \Drupal::request()->server->get('SERVER_SOFTWARE');
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    // Test the web server identity.
    if (preg_match("/Nginx/i", $server_software)) {
      $is_nginx = TRUE;
      $is_apache = FALSE;
      $fastcgi = FALSE;
    }
    elseif (preg_match("/Apache/i", $server_software)) {
      $is_nginx = FALSE;
      $is_apache = TRUE;
      $fastcgi = strpos($server_software, 'mod_fastcgi') !== FALSE || strpos($server_software, 'mod_fcgi') !== FALSE;
    }
    else {
      $is_nginx = FALSE;
      $is_apache = FALSE;
      $fastcgi = FALSE;
    }

    if (!$is_apache && !$is_nginx) {
94
      $value = t('Not enabled');
95
      $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.');
96 97 98
    }
    elseif ($fastcgi) {
      $value = t('Not enabled');
99
      $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.');
100 101 102
    }
    elseif (!$implementation) {
      $value = t('Not enabled');
103
      $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>.');
104 105
    }
    elseif ($implementation == 'apc') {
106 107
      $value = t('Enabled (<a href="http://php.net/manual/apcu.configuration.php#ini.apcu.rfc1867">APC RFC1867</a>)');
      $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.');
108 109
    }
    elseif ($implementation == 'uploadprogress') {
110
      $value = t('Enabled (<a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress</a>)');
111
    }
112
    $requirements['file_progress'] = [
113 114 115
      'title' => t('Upload progress'),
      'value' => $value,
      'description' => $description,
116
    ];
117 118 119 120
  }

  return $requirements;
}
121 122 123 124 125 126 127 128 129 130 131 132

/**
 * Prevent unused files from being deleted.
 */
function file_update_8300() {
  // Disable deletion of unused permanent files.
  \Drupal::configFactory()->getEditable('file.settings')
    ->set('make_unused_managed_files_temporary', FALSE)
    ->save();

  return t('Files that have no remaining usages are no longer deleted by default.');
}
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

/**
 * Add 'use_description_as_link_text' setting to file field formatters.
 */
function file_update_8001() {
  $displays = EntityViewDisplay::loadMultiple();
  foreach ($displays as $display) {
    /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
    $fields_settings = $display->get('content');
    $changed = FALSE;
    foreach ($fields_settings as $field_name => $settings) {
      if (!empty($settings['type'])) {
        switch ($settings['type']) {
          // The file_table formatter never displayed available descriptions
          // before, so we disable this option to ensure backward compatibility.
          case 'file_table':
            $fields_settings[$field_name]['settings']['use_description_as_link_text'] = FALSE;
            $changed = TRUE;
            break;

          // The file_default formatter always displayed available descriptions
          // before, so we enable this option to ensure backward compatibility.
          case 'file_default':
            $fields_settings[$field_name]['settings']['use_description_as_link_text'] = TRUE;
            $changed = TRUE;
            break;
        }
      }
    }
    if ($changed === TRUE) {
      $display->set('content', $fields_settings)->save();
    }
  }
}