file.module 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. <?php
  2. /**
  3. * @file
  4. * Defines a "managed_file" Form API field and a "file" field for Field module.
  5. */
  6. use Drupal\Core\Datetime\Entity\DateFormat;
  7. use Drupal\Core\Field\FieldDefinitionInterface;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\Messenger\MessengerInterface;
  10. use Drupal\Core\Render\BubbleableMetadata;
  11. use Drupal\Core\Render\Element;
  12. use Drupal\Core\Routing\RouteMatchInterface;
  13. use Drupal\Core\Url;
  14. use Drupal\file\Entity\File;
  15. use Drupal\file\FileInterface;
  16. use Drupal\Component\Utility\NestedArray;
  17. use Drupal\Component\Utility\Unicode;
  18. use Drupal\Core\Entity\EntityStorageInterface;
  19. use Drupal\Core\Template\Attribute;
  20. /**
  21. * The regex pattern used when checking for insecure file types.
  22. */
  23. define('FILE_INSECURE_EXTENSION_REGEX', '/\.(php|pl|py|cgi|asp|js)(\.|$)/i');
  24. // Load all Field module hooks for File.
  25. require_once __DIR__ . '/file.field.inc';
  26. /**
  27. * Implements hook_help().
  28. */
  29. function file_help($route_name, RouteMatchInterface $route_match) {
  30. switch ($route_name) {
  31. case 'help.page.file':
  32. $output = '';
  33. $output .= '<h3>' . t('About') . '</h3>';
  34. $output .= '<p>' . t('The File module allows you to create fields that contain files. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":file_documentation">online documentation for the File module</a>.', [':field' => \Drupal::url('help.page', ['name' => 'field']), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#', ':file_documentation' => 'https://www.drupal.org/documentation/modules/file']) . '</p>';
  35. $output .= '<h3>' . t('Uses') . '</h3>';
  36. $output .= '<dl>';
  37. $output .= '<dt>' . t('Managing and displaying file fields') . '</dt>';
  38. $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the file field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#']) . '</dd>';
  39. $output .= '<dt>' . t('Allowing file extensions') . '</dt>';
  40. $output .= '<dd>' . t('In the field settings, you can define the allowed file extensions (for example <em>pdf docx psd</em>) for the files that will be uploaded with the file field.') . '</dd>';
  41. $output .= '<dt>' . t('Storing files') . '</dt>';
  42. $output .= '<dd>' . t('Uploaded files can either be stored as <em>public</em> or <em>private</em>, depending on the <a href=":file-system">File system settings</a>. For more information, see the <a href=":system-help">System module help page</a>.', [':file-system' => \Drupal::url('system.file_system_settings'), ':system-help' => \Drupal::url('help.page', ['name' => 'system'])]) . '</dd>';
  43. $output .= '<dt>' . t('Restricting the maximum file size') . '</dt>';
  44. $output .= '<dd>' . t('The maximum file size that users can upload is limited by PHP settings of the server, but you can restrict by entering the desired value as the <em>Maximum upload size</em> setting. The maximum file size is automatically displayed to users in the help text of the file field.') . '</dd>';
  45. $output .= '<dt>' . t('Displaying files and descriptions') . '<dt>';
  46. $output .= '<dd>' . t('In the field settings, you can allow users to toggle whether individual files are displayed. In the display settings, you can then choose one of the following formats: <ul><li><em>Generic file</em> displays links to the files and adds icons that symbolize the file extensions. If <em>descriptions</em> are enabled and have been submitted, then the description is displayed instead of the file name.</li><li><em>URL to file</em> displays the full path to the file as plain text.</li><li><em>Table of files</em> lists links to the files and the file sizes in a table.</li><li><em>RSS enclosure</em> only displays the first file, and only in a RSS feed, formatted according to the RSS 2.0 syntax for enclosures.</li></ul> A file can still be linked to directly by its URI even if it is not displayed.') . '</dd>';
  47. $output .= '</dl>';
  48. return $output;
  49. }
  50. }
  51. /**
  52. * Implements hook_field_widget_info_alter().
  53. */
  54. function file_field_widget_info_alter(array &$info) {
  55. // Allows using the 'uri' widget for the 'file_uri' field type, which uses it
  56. // as the default widget.
  57. // @see \Drupal\file\Plugin\Field\FieldType\FileUriItem
  58. $info['uri']['field_types'][] = 'file_uri';
  59. }
  60. /**
  61. * Loads file entities from the database.
  62. *
  63. * @param array|null $fids
  64. * (optional) An array of entity IDs. If omitted or NULL, all entities are
  65. * loaded.
  66. * @param bool $reset
  67. * (optional) Whether to reset the internal file_load_multiple() cache.
  68. * Defaults to FALSE.
  69. *
  70. * @return array
  71. * An array of file entities, indexed by fid.
  72. *
  73. * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
  74. * Use \Drupal\file\Entity\File::loadMultiple().
  75. *
  76. * @see hook_ENTITY_TYPE_load()
  77. * @see file_load()
  78. * @see entity_load()
  79. * @see \Drupal\Core\Entity\Query\EntityQueryInterface
  80. */
  81. function file_load_multiple(array $fids = NULL, $reset = FALSE) {
  82. if ($reset) {
  83. \Drupal::entityManager()->getStorage('file')->resetCache($fids);
  84. }
  85. return File::loadMultiple($fids);
  86. }
  87. /**
  88. * Loads a single file entity from the database.
  89. *
  90. * @param int $fid
  91. * A file ID.
  92. * @param bool $reset
  93. * (optional) Whether to reset the internal file_load_multiple() cache.
  94. * Defaults to FALSE.
  95. *
  96. * @return \Drupal\file\FileInterface|null
  97. * A file entity or NULL if the file was not found.
  98. *
  99. * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
  100. * Use \Drupal\file\Entity\File::load().
  101. *
  102. * @see hook_ENTITY_TYPE_load()
  103. * @see file_load_multiple()
  104. */
  105. function file_load($fid, $reset = FALSE) {
  106. if ($reset) {
  107. \Drupal::entityManager()->getStorage('file')->resetCache([$fid]);
  108. }
  109. return File::load($fid);
  110. }
  111. /**
  112. * Copies a file to a new location and adds a file record to the database.
  113. *
  114. * This function should be used when manipulating files that have records
  115. * stored in the database. This is a powerful function that in many ways
  116. * performs like an advanced version of copy().
  117. * - Checks if $source and $destination are valid and readable/writable.
  118. * - If file already exists in $destination either the call will error out,
  119. * replace the file or rename the file based on the $replace parameter.
  120. * - If the $source and $destination are equal, the behavior depends on the
  121. * $replace parameter. FILE_EXISTS_REPLACE will error out. FILE_EXISTS_RENAME
  122. * will rename the file until the $destination is unique.
  123. * - Adds the new file to the files database. If the source file is a
  124. * temporary file, the resulting file will also be a temporary file. See
  125. * file_save_upload() for details on temporary files.
  126. *
  127. * @param \Drupal\file\FileInterface $source
  128. * A file entity.
  129. * @param string $destination
  130. * A string containing the destination that $source should be
  131. * copied to. This must be a stream wrapper URI.
  132. * @param int $replace
  133. * (optional) Replace behavior when the destination file already exists.
  134. * Possible values include:
  135. * - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
  136. * the destination name exists, then its database entry will be updated. If
  137. * no database entry is found, then a new one will be created.
  138. * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
  139. * filename is unique.
  140. * - FILE_EXISTS_ERROR: Do nothing and return FALSE.
  141. *
  142. * @return \Drupal\file\FileInterface|false
  143. * File entity if the copy is successful, or FALSE in the event of an error.
  144. *
  145. * @see file_unmanaged_copy()
  146. * @see hook_file_copy()
  147. */
  148. function file_copy(FileInterface $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  149. if (!file_valid_uri($destination)) {
  150. if (($realpath = \Drupal::service('file_system')->realpath($source->getFileUri())) !== FALSE) {
  151. \Drupal::logger('file')->notice('File %file (%realpath) could not be copied because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%realpath' => $realpath, '%destination' => $destination]);
  152. }
  153. else {
  154. \Drupal::logger('file')->notice('File %file could not be copied because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%destination' => $destination]);
  155. }
  156. \Drupal::messenger()->addError(t('The specified file %file could not be copied because the destination is invalid. More information is available in the system log.', ['%file' => $source->getFileUri()]));
  157. return FALSE;
  158. }
  159. if ($uri = file_unmanaged_copy($source->getFileUri(), $destination, $replace)) {
  160. $file = $source->createDuplicate();
  161. $file->setFileUri($uri);
  162. $file->setFilename(drupal_basename($uri));
  163. // If we are replacing an existing file re-use its database record.
  164. // @todo Do not create a new entity in order to update it. See
  165. // https://www.drupal.org/node/2241865.
  166. if ($replace == FILE_EXISTS_REPLACE) {
  167. $existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
  168. if (count($existing_files)) {
  169. $existing = reset($existing_files);
  170. $file->fid = $existing->id();
  171. $file->setOriginalId($existing->id());
  172. $file->setFilename($existing->getFilename());
  173. }
  174. }
  175. // If we are renaming around an existing file (rather than a directory),
  176. // use its basename for the filename.
  177. elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
  178. $file->setFilename(drupal_basename($destination));
  179. }
  180. $file->save();
  181. // Inform modules that the file has been copied.
  182. \Drupal::moduleHandler()->invokeAll('file_copy', [$file, $source]);
  183. return $file;
  184. }
  185. return FALSE;
  186. }
  187. /**
  188. * Moves a file to a new location and update the file's database entry.
  189. *
  190. * - Checks if $source and $destination are valid and readable/writable.
  191. * - Performs a file move if $source is not equal to $destination.
  192. * - If file already exists in $destination either the call will error out,
  193. * replace the file or rename the file based on the $replace parameter.
  194. * - Adds the new file to the files database.
  195. *
  196. * @param \Drupal\file\FileInterface $source
  197. * A file entity.
  198. * @param string $destination
  199. * A string containing the destination that $source should be moved
  200. * to. This must be a stream wrapper URI.
  201. * @param int $replace
  202. * (optional) The replace behavior when the destination file already exists.
  203. * Possible values include:
  204. * - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
  205. * the destination name exists then its database entry will be updated and
  206. * $source->delete() called after invoking hook_file_move(). If no database
  207. * entry is found, then the source files record will be updated.
  208. * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
  209. * filename is unique.
  210. * - FILE_EXISTS_ERROR: Do nothing and return FALSE.
  211. *
  212. * @return \Drupal\file\FileInterface|false
  213. * Resulting file entity for success, or FALSE in the event of an error.
  214. *
  215. * @see file_unmanaged_move()
  216. * @see hook_file_move()
  217. */
  218. function file_move(FileInterface $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  219. if (!file_valid_uri($destination)) {
  220. if (($realpath = \Drupal::service('file_system')->realpath($source->getFileUri())) !== FALSE) {
  221. \Drupal::logger('file')->notice('File %file (%realpath) could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%realpath' => $realpath, '%destination' => $destination]);
  222. }
  223. else {
  224. \Drupal::logger('file')->notice('File %file could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%destination' => $destination]);
  225. }
  226. \Drupal::messenger()->addError(t('The specified file %file could not be moved because the destination is invalid. More information is available in the system log.', ['%file' => $source->getFileUri()]));
  227. return FALSE;
  228. }
  229. if ($uri = file_unmanaged_move($source->getFileUri(), $destination, $replace)) {
  230. $delete_source = FALSE;
  231. $file = clone $source;
  232. $file->setFileUri($uri);
  233. // If we are replacing an existing file re-use its database record.
  234. if ($replace == FILE_EXISTS_REPLACE) {
  235. $existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
  236. if (count($existing_files)) {
  237. $existing = reset($existing_files);
  238. $delete_source = TRUE;
  239. $file->fid = $existing->id();
  240. $file->uuid = $existing->uuid();
  241. }
  242. }
  243. // If we are renaming around an existing file (rather than a directory),
  244. // use its basename for the filename.
  245. elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
  246. $file->setFilename(drupal_basename($destination));
  247. }
  248. $file->save();
  249. // Inform modules that the file has been moved.
  250. \Drupal::moduleHandler()->invokeAll('file_move', [$file, $source]);
  251. // Delete the original if it's not in use elsewhere.
  252. if ($delete_source && !\Drupal::service('file.usage')->listUsage($source)) {
  253. $source->delete();
  254. }
  255. return $file;
  256. }
  257. return FALSE;
  258. }
  259. /**
  260. * Checks that a file meets the criteria specified by the validators.
  261. *
  262. * After executing the validator callbacks specified hook_file_validate() will
  263. * also be called to allow other modules to report errors about the file.
  264. *
  265. * @param \Drupal\file\FileInterface $file
  266. * A file entity.
  267. * @param array $validators
  268. * (optional) An associative array of callback functions used to validate
  269. * the file. The keys are function names and the values arrays of callback
  270. * parameters which will be passed in after the file entity. The functions
  271. * should return an array of error messages; an empty array indicates that
  272. * the file passed validation. The callback functions will be called in the
  273. * order specified in the array, then the hook hook_file_validate()
  274. * will be invoked so other modules can validate the new file.
  275. *
  276. * @return array
  277. * An array containing validation error messages.
  278. *
  279. * @see hook_file_validate()
  280. */
  281. function file_validate(FileInterface $file, $validators = []) {
  282. // Call the validation functions specified by this function's caller.
  283. $errors = [];
  284. foreach ($validators as $function => $args) {
  285. if (function_exists($function)) {
  286. array_unshift($args, $file);
  287. $errors = array_merge($errors, call_user_func_array($function, $args));
  288. }
  289. }
  290. // Let other modules perform validation on the new file.
  291. return array_merge($errors, \Drupal::moduleHandler()->invokeAll('file_validate', [$file]));
  292. }
  293. /**
  294. * Checks for files with names longer than can be stored in the database.
  295. *
  296. * @param \Drupal\file\FileInterface $file
  297. * A file entity.
  298. *
  299. * @return array
  300. * An empty array if the file name length is smaller than the limit or an
  301. * array containing an error message if it's not or is empty.
  302. */
  303. function file_validate_name_length(FileInterface $file) {
  304. $errors = [];
  305. if (!$file->getFilename()) {
  306. $errors[] = t("The file's name is empty. Please give a name to the file.");
  307. }
  308. if (strlen($file->getFilename()) > 240) {
  309. $errors[] = t("The file's name exceeds the 240 characters limit. Please rename the file and try again.");
  310. }
  311. return $errors;
  312. }
  313. /**
  314. * Checks that the filename ends with an allowed extension.
  315. *
  316. * @param \Drupal\file\FileInterface $file
  317. * A file entity.
  318. * @param string $extensions
  319. * A string with a space separated list of allowed extensions.
  320. *
  321. * @return array
  322. * An empty array if the file extension is allowed or an array containing an
  323. * error message if it's not.
  324. *
  325. * @see hook_file_validate()
  326. */
  327. function file_validate_extensions(FileInterface $file, $extensions) {
  328. $errors = [];
  329. $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')$/i';
  330. if (!preg_match($regex, $file->getFilename())) {
  331. $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', ['%files-allowed' => $extensions]);
  332. }
  333. return $errors;
  334. }
  335. /**
  336. * Checks that the file's size is below certain limits.
  337. *
  338. * @param \Drupal\file\FileInterface $file
  339. * A file entity.
  340. * @param int $file_limit
  341. * (optional) The maximum file size in bytes. Zero (the default) indicates
  342. * that no limit should be enforced.
  343. * @param int $user_limit
  344. * (optional) The maximum number of bytes the user is allowed. Zero (the
  345. * default) indicates that no limit should be enforced.
  346. *
  347. * @return array
  348. * An empty array if the file size is below limits or an array containing an
  349. * error message if it's not.
  350. *
  351. * @see hook_file_validate()
  352. */
  353. function file_validate_size(FileInterface $file, $file_limit = 0, $user_limit = 0) {
  354. $user = \Drupal::currentUser();
  355. $errors = [];
  356. if ($file_limit && $file->getSize() > $file_limit) {
  357. $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', ['%filesize' => format_size($file->getSize()), '%maxsize' => format_size($file_limit)]);
  358. }
  359. // Save a query by only calling spaceUsed() when a limit is provided.
  360. if ($user_limit && (\Drupal::entityManager()->getStorage('file')->spaceUsed($user->id()) + $file->getSize()) > $user_limit) {
  361. $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', ['%filesize' => format_size($file->getSize()), '%quota' => format_size($user_limit)]);
  362. }
  363. return $errors;
  364. }
  365. /**
  366. * Checks that the file is recognized as a valid image.
  367. *
  368. * @param \Drupal\file\FileInterface $file
  369. * A file entity.
  370. *
  371. * @return array
  372. * An empty array if the file is a valid image or an array containing an error
  373. * message if it's not.
  374. *
  375. * @see hook_file_validate()
  376. */
  377. function file_validate_is_image(FileInterface $file) {
  378. $errors = [];
  379. $image_factory = \Drupal::service('image.factory');
  380. $image = $image_factory->get($file->getFileUri());
  381. if (!$image->isValid()) {
  382. $supported_extensions = $image_factory->getSupportedExtensions();
  383. $errors[] = t('The image file is invalid or the image type is not allowed. Allowed types: %types', ['%types' => implode(', ', $supported_extensions)]);
  384. }
  385. return $errors;
  386. }
  387. /**
  388. * Verifies that image dimensions are within the specified maximum and minimum.
  389. *
  390. * Non-image files will be ignored. If an image toolkit is available the image
  391. * will be scaled to fit within the desired maximum dimensions.
  392. *
  393. * @param \Drupal\file\FileInterface $file
  394. * A file entity. This function may resize the file affecting its size.
  395. * @param string|int $maximum_dimensions
  396. * (optional) A string in the form WIDTHxHEIGHT; for example, '640x480' or
  397. * '85x85'. If an image toolkit is installed, the image will be resized down
  398. * to these dimensions. A value of zero (the default) indicates no restriction
  399. * on size, so no resizing will be attempted.
  400. * @param string|int $minimum_dimensions
  401. * (optional) A string in the form WIDTHxHEIGHT. This will check that the
  402. * image meets a minimum size. A value of zero (the default) indicates that
  403. * there is no restriction on size.
  404. *
  405. * @return array
  406. * An empty array if the file meets the specified dimensions, was resized
  407. * successfully to meet those requirements or is not an image. If the image
  408. * does not meet the requirements or an attempt to resize it fails, an array
  409. * containing the error message will be returned.
  410. *
  411. * @see hook_file_validate()
  412. */
  413. function file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
  414. $errors = [];
  415. // Check first that the file is an image.
  416. $image_factory = \Drupal::service('image.factory');
  417. $image = $image_factory->get($file->getFileUri());
  418. if ($image->isValid()) {
  419. $scaling = FALSE;
  420. if ($maximum_dimensions) {
  421. // Check that it is smaller than the given dimensions.
  422. list($width, $height) = explode('x', $maximum_dimensions);
  423. if ($image->getWidth() > $width || $image->getHeight() > $height) {
  424. // Try to resize the image to fit the dimensions.
  425. if ($image->scale($width, $height)) {
  426. $scaling = TRUE;
  427. $image->save();
  428. if (!empty($width) && !empty($height)) {
  429. $message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
  430. [
  431. '%dimensions' => $maximum_dimensions,
  432. '%new_width' => $image->getWidth(),
  433. '%new_height' => $image->getHeight(),
  434. ]);
  435. }
  436. elseif (empty($width)) {
  437. $message = t('The image was resized to fit within the maximum allowed height of %height pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
  438. [
  439. '%height' => $height,
  440. '%new_width' => $image->getWidth(),
  441. '%new_height' => $image->getHeight(),
  442. ]);
  443. }
  444. elseif (empty($height)) {
  445. $message = t('The image was resized to fit within the maximum allowed width of %width pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.',
  446. [
  447. '%width' => $width,
  448. '%new_width' => $image->getWidth(),
  449. '%new_height' => $image->getHeight(),
  450. ]);
  451. }
  452. \Drupal::messenger()->addStatus($message);
  453. }
  454. else {
  455. $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.');
  456. }
  457. }
  458. }
  459. if ($minimum_dimensions) {
  460. // Check that it is larger than the given dimensions.
  461. list($width, $height) = explode('x', $minimum_dimensions);
  462. if ($image->getWidth() < $width || $image->getHeight() < $height) {
  463. if ($scaling) {
  464. $errors[] = t('The resized image is too small. The minimum dimensions are %dimensions pixels and after resizing, the image size will be %widthx%height pixels.',
  465. [
  466. '%dimensions' => $minimum_dimensions,
  467. '%width' => $image->getWidth(),
  468. '%height' => $image->getHeight(),
  469. ]);
  470. }
  471. else {
  472. $errors[] = t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.',
  473. [
  474. '%dimensions' => $minimum_dimensions,
  475. '%width' => $image->getWidth(),
  476. '%height' => $image->getHeight(),
  477. ]);
  478. }
  479. }
  480. }
  481. }
  482. return $errors;
  483. }
  484. /**
  485. * Saves a file to the specified destination and creates a database entry.
  486. *
  487. * @param string $data
  488. * A string containing the contents of the file.
  489. * @param string|null $destination
  490. * (optional) A string containing the destination URI. This must be a stream
  491. * wrapper URI. If no value or NULL is provided, a randomized name will be
  492. * generated and the file will be saved using Drupal's default files scheme,
  493. * usually "public://".
  494. * @param int $replace
  495. * (optional) The replace behavior when the destination file already exists.
  496. * Possible values include:
  497. * - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
  498. * the destination name exists, then its database entry will be updated. If
  499. * no database entry is found, then a new one will be created.
  500. * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
  501. * filename is unique.
  502. * - FILE_EXISTS_ERROR: Do nothing and return FALSE.
  503. *
  504. * @return \Drupal\file\FileInterface|false
  505. * A file entity, or FALSE on error.
  506. *
  507. * @see file_unmanaged_save_data()
  508. */
  509. function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  510. $user = \Drupal::currentUser();
  511. if (empty($destination)) {
  512. $destination = file_default_scheme() . '://';
  513. }
  514. if (!file_valid_uri($destination)) {
  515. \Drupal::logger('file')->notice('The data could not be saved because the destination %destination is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.', ['%destination' => $destination]);
  516. \Drupal::messenger()->addError(t('The data could not be saved because the destination is invalid. More information is available in the system log.'));
  517. return FALSE;
  518. }
  519. if ($uri = file_unmanaged_save_data($data, $destination, $replace)) {
  520. // Create a file entity.
  521. $file = File::create([
  522. 'uri' => $uri,
  523. 'uid' => $user->id(),
  524. 'status' => FILE_STATUS_PERMANENT,
  525. ]);
  526. // If we are replacing an existing file re-use its database record.
  527. // @todo Do not create a new entity in order to update it. See
  528. // https://www.drupal.org/node/2241865.
  529. if ($replace == FILE_EXISTS_REPLACE) {
  530. $existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
  531. if (count($existing_files)) {
  532. $existing = reset($existing_files);
  533. $file->fid = $existing->id();
  534. $file->setOriginalId($existing->id());
  535. $file->setFilename($existing->getFilename());
  536. }
  537. }
  538. // If we are renaming around an existing file (rather than a directory),
  539. // use its basename for the filename.
  540. elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
  541. $file->setFilename(drupal_basename($destination));
  542. }
  543. $file->save();
  544. return $file;
  545. }
  546. return FALSE;
  547. }
  548. /**
  549. * Examines a file entity and returns appropriate content headers for download.
  550. *
  551. * @param \Drupal\file\FileInterface $file
  552. * A file entity.
  553. *
  554. * @return array
  555. * An associative array of headers, as expected by
  556. * \Symfony\Component\HttpFoundation\StreamedResponse.
  557. */
  558. function file_get_content_headers(FileInterface $file) {
  559. $type = Unicode::mimeHeaderEncode($file->getMimeType());
  560. return [
  561. 'Content-Type' => $type,
  562. 'Content-Length' => $file->getSize(),
  563. 'Cache-Control' => 'private',
  564. ];
  565. }
  566. /**
  567. * Implements hook_theme().
  568. */
  569. function file_theme() {
  570. return [
  571. // From file.module.
  572. 'file_link' => [
  573. 'variables' => ['file' => NULL, 'description' => NULL, 'attributes' => []],
  574. ],
  575. 'file_managed_file' => [
  576. 'render element' => 'element',
  577. ],
  578. 'file_audio' => [
  579. 'variables' => ['files' => [], 'attributes' => NULL],
  580. ],
  581. 'file_video' => [
  582. 'variables' => ['files' => [], 'attributes' => NULL],
  583. ],
  584. // From file.field.inc.
  585. 'file_widget_multiple' => [
  586. 'render element' => 'element',
  587. 'file' => 'file.field.inc',
  588. ],
  589. 'file_upload_help' => [
  590. 'variables' => ['description' => NULL, 'upload_validators' => NULL, 'cardinality' => NULL],
  591. 'file' => 'file.field.inc',
  592. ],
  593. ];
  594. }
  595. /**
  596. * Implements hook_file_download().
  597. */
  598. function file_file_download($uri) {
  599. // Get the file record based on the URI. If not in the database just return.
  600. /** @var \Drupal\file\FileInterface[] $files */
  601. $files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
  602. if (count($files)) {
  603. foreach ($files as $item) {
  604. // Since some database servers sometimes use a case-insensitive comparison
  605. // by default, double check that the filename is an exact match.
  606. if ($item->getFileUri() === $uri) {
  607. $file = $item;
  608. break;
  609. }
  610. }
  611. }
  612. if (!isset($file)) {
  613. return;
  614. }
  615. // Find out if a temporary file is still used in the system.
  616. if ($file->isTemporary()) {
  617. $usage = \Drupal::service('file.usage')->listUsage($file);
  618. if (empty($usage) && $file->getOwnerId() != \Drupal::currentUser()->id()) {
  619. // Deny access to temporary files without usage that are not owned by the
  620. // same user. This prevents the security issue that a private file that
  621. // was protected by field permissions becomes available after its usage
  622. // was removed and before it is actually deleted from the file system.
  623. // Modules that depend on this behavior should make the file permanent
  624. // instead.
  625. return -1;
  626. }
  627. }
  628. // Find out which (if any) fields of this type contain the file.
  629. $references = file_get_file_references($file, NULL, EntityStorageInterface::FIELD_LOAD_CURRENT, NULL);
  630. // Stop processing if there are no references in order to avoid returning
  631. // headers for files controlled by other modules. Make an exception for
  632. // temporary files where the host entity has not yet been saved (for example,
  633. // an image preview on a node/add form) in which case, allow download by the
  634. // file's owner.
  635. if (empty($references) && ($file->isPermanent() || $file->getOwnerId() != \Drupal::currentUser()->id())) {
  636. return;
  637. }
  638. if (!$file->access('download')) {
  639. return -1;
  640. }
  641. // Access is granted.
  642. $headers = file_get_content_headers($file);
  643. return $headers;
  644. }
  645. /**
  646. * Implements hook_cron().
  647. */
  648. function file_cron() {
  649. $age = \Drupal::config('system.file')->get('temporary_maximum_age');
  650. $file_storage = \Drupal::entityManager()->getStorage('file');
  651. // Only delete temporary files if older than $age. Note that automatic cleanup
  652. // is disabled if $age set to 0.
  653. if ($age) {
  654. $fids = Drupal::entityQuery('file')
  655. ->condition('status', FILE_STATUS_PERMANENT, '<>')
  656. ->condition('changed', REQUEST_TIME - $age, '<')
  657. ->range(0, 100)
  658. ->execute();
  659. $files = $file_storage->loadMultiple($fids);
  660. foreach ($files as $file) {
  661. $references = \Drupal::service('file.usage')->listUsage($file);
  662. if (empty($references)) {
  663. if (file_exists($file->getFileUri())) {
  664. $file->delete();
  665. }
  666. else {
  667. \Drupal::logger('file system')->error('Could not delete temporary file "%path" during garbage collection', ['%path' => $file->getFileUri()]);
  668. }
  669. }
  670. else {
  671. \Drupal::logger('file system')->info('Did not delete temporary file "%path" during garbage collection because it is in use by the following modules: %modules.', ['%path' => $file->getFileUri(), '%modules' => implode(', ', array_keys($references))]);
  672. }
  673. }
  674. }
  675. }
  676. /**
  677. * Saves form file uploads.
  678. *
  679. * The files will be added to the {file_managed} table as temporary files.
  680. * Temporary files are periodically cleaned. Use the 'file.usage' service to
  681. * register the usage of the file which will automatically mark it as permanent.
  682. *
  683. * @param array $element
  684. * The FAPI element whose values are being saved.
  685. * @param \Drupal\Core\Form\FormStateInterface $form_state
  686. * The current state of the form.
  687. * @param null|int $delta
  688. * (optional) The delta of the file to return the file entity.
  689. * Defaults to NULL.
  690. * @param int $replace
  691. * (optional) The replace behavior when the destination file already exists.
  692. * Possible values include:
  693. * - FILE_EXISTS_REPLACE: Replace the existing file.
  694. * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
  695. * filename is unique.
  696. * - FILE_EXISTS_ERROR: Do nothing and return FALSE.
  697. *
  698. * @return array|\Drupal\file\FileInterface|null|false
  699. * An array of file entities or a single file entity if $delta != NULL. Each
  700. * array element contains the file entity if the upload succeeded or FALSE if
  701. * there was an error. Function returns NULL if no file was uploaded.
  702. *
  703. * @deprecated in Drupal 8.4.x, will be removed before Drupal 9.0.0.
  704. * For backwards compatibility use core file upload widgets in forms.
  705. *
  706. * @internal
  707. * This function wraps file_save_upload() to allow correct error handling in
  708. * forms.
  709. *
  710. * @todo Revisit after https://www.drupal.org/node/2244513.
  711. */
  712. function _file_save_upload_from_form(array $element, FormStateInterface $form_state, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
  713. // Get all errors set before calling this method. This will also clear them
  714. // from $_SESSION.
  715. $errors_before = \Drupal::messenger()->deleteByType(MessengerInterface::TYPE_ERROR);
  716. $upload_location = isset($element['#upload_location']) ? $element['#upload_location'] : FALSE;
  717. $upload_name = implode('_', $element['#parents']);
  718. $upload_validators = isset($element['#upload_validators']) ? $element['#upload_validators'] : [];
  719. $result = file_save_upload($upload_name, $upload_validators, $upload_location, $delta, $replace);
  720. // Get new errors that are generated while trying to save the upload. This
  721. // will also clear them from $_SESSION.
  722. $errors_new = \Drupal::messenger()->deleteByType(MessengerInterface::TYPE_ERROR);
  723. if (!empty($errors_new)) {
  724. if (count($errors_new) > 1) {
  725. // Render multiple errors into a single message.
  726. // This is needed because only one error per element is supported.
  727. $render_array = [
  728. 'error' => [
  729. '#markup' => t('One or more files could not be uploaded.'),
  730. ],
  731. 'item_list' => [
  732. '#theme' => 'item_list',
  733. '#items' => $errors_new,
  734. ],
  735. ];
  736. $error_message = \Drupal::service('renderer')->renderPlain($render_array);
  737. }
  738. else {
  739. $error_message = reset($errors_new);
  740. }
  741. $form_state->setError($element, $error_message);
  742. }
  743. // Ensure that errors set prior to calling this method are still shown to the
  744. // user.
  745. if (!empty($errors_before)) {
  746. foreach ($errors_before as $error) {
  747. \Drupal::messenger()->addError($error);
  748. }
  749. }
  750. return $result;
  751. }
  752. /**
  753. * Saves file uploads to a new location.
  754. *
  755. * The files will be added to the {file_managed} table as temporary files.
  756. * Temporary files are periodically cleaned. Use the 'file.usage' service to
  757. * register the usage of the file which will automatically mark it as permanent.
  758. *
  759. * Note that this function does not support correct form error handling. The
  760. * file upload widgets in core do support this. It is advised to use these in
  761. * any custom form, instead of calling this function.
  762. *
  763. * @param string $form_field_name
  764. * A string that is the associative array key of the upload form element in
  765. * the form array.
  766. * @param array $validators
  767. * (optional) An associative array of callback functions used to validate the
  768. * file. See file_validate() for a full discussion of the array format.
  769. * If the array is empty, it will be set up to call file_validate_extensions()
  770. * with a safe list of extensions, as follows: "jpg jpeg gif png txt doc
  771. * xls pdf ppt pps odt ods odp". To allow all extensions, you must explicitly
  772. * set this array to ['file_validate_extensions' => '']. (Beware: this is not
  773. * safe and should only be allowed for trusted users, if at all.)
  774. * @param string|false $destination
  775. * (optional) A string containing the URI that the file should be copied to.
  776. * This must be a stream wrapper URI. If this value is omitted or set to
  777. * FALSE, Drupal's temporary files scheme will be used ("temporary://").
  778. * @param null|int $delta
  779. * (optional) The delta of the file to return the file entity.
  780. * Defaults to NULL.
  781. * @param int $replace
  782. * (optional) The replace behavior when the destination file already exists.
  783. * Possible values include:
  784. * - FILE_EXISTS_REPLACE: Replace the existing file.
  785. * - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
  786. * filename is unique.
  787. * - FILE_EXISTS_ERROR: Do nothing and return FALSE.
  788. *
  789. * @return array|\Drupal\file\FileInterface|null|false
  790. * An array of file entities or a single file entity if $delta != NULL. Each
  791. * array element contains the file entity if the upload succeeded or FALSE if
  792. * there was an error. Function returns NULL if no file was uploaded.
  793. *
  794. * @see _file_save_upload_from_form()
  795. *
  796. * @todo: move this logic to a service in https://www.drupal.org/node/2244513.
  797. */
  798. function file_save_upload($form_field_name, $validators = [], $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
  799. $user = \Drupal::currentUser();
  800. static $upload_cache;
  801. $all_files = \Drupal::request()->files->get('files', []);
  802. // Make sure there's an upload to process.
  803. if (empty($all_files[$form_field_name])) {
  804. return NULL;
  805. }
  806. $file_upload = $all_files[$form_field_name];
  807. // Return cached objects without processing since the file will have
  808. // already been processed and the paths in $_FILES will be invalid.
  809. if (isset($upload_cache[$form_field_name])) {
  810. if (isset($delta)) {
  811. return $upload_cache[$form_field_name][$delta];
  812. }
  813. return $upload_cache[$form_field_name];
  814. }
  815. // Prepare uploaded files info. Representation is slightly different
  816. // for multiple uploads and we fix that here.
  817. $uploaded_files = $file_upload;
  818. if (!is_array($file_upload)) {
  819. $uploaded_files = [$file_upload];
  820. }
  821. $files = [];
  822. foreach ($uploaded_files as $i => $file_info) {
  823. // Check for file upload errors and return FALSE for this file if a lower
  824. // level system error occurred. For a complete list of errors:
  825. // See http://php.net/manual/features.file-upload.errors.php.
  826. switch ($file_info->getError()) {
  827. case UPLOAD_ERR_INI_SIZE:
  828. case UPLOAD_ERR_FORM_SIZE:
  829. \Drupal::messenger()->addError(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $file_info->getFilename(), '%maxsize' => format_size(file_upload_max_size())]));
  830. $files[$i] = FALSE;
  831. continue;
  832. case UPLOAD_ERR_PARTIAL:
  833. case UPLOAD_ERR_NO_FILE:
  834. \Drupal::messenger()->addError(t('The file %file could not be saved because the upload did not complete.', ['%file' => $file_info->getFilename()]));
  835. $files[$i] = FALSE;
  836. continue;
  837. case UPLOAD_ERR_OK:
  838. // Final check that this is a valid upload, if it isn't, use the
  839. // default error handler.
  840. if (is_uploaded_file($file_info->getRealPath())) {
  841. break;
  842. }
  843. // Unknown error
  844. default:
  845. \Drupal::messenger()->addError(t('The file %file could not be saved. An unknown error has occurred.', ['%file' => $file_info->getFilename()]));
  846. $files[$i] = FALSE;
  847. continue;
  848. }
  849. // Begin building file entity.
  850. $values = [
  851. 'uid' => $user->id(),
  852. 'status' => 0,
  853. 'filename' => $file_info->getClientOriginalName(),
  854. 'uri' => $file_info->getRealPath(),
  855. 'filesize' => $file_info->getSize(),
  856. ];
  857. $values['filemime'] = \Drupal::service('file.mime_type.guesser')->guess($values['filename']);
  858. $file = File::create($values);
  859. $extensions = '';
  860. if (isset($validators['file_validate_extensions'])) {
  861. if (isset($validators['file_validate_extensions'][0])) {
  862. // Build the list of non-munged extensions if the caller provided them.
  863. $extensions = $validators['file_validate_extensions'][0];
  864. }
  865. else {
  866. // If 'file_validate_extensions' is set and the list is empty then the
  867. // caller wants to allow any extension. In this case we have to remove the
  868. // validator or else it will reject all extensions.
  869. unset($validators['file_validate_extensions']);
  870. }
  871. }
  872. else {
  873. // No validator was provided, so add one using the default list.
  874. // Build a default non-munged safe list for file_munge_filename().
  875. $extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
  876. $validators['file_validate_extensions'] = [];
  877. $validators['file_validate_extensions'][0] = $extensions;
  878. }
  879. if (!empty($extensions)) {
  880. // Munge the filename to protect against possible malicious extension
  881. // hiding within an unknown file type (ie: filename.html.foo).
  882. $file->setFilename(file_munge_filename($file->getFilename(), $extensions));
  883. }
  884. // Rename potentially executable files, to help prevent exploits (i.e. will
  885. // rename filename.php.foo and filename.php to filename.php.foo.txt and
  886. // filename.php.txt, respectively). Don't rename if 'allow_insecure_uploads'
  887. // evaluates to TRUE.
  888. if (!\Drupal::config('system.file')->get('allow_insecure_uploads') && preg_match(FILE_INSECURE_EXTENSION_REGEX, $file->getFilename()) && (substr($file->getFilename(), -4) != '.txt')) {
  889. $file->setMimeType('text/plain');
  890. // The destination filename will also later be used to create the URI.
  891. $file->setFilename($file->getFilename() . '.txt');
  892. // The .txt extension may not be in the allowed list of extensions. We have
  893. // to add it here or else the file upload will fail.
  894. if (!empty($extensions)) {
  895. $validators['file_validate_extensions'][0] .= ' txt';
  896. \Drupal::messenger()->addStatus(t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $file->getFilename()]));
  897. }
  898. }
  899. // If the destination is not provided, use the temporary directory.
  900. if (empty($destination)) {
  901. $destination = 'temporary://';
  902. }
  903. // Assert that the destination contains a valid stream.
  904. $destination_scheme = file_uri_scheme($destination);
  905. if (!file_stream_wrapper_valid_scheme($destination_scheme)) {
  906. \Drupal::messenger()->addError(t('The file could not be uploaded because the destination %destination is invalid.', ['%destination' => $destination]));
  907. $files[$i] = FALSE;
  908. continue;
  909. }
  910. $file->source = $form_field_name;
  911. // A file URI may already have a trailing slash or look like "public://".
  912. if (substr($destination, -1) != '/') {
  913. $destination .= '/';
  914. }
  915. $file->destination = file_destination($destination . $file->getFilename(), $replace);
  916. // If file_destination() returns FALSE then $replace === FILE_EXISTS_ERROR and
  917. // there's an existing file so we need to bail.
  918. if ($file->destination === FALSE) {
  919. \Drupal::messenger()->addError(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', ['%source' => $form_field_name, '%directory' => $destination]));
  920. $files[$i] = FALSE;
  921. continue;
  922. }
  923. // Add in our check of the file name length.
  924. $validators['file_validate_name_length'] = [];
  925. // Call the validation functions specified by this function's caller.
  926. $errors = file_validate($file, $validators);
  927. // Check for errors.
  928. if (!empty($errors)) {
  929. $message = [
  930. 'error' => [
  931. '#markup' => t('The specified file %name could not be uploaded.', ['%name' => $file->getFilename()]),
  932. ],
  933. 'item_list' => [
  934. '#theme' => 'item_list',
  935. '#items' => $errors,
  936. ],
  937. ];
  938. // @todo Add support for render arrays in
  939. // \Drupal\Core\Messenger\MessengerInterface::addMessage()?
  940. // @see https://www.drupal.org/node/2505497.
  941. \Drupal::messenger()->addError(\Drupal::service('renderer')->renderPlain($message));
  942. $files[$i] = FALSE;
  943. continue;
  944. }
  945. $file->setFileUri($file->destination);
  946. if (!drupal_move_uploaded_file($file_info->getRealPath(), $file->getFileUri())) {
  947. \Drupal::messenger()->addError(t('File upload error. Could not move uploaded file.'));
  948. \Drupal::logger('file')->notice('Upload error. Could not move uploaded file %file to destination %destination.', ['%file' => $file->getFilename(), '%destination' => $file->getFileUri()]);
  949. $files[$i] = FALSE;
  950. continue;
  951. }
  952. // Set the permissions on the new file.
  953. drupal_chmod($file->getFileUri());
  954. // If we are replacing an existing file re-use its database record.
  955. // @todo Do not create a new entity in order to update it. See
  956. // https://www.drupal.org/node/2241865.
  957. if ($replace == FILE_EXISTS_REPLACE) {
  958. $existing_files = entity_load_multiple_by_properties('file', ['uri' => $file->getFileUri()]);
  959. if (count($existing_files)) {
  960. $existing = reset($existing_files);
  961. $file->fid = $existing->id();
  962. $file->setOriginalId($existing->id());
  963. }
  964. }
  965. // If we made it this far it's safe to record this file in the database.
  966. $file->save();
  967. $files[$i] = $file;
  968. // Allow an anonymous user who creates a non-public file to see it. See
  969. // \Drupal\file\FileAccessControlHandler::checkAccess().
  970. if ($user->isAnonymous() && $destination_scheme !== 'public') {
  971. $session = \Drupal::request()->getSession();
  972. $allowed_temp_files = $session->get('anonymous_allowed_file_ids', []);
  973. $allowed_temp_files[$file->id()] = $file->id();
  974. $session->set('anonymous_allowed_file_ids', $allowed_temp_files);
  975. }
  976. }
  977. // Add files to the cache.
  978. $upload_cache[$form_field_name] = $files;
  979. return isset($delta) ? $files[$delta] : $files;
  980. }
  981. /**
  982. * Determines the preferred upload progress implementation.
  983. *
  984. * @return string|false
  985. * A string indicating which upload progress system is available. Either "apc"
  986. * or "uploadprogress". If neither are available, returns FALSE.
  987. */
  988. function file_progress_implementation() {
  989. static $implementation;
  990. if (!isset($implementation)) {
  991. $implementation = FALSE;
  992. // We prefer the PECL extension uploadprogress because it supports multiple
  993. // simultaneous uploads. APCu only supports one at a time.
  994. if (extension_loaded('uploadprogress')) {
  995. $implementation = 'uploadprogress';
  996. }
  997. elseif (version_compare(PHP_VERSION, '7', '<') && extension_loaded('apc') && ini_get('apc.rfc1867')) {
  998. $implementation = 'apc';
  999. }
  1000. }
  1001. return $implementation;
  1002. }
  1003. /**
  1004. * Implements hook_ENTITY_TYPE_predelete() for file entities.
  1005. */
  1006. function file_file_predelete(File $file) {
  1007. // @todo Remove references to a file that is in-use.
  1008. }
  1009. /**
  1010. * Implements hook_tokens().
  1011. */
  1012. function file_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  1013. $token_service = \Drupal::token();
  1014. $url_options = ['absolute' => TRUE];
  1015. if (isset($options['langcode'])) {
  1016. $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
  1017. $langcode = $options['langcode'];
  1018. }
  1019. else {
  1020. $langcode = NULL;
  1021. }
  1022. $replacements = [];
  1023. if ($type == 'file' && !empty($data['file'])) {
  1024. /** @var \Drupal\file\FileInterface $file */
  1025. $file = $data['file'];
  1026. foreach ($tokens as $name => $original) {
  1027. switch ($name) {
  1028. // Basic keys and values.
  1029. case 'fid':
  1030. $replacements[$original] = $file->id();
  1031. break;
  1032. // Essential file data
  1033. case 'name':
  1034. $replacements[$original] = $file->getFilename();
  1035. break;
  1036. case 'path':
  1037. $replacements[$original] = $file->getFileUri();
  1038. break;
  1039. case 'mime':
  1040. $replacements[$original] = $file->getMimeType();
  1041. break;
  1042. case 'size':
  1043. $replacements[$original] = format_size($file->getSize());
  1044. break;
  1045. case 'url':
  1046. // Ideally, this would use file_url_transform_relative(), but because
  1047. // tokens are also often used in e-mails, it's better to keep absolute
  1048. // file URLs. The 'url.site' cache context is associated to ensure the
  1049. // correct absolute URL is used in case of a multisite setup.
  1050. $replacements[$original] = file_create_url($file->getFileUri());
  1051. $bubbleable_metadata->addCacheContexts(['url.site']);
  1052. break;
  1053. // These tokens are default variations on the chained tokens handled below.
  1054. case 'created':
  1055. $date_format = DateFormat::load('medium');
  1056. $bubbleable_metadata->addCacheableDependency($date_format);
  1057. $replacements[$original] = format_date($file->getCreatedTime(), 'medium', '', NULL, $langcode);
  1058. break;
  1059. case 'changed':
  1060. $date_format = DateFormat::load('medium');
  1061. $bubbleable_metadata = $bubbleable_metadata->addCacheableDependency($date_format);
  1062. $replacements[$original] = format_date($file->getChangedTime(), 'medium', '', NULL, $langcode);
  1063. break;
  1064. case 'owner':
  1065. $owner = $file->getOwner();
  1066. $bubbleable_metadata->addCacheableDependency($owner);
  1067. $name = $owner->label();
  1068. $replacements[$original] = $name;
  1069. break;
  1070. }
  1071. }
  1072. if ($date_tokens = $token_service->findWithPrefix($tokens, 'created')) {
  1073. $replacements += $token_service->generate('date', $date_tokens, ['date' => $file->getCreatedTime()], $options, $bubbleable_metadata);
  1074. }
  1075. if ($date_tokens = $token_service->findWithPrefix($tokens, 'changed')) {
  1076. $replacements += $token_service->generate('date', $date_tokens, ['date' => $file->getChangedTime()], $options, $bubbleable_metadata);
  1077. }
  1078. if (($owner_tokens = $token_service->findWithPrefix($tokens, 'owner')) && $file->getOwner()) {
  1079. $replacements += $token_service->generate('user', $owner_tokens, ['user' => $file->getOwner()], $options, $bubbleable_metadata);
  1080. }
  1081. }
  1082. return $replacements;
  1083. }
  1084. /**
  1085. * Implements hook_token_info().
  1086. */
  1087. function file_token_info() {
  1088. $types['file'] = [
  1089. 'name' => t("Files"),
  1090. 'description' => t("Tokens related to uploaded files."),
  1091. 'needs-data' => 'file',
  1092. ];
  1093. // File related tokens.
  1094. $file['fid'] = [
  1095. 'name' => t("File ID"),
  1096. 'description' => t("The unique ID of the uploaded file."),
  1097. ];
  1098. $file['name'] = [
  1099. 'name' => t("File name"),
  1100. 'description' => t("The name of the file on disk."),
  1101. ];
  1102. $file['path'] = [
  1103. 'name' => t("Path"),
  1104. 'description' => t("The location of the file relative to Drupal root."),
  1105. ];
  1106. $file['mime'] = [
  1107. 'name' => t("MIME type"),
  1108. 'description' => t("The MIME type of the file."),
  1109. ];
  1110. $file['size'] = [
  1111. 'name' => t("File size"),
  1112. 'description' => t("The size of the file."),
  1113. ];
  1114. $file['url'] = [
  1115. 'name' => t("URL"),
  1116. 'description' => t("The web-accessible URL for the file."),
  1117. ];
  1118. $file['created'] = [
  1119. 'name' => t("Created"),
  1120. 'description' => t("The date the file created."),
  1121. 'type' => 'date',
  1122. ];
  1123. $file['changed'] = [
  1124. 'name' => t("Changed"),
  1125. 'description' => t("The date the file was most recently changed."),
  1126. 'type' => 'date',
  1127. ];
  1128. $file['owner'] = [
  1129. 'name' => t("Owner"),
  1130. 'description' => t("The user who originally uploaded the file."),
  1131. 'type' => 'user',
  1132. ];
  1133. return [
  1134. 'types' => $types,
  1135. 'tokens' => [
  1136. 'file' => $file,
  1137. ],
  1138. ];
  1139. }
  1140. /**
  1141. * Form submission handler for upload / remove buttons of managed_file elements.
  1142. *
  1143. * @see \Drupal\file\Element\ManagedFile::processManagedFile()
  1144. */
  1145. function file_managed_file_submit($form, FormStateInterface $form_state) {
  1146. // Determine whether it was the upload or the remove button that was clicked,
  1147. // and set $element to the managed_file element that contains that button.
  1148. $parents = $form_state->getTriggeringElement()['#array_parents'];
  1149. $button_key = array_pop($parents);
  1150. $element = NestedArray::getValue($form, $parents);
  1151. // No action is needed here for the upload button, because all file uploads on
  1152. // the form are processed by \Drupal\file\Element\ManagedFile::valueCallback()
  1153. // regardless of which button was clicked. Action is needed here for the
  1154. // remove button, because we only remove a file in response to its remove
  1155. // button being clicked.
  1156. if ($button_key == 'remove_button') {
  1157. $fids = array_keys($element['#files']);
  1158. // Get files that will be removed.
  1159. if ($element['#multiple']) {
  1160. $remove_fids = [];
  1161. foreach (Element::children($element) as $name) {
  1162. if (strpos($name, 'file_') === 0 && $element[$name]['selected']['#value']) {
  1163. $remove_fids[] = (int) substr($name, 5);
  1164. }
  1165. }
  1166. $fids = array_diff($fids, $remove_fids);
  1167. }
  1168. else {
  1169. // If we deal with single upload element remove the file and set
  1170. // element's value to empty array (file could not be removed from
  1171. // element if we don't do that).
  1172. $remove_fids = $fids;
  1173. $fids = [];
  1174. }
  1175. foreach ($remove_fids as $fid) {
  1176. // If it's a temporary file we can safely remove it immediately, otherwise
  1177. // it's up to the implementing module to remove usages of files to have them
  1178. // removed.
  1179. if ($element['#files'][$fid] && $element['#files'][$fid]->isTemporary()) {
  1180. $element['#files'][$fid]->delete();
  1181. }
  1182. }
  1183. // Update both $form_state->getValues() and FormState::$input to reflect
  1184. // that the file has been removed, so that the form is rebuilt correctly.
  1185. // $form_state->getValues() must be updated in case additional submit
  1186. // handlers run, and for form building functions that run during the
  1187. // rebuild, such as when the managed_file element is part of a field widget.
  1188. // FormState::$input must be updated so that
  1189. // \Drupal\file\Element\ManagedFile::valueCallback() has correct information
  1190. // during the rebuild.
  1191. $form_state->setValueForElement($element['fids'], implode(' ', $fids));
  1192. NestedArray::setValue($form_state->getUserInput(), $element['fids']['#parents'], implode(' ', $fids));
  1193. }
  1194. // Set the form to rebuild so that $form is correctly updated in response to
  1195. // processing the file removal. Since this function did not change $form_state
  1196. // if the upload button was clicked, a rebuild isn't necessary in that
  1197. // situation and calling $form_state->disableRedirect() would suffice.
  1198. // However, we choose to always rebuild, to keep the form processing workflow
  1199. // consistent between the two buttons.
  1200. $form_state->setRebuild();
  1201. }
  1202. /**
  1203. * Saves any files that have been uploaded into a managed_file element.
  1204. *
  1205. * @param array $element
  1206. * The FAPI element whose values are being saved.
  1207. * @param \Drupal\Core\Form\FormStateInterface $form_state
  1208. * The current state of the form.
  1209. *
  1210. * @return array|false
  1211. * An array of file entities for each file that was saved, keyed by its file
  1212. * ID. Each array element contains a file entity. Function returns FALSE if
  1213. * upload directory could not be created or no files were uploaded.
  1214. */
  1215. function file_managed_file_save_upload($element, FormStateInterface $form_state) {
  1216. $upload_name = implode('_', $element['#parents']);
  1217. $all_files = \Drupal::request()->files->get('files', []);
  1218. if (empty($all_files[$upload_name])) {
  1219. return FALSE;
  1220. }
  1221. $file_upload = $all_files[$upload_name];
  1222. $destination = isset($element['#upload_location']) ? $element['#upload_location'] : NULL;
  1223. if (isset($destination) && !file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
  1224. \Drupal::logger('file')->notice('The upload directory %directory for the file field %name could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', ['%directory' => $destination, '%name' => $element['#field_name']]);
  1225. $form_state->setError($element, t('The file could not be uploaded.'));
  1226. return FALSE;
  1227. }
  1228. // Save attached files to the database.
  1229. $files_uploaded = $element['#multiple'] && count(array_filter($file_upload)) > 0;
  1230. $files_uploaded |= !$element['#multiple'] && !empty($file_upload);
  1231. if ($files_uploaded) {
  1232. if (!$files = _file_save_upload_from_form($element, $form_state)) {
  1233. \Drupal::logger('file')->notice('The file upload failed. %upload', ['%upload' => $upload_name]);
  1234. return [];
  1235. }
  1236. // Value callback expects FIDs to be keys.
  1237. $files = array_filter($files);
  1238. $fids = array_map(function ($file) {
  1239. return $file->id();
  1240. }, $files);
  1241. return empty($files) ? [] : array_combine($fids, $files);
  1242. }
  1243. return [];
  1244. }
  1245. /**
  1246. * Prepares variables for file form widget templates.
  1247. *
  1248. * Default template: file-managed-file.html.twig.
  1249. *
  1250. * @param array $variables
  1251. * An associative array containing:
  1252. * - element: A render element representing the file.
  1253. */
  1254. function template_preprocess_file_managed_file(&$variables) {
  1255. $element = $variables['element'];
  1256. $variables['attributes'] = [];
  1257. if (isset($element['#id'])) {
  1258. $variables['attributes']['id'] = $element['#id'];
  1259. }
  1260. if (!empty($element['#attributes']['class'])) {
  1261. $variables['attributes']['class'] = (array) $element['#attributes']['class'];
  1262. }
  1263. }
  1264. /**
  1265. * Prepares variables for file link templates.
  1266. *
  1267. * Default template: file-link.html.twig.
  1268. *
  1269. * @param array $variables
  1270. * An associative array containing:
  1271. * - file: A file object to which the link will be created.
  1272. * - icon_directory: (optional) A path to a directory of icons to be used for
  1273. * files. Defaults to the value of the "icon.directory" variable.
  1274. * - description: A description to be displayed instead of the filename.
  1275. * - attributes: An associative array of attributes to be placed in the a tag.
  1276. */
  1277. function template_preprocess_file_link(&$variables) {
  1278. $file = $variables['file'];
  1279. $options = [];
  1280. $file_entity = ($file instanceof File) ? $file : File::load($file->fid);
  1281. // @todo Wrap in file_url_transform_relative(). This is currently
  1282. // impossible. As a work-around, we currently add the 'url.site' cache context
  1283. // to ensure different file URLs are generated for different sites in a
  1284. // multisite setup, including HTTP and HTTPS versions of the same site.
  1285. // Fix in https://www.drupal.org/node/2646744.
  1286. $url = file_create_url($file_entity->getFileUri());
  1287. $variables['#cache']['contexts'][] = 'url.site';
  1288. $mime_type = $file->getMimeType();
  1289. // Set options as per anchor format described at
  1290. // http://microformats.org/wiki/file-format-examples
  1291. $options['attributes']['type'] = $mime_type . '; length=' . $file->getSize();
  1292. // Use the description as the link text if available.
  1293. if (empty($variables['description'])) {
  1294. $link_text = $file_entity->getFilename();
  1295. }
  1296. else {
  1297. $link_text = $variables['description'];
  1298. $options['attributes']['title'] = $file_entity->getFilename();
  1299. }
  1300. // Classes to add to the file field for icons.
  1301. $classes = [
  1302. 'file',
  1303. // Add a specific class for each and every mime type.
  1304. 'file--mime-' . strtr($mime_type, ['/' => '-', '.' => '-']),
  1305. // Add a more general class for groups of well known MIME types.
  1306. 'file--' . file_icon_class($mime_type),
  1307. ];
  1308. // Set file classes to the options array.
  1309. $variables['attributes'] = new Attribute($variables['attributes']);
  1310. $variables['attributes']->addClass($classes);
  1311. $variables['link'] = \Drupal::l($link_text, Url::fromUri($url, $options));
  1312. }
  1313. /**
  1314. * Gets a class for the icon for a MIME type.
  1315. *
  1316. * @param string $mime_type
  1317. * A MIME type.
  1318. *
  1319. * @return string
  1320. * A class associated with the file.
  1321. */
  1322. function file_icon_class($mime_type) {
  1323. // Search for a group with the files MIME type.
  1324. $generic_mime = (string) file_icon_map($mime_type);
  1325. if (!empty($generic_mime)) {
  1326. return $generic_mime;
  1327. }
  1328. // Use generic icons for each category that provides such icons.
  1329. foreach (['audio', 'image', 'text', 'video'] as $category) {
  1330. if (strpos($mime_type, $category) === 0) {
  1331. return $category;
  1332. }
  1333. }
  1334. // If there's no generic icon for the type the general class.
  1335. return 'general';
  1336. }
  1337. /**
  1338. * Determines the generic icon MIME package based on a file's MIME type.
  1339. *
  1340. * @param string $mime_type
  1341. * A MIME type.
  1342. *
  1343. * @return string|false
  1344. * The generic icon MIME package expected for this file.
  1345. */
  1346. function file_icon_map($mime_type) {
  1347. switch ($mime_type) {
  1348. // Word document types.
  1349. case 'application/msword':
  1350. case 'application/vnd.ms-word.document.macroEnabled.12':
  1351. case 'application/vnd.oasis.opendocument.text':
  1352. case 'application/vnd.oasis.opendocument.text-template':
  1353. case 'application/vnd.oasis.opendocument.text-master':
  1354. case 'application/vnd.oasis.opendocument.text-web':
  1355. case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
  1356. case 'application/vnd.stardivision.writer':
  1357. case 'application/vnd.sun.xml.writer':
  1358. case 'application/vnd.sun.xml.writer.template':
  1359. case 'application/vnd.sun.xml.writer.global':
  1360. case 'application/vnd.wordperfect':
  1361. case 'application/x-abiword':
  1362. case 'application/x-applix-word':
  1363. case 'application/x-kword':
  1364. case 'application/x-kword-crypt':
  1365. return 'x-office-document';
  1366. // Spreadsheet document types.
  1367. case 'application/vnd.ms-excel':
  1368. case 'application/vnd.ms-excel.sheet.macroEnabled.12':
  1369. case 'application/vnd.oasis.opendocument.spreadsheet':
  1370. case 'application/vnd.oasis.opendocument.spreadsheet-template':
  1371. case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
  1372. case 'application/vnd.stardivision.calc':
  1373. case 'application/vnd.sun.xml.calc':
  1374. case 'application/vnd.sun.xml.calc.template':
  1375. case 'application/vnd.lotus-1-2-3':
  1376. case 'application/x-applix-spreadsheet':
  1377. case 'application/x-gnumeric':
  1378. case 'application/x-kspread':
  1379. case 'application/x-kspread-crypt':
  1380. return 'x-office-spreadsheet';
  1381. // Presentation document types.
  1382. case 'application/vnd.ms-powerpoint':
  1383. case 'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
  1384. case 'application/vnd.oasis.opendocument.presentation':
  1385. case 'application/vnd.oasis.opendocument.presentation-template':
  1386. case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
  1387. case 'application/vnd.stardivision.impress':
  1388. case 'application/vnd.sun.xml.impress':
  1389. case 'application/vnd.sun.xml.impress.template':
  1390. case 'application/x-kpresenter':
  1391. return 'x-office-presentation';
  1392. // Compressed archive types.
  1393. case 'application/zip':
  1394. case 'application/x-zip':
  1395. case 'application/stuffit':
  1396. case 'application/x-stuffit':
  1397. case 'application/x-7z-compressed':
  1398. case 'application/x-ace':
  1399. case 'application/x-arj':
  1400. case 'application/x-bzip':
  1401. case 'application/x-bzip-compressed-tar':
  1402. case 'application/x-compress':
  1403. case 'application/x-compressed-tar':
  1404. case 'application/x-cpio-compressed':
  1405. case 'application/x-deb':
  1406. case 'application/x-gzip':
  1407. case 'application/x-java-archive':
  1408. case 'application/x-lha':
  1409. case 'application/x-lhz':
  1410. case 'application/x-lzop':
  1411. case 'application/x-rar':
  1412. case 'application/x-rpm':
  1413. case 'application/x-tzo':
  1414. case 'application/x-tar':
  1415. case 'application/x-tarz':
  1416. case 'application/x-tgz':
  1417. return 'package-x-generic';
  1418. // Script file types.
  1419. case 'application/ecmascript':
  1420. case 'application/javascript':
  1421. case 'application/mathematica':
  1422. case 'application/vnd.mozilla.xul+xml':
  1423. case 'application/x-asp':
  1424. case 'application/x-awk':
  1425. case 'application/x-cgi':
  1426. case 'application/x-csh':
  1427. case 'application/x-m4':
  1428. case 'application/x-perl':
  1429. case 'application/x-php':
  1430. case 'application/x-ruby':
  1431. case 'application/x-shellscript':
  1432. case 'text/vnd.wap.wmlscript':
  1433. case 'text/x-emacs-lisp':
  1434. case 'text/x-haskell':
  1435. case 'text/x-literate-haskell':
  1436. case 'text/x-lua':
  1437. case 'text/x-makefile':
  1438. case 'text/x-matlab':
  1439. case 'text/x-python':
  1440. case 'text/x-sql':
  1441. case 'text/x-tcl':
  1442. return 'text-x-script';
  1443. // HTML aliases.
  1444. case 'application/xhtml+xml':
  1445. return 'text-html';
  1446. // Executable types.
  1447. case 'application/x-macbinary':
  1448. case 'application/x-ms-dos-executable':
  1449. case 'application/x-pef-executable':
  1450. return 'application-x-executable';
  1451. // Acrobat types
  1452. case 'application/pdf':
  1453. case 'application/x-pdf':
  1454. case 'applications/vnd.pdf':
  1455. case 'text/pdf':
  1456. case 'text/x-pdf':
  1457. return 'application-pdf';
  1458. default:
  1459. return FALSE;
  1460. }
  1461. }
  1462. /**
  1463. * Retrieves a list of references to a file.
  1464. *
  1465. * @param \Drupal\file\FileInterface $file
  1466. * A file entity.
  1467. * @param \Drupal\Core\Field\FieldDefinitionInterface|null $field
  1468. * (optional) A field definition to be used for this check. If given,
  1469. * limits the reference check to the given field. Defaults to NULL.
  1470. * @param int $age
  1471. * (optional) A constant that specifies which references to count. Use
  1472. * EntityStorageInterface::FIELD_LOAD_REVISION (the default) to retrieve all
  1473. * references within all revisions or
  1474. * EntityStorageInterface::FIELD_LOAD_CURRENT to retrieve references only in
  1475. * the current revisions of all entities that have references to this file.
  1476. * @param string $field_type
  1477. * (optional) The name of a field type. If given, limits the reference check
  1478. * to fields of the given type. If both $field and $field_type are given but
  1479. * $field is not the same type as $field_type, an empty array will be
  1480. * returned. Defaults to 'file'.
  1481. *
  1482. * @return array
  1483. * A multidimensional array. The keys are field_name, entity_type,
  1484. * entity_id and the value is an entity referencing this file.
  1485. *
  1486. * @ingroup file
  1487. */
  1488. function file_get_file_references(FileInterface $file, FieldDefinitionInterface $field = NULL, $age = EntityStorageInterface::FIELD_LOAD_REVISION, $field_type = 'file') {
  1489. $references = &drupal_static(__FUNCTION__, []);
  1490. $field_columns = &drupal_static(__FUNCTION__ . ':field_columns', []);
  1491. // Fill the static cache, disregard $field and $field_type for now.
  1492. if (!isset($references[$file->id()][$age])) {
  1493. $references[$file->id()][$age] = [];
  1494. $usage_list = \Drupal::service('file.usage')->listUsage($file);
  1495. $file_usage_list = isset($usage_list['file']) ? $usage_list['file'] : [];
  1496. foreach ($file_usage_list as $entity_type_id => $entity_ids) {
  1497. $entities = \Drupal::entityTypeManager()
  1498. ->getStorage($entity_type_id)->loadMultiple(array_keys($entity_ids));
  1499. foreach ($entities as $entity) {
  1500. $bundle = $entity->bundle();
  1501. // We need to find file fields for this entity type and bundle.
  1502. if (!isset($file_fields[$entity_type_id][$bundle])) {
  1503. $file_fields[$entity_type_id][$bundle] = [];
  1504. // This contains the possible field names.
  1505. foreach ($entity->getFieldDefinitions() as $field_name => $field_definition) {
  1506. // If this is the first time this field type is seen, check
  1507. // whether it references files.
  1508. if (!isset($field_columns[$field_definition->getType()])) {
  1509. $field_columns[$field_definition->getType()] = file_field_find_file_reference_column($field_definition);
  1510. }
  1511. // If the field type does reference files then record it.
  1512. if ($field_columns[$field_definition->getType()]) {
  1513. $file_fields[$entity_type_id][$bundle][$field_name] = $field_columns[$field_definition->getType()];
  1514. }
  1515. }
  1516. }
  1517. foreach ($file_fields[$entity_type_id][$bundle] as $field_name => $field_column) {
  1518. // Iterate over the field items to find the referenced file and field
  1519. // name. This will fail if the usage checked is in a non-current
  1520. // revision because field items are from the current
  1521. // revision.
  1522. // We also iterate over all translations because a file can be linked
  1523. // to a language other than the default.
  1524. foreach ($entity->getTranslationLanguages() as $langcode => $language) {
  1525. foreach ($entity->getTranslation($langcode)->get($field_name) as $item) {
  1526. if ($file->id() == $item->{$field_column}) {
  1527. $references[$file->id()][$age][$field_name][$entity_type_id][$entity->id()] = $entity;
  1528. break;
  1529. }
  1530. }
  1531. }
  1532. }
  1533. }
  1534. }
  1535. }
  1536. $return = $references[$file->id()][$age];
  1537. // Filter the static cache down to the requested entries. The usual static
  1538. // cache is very small so this will be very fast.
  1539. if ($field || $field_type) {
  1540. foreach ($return as $field_name => $data) {
  1541. foreach (array_keys($data) as $entity_type_id) {
  1542. $field_storage_definitions = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type_id);
  1543. $current_field = $field_storage_definitions[$field_name];
  1544. if (($field_type && $current_field->getType() != $field_type) || ($field && $field->uuid() != $current_field->uuid())) {
  1545. unset($return[$field_name][$entity_type_id]);
  1546. }
  1547. }
  1548. }
  1549. }
  1550. return $return;
  1551. }
  1552. /**
  1553. * Formats human-readable version of file status.
  1554. *
  1555. * @param int|null $choice
  1556. * (optional) An integer status code. If not set, all statuses are returned.
  1557. * Defaults to NULL.
  1558. *
  1559. * @return \Drupal\Core\StringTranslation\TranslatableMarkup|\Drupal\Core\StringTranslation\TranslatableMarkup[]
  1560. * An array of file statuses or a specified status if $choice is set.
  1561. */
  1562. function _views_file_status($choice = NULL) {
  1563. $status = [
  1564. 0 => t('Temporary'),
  1565. FILE_STATUS_PERMANENT => t('Permanent'),
  1566. ];
  1567. if (isset($choice)) {
  1568. return isset($status[$choice]) ? $status[$choice] : t('Unknown');
  1569. }
  1570. return $status;
  1571. }