ManagedFile.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. namespace Drupal\file\Element;
  3. use Drupal\Component\Utility\Crypt;
  4. use Drupal\Component\Utility\Html;
  5. use Drupal\Component\Utility\NestedArray;
  6. use Drupal\Core\Ajax\AjaxResponse;
  7. use Drupal\Core\Ajax\ReplaceCommand;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\Render\Element;
  10. use Drupal\Core\Render\Element\FormElement;
  11. use Drupal\Core\Site\Settings;
  12. use Drupal\Core\Url;
  13. use Drupal\file\Entity\File;
  14. use Symfony\Component\HttpFoundation\Request;
  15. /**
  16. * Provides an AJAX/progress aware widget for uploading and saving a file.
  17. *
  18. * @FormElement("managed_file")
  19. */
  20. class ManagedFile extends FormElement {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function getInfo() {
  25. $class = get_class($this);
  26. return [
  27. '#input' => TRUE,
  28. '#process' => [
  29. [$class, 'processManagedFile'],
  30. ],
  31. '#element_validate' => [
  32. [$class, 'validateManagedFile'],
  33. ],
  34. '#pre_render' => [
  35. [$class, 'preRenderManagedFile'],
  36. ],
  37. '#theme' => 'file_managed_file',
  38. '#theme_wrappers' => ['form_element'],
  39. '#progress_indicator' => 'throbber',
  40. '#progress_message' => NULL,
  41. '#upload_validators' => [],
  42. '#upload_location' => NULL,
  43. '#size' => 22,
  44. '#multiple' => FALSE,
  45. '#extended' => FALSE,
  46. '#attached' => [
  47. 'library' => ['file/drupal.file'],
  48. ],
  49. '#accept' => NULL,
  50. ];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  56. // Find the current value of this field.
  57. $fids = !empty($input['fids']) ? explode(' ', $input['fids']) : [];
  58. foreach ($fids as $key => $fid) {
  59. $fids[$key] = (int) $fid;
  60. }
  61. $force_default = FALSE;
  62. // Process any input and save new uploads.
  63. if ($input !== FALSE) {
  64. $input['fids'] = $fids;
  65. $return = $input;
  66. // Uploads take priority over all other values.
  67. if ($files = file_managed_file_save_upload($element, $form_state)) {
  68. if ($element['#multiple']) {
  69. $fids = array_merge($fids, array_keys($files));
  70. }
  71. else {
  72. $fids = array_keys($files);
  73. }
  74. }
  75. else {
  76. // Check for #filefield_value_callback values.
  77. // Because FAPI does not allow multiple #value_callback values like it
  78. // does for #element_validate and #process, this fills the missing
  79. // functionality to allow File fields to be extended through FAPI.
  80. if (isset($element['#file_value_callbacks'])) {
  81. foreach ($element['#file_value_callbacks'] as $callback) {
  82. $callback($element, $input, $form_state);
  83. }
  84. }
  85. // Load files if the FIDs have changed to confirm they exist.
  86. if (!empty($input['fids'])) {
  87. $fids = [];
  88. foreach ($input['fids'] as $fid) {
  89. if ($file = File::load($fid)) {
  90. $fids[] = $file->id();
  91. // Temporary files that belong to other users should never be
  92. // allowed.
  93. if ($file->isTemporary()) {
  94. if ($file->getOwnerId() != \Drupal::currentUser()->id()) {
  95. $force_default = TRUE;
  96. break;
  97. }
  98. // Since file ownership can't be determined for anonymous users,
  99. // they are not allowed to reuse temporary files at all. But
  100. // they do need to be able to reuse their own files from earlier
  101. // submissions of the same form, so to allow that, check for the
  102. // token added by $this->processManagedFile().
  103. elseif (\Drupal::currentUser()->isAnonymous()) {
  104. $token = NestedArray::getValue($form_state->getUserInput(), array_merge($element['#parents'], ['file_' . $file->id(), 'fid_token']));
  105. if ($token !== Crypt::hmacBase64('file-' . $file->id(), \Drupal::service('private_key')->get() . Settings::getHashSalt())) {
  106. $force_default = TRUE;
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. if ($force_default) {
  114. $fids = [];
  115. }
  116. }
  117. }
  118. }
  119. // If there is no input or if the default value was requested above, use the
  120. // default value.
  121. if ($input === FALSE || $force_default) {
  122. if ($element['#extended']) {
  123. $default_fids = isset($element['#default_value']['fids']) ? $element['#default_value']['fids'] : [];
  124. $return = isset($element['#default_value']) ? $element['#default_value'] : ['fids' => []];
  125. }
  126. else {
  127. $default_fids = isset($element['#default_value']) ? $element['#default_value'] : [];
  128. $return = ['fids' => []];
  129. }
  130. // Confirm that the file exists when used as a default value.
  131. if (!empty($default_fids)) {
  132. $fids = [];
  133. foreach ($default_fids as $fid) {
  134. if ($file = File::load($fid)) {
  135. $fids[] = $file->id();
  136. }
  137. }
  138. }
  139. }
  140. $return['fids'] = $fids;
  141. return $return;
  142. }
  143. /**
  144. * #ajax callback for managed_file upload forms.
  145. *
  146. * This ajax callback takes care of the following things:
  147. * - Ensures that broken requests due to too big files are caught.
  148. * - Adds a class to the response to be able to highlight in the UI, that a
  149. * new file got uploaded.
  150. *
  151. * @param array $form
  152. * The build form.
  153. * @param \Drupal\Core\Form\FormStateInterface $form_state
  154. * The form state.
  155. * @param \Symfony\Component\HttpFoundation\Request $request
  156. * The current request.
  157. *
  158. * @return \Drupal\Core\Ajax\AjaxResponse
  159. * The ajax response of the ajax upload.
  160. */
  161. public static function uploadAjaxCallback(&$form, FormStateInterface &$form_state, Request $request) {
  162. /** @var \Drupal\Core\Render\RendererInterface $renderer */
  163. $renderer = \Drupal::service('renderer');
  164. $form_parents = explode('/', $request->query->get('element_parents'));
  165. // Sanitize form parents before using them.
  166. $form_parents = array_filter($form_parents, [Element::class, 'child']);
  167. // Retrieve the element to be rendered.
  168. $form = NestedArray::getValue($form, $form_parents);
  169. // Add the special AJAX class if a new file was added.
  170. $current_file_count = $form_state->get('file_upload_delta_initial');
  171. if (isset($form['#file_upload_delta']) && $current_file_count < $form['#file_upload_delta']) {
  172. $form[$current_file_count]['#attributes']['class'][] = 'ajax-new-content';
  173. }
  174. // Otherwise just add the new content class on a placeholder.
  175. else {
  176. $form['#suffix'] .= '<span class="ajax-new-content"></span>';
  177. }
  178. $status_messages = ['#type' => 'status_messages'];
  179. $form['#prefix'] .= $renderer->renderRoot($status_messages);
  180. $output = $renderer->renderRoot($form);
  181. $response = new AjaxResponse();
  182. $response->setAttachments($form['#attached']);
  183. return $response->addCommand(new ReplaceCommand(NULL, $output));
  184. }
  185. /**
  186. * Render API callback: Expands the managed_file element type.
  187. *
  188. * Expands the file type to include Upload and Remove buttons, as well as
  189. * support for a default value.
  190. */
  191. public static function processManagedFile(&$element, FormStateInterface $form_state, &$complete_form) {
  192. // This is used sometimes so let's implode it just once.
  193. $parents_prefix = implode('_', $element['#parents']);
  194. $fids = isset($element['#value']['fids']) ? $element['#value']['fids'] : [];
  195. // Set some default element properties.
  196. $element['#progress_indicator'] = empty($element['#progress_indicator']) ? 'none' : $element['#progress_indicator'];
  197. $element['#files'] = !empty($fids) ? File::loadMultiple($fids) : FALSE;
  198. $element['#tree'] = TRUE;
  199. // Generate a unique wrapper HTML ID.
  200. $ajax_wrapper_id = Html::getUniqueId('ajax-wrapper');
  201. $ajax_settings = [
  202. 'callback' => [get_called_class(), 'uploadAjaxCallback'],
  203. 'options' => [
  204. 'query' => [
  205. 'element_parents' => implode('/', $element['#array_parents']),
  206. ],
  207. ],
  208. 'wrapper' => $ajax_wrapper_id,
  209. 'effect' => 'fade',
  210. 'progress' => [
  211. 'type' => $element['#progress_indicator'],
  212. 'message' => $element['#progress_message'],
  213. ],
  214. ];
  215. // Set up the buttons first since we need to check if they were clicked.
  216. $element['upload_button'] = [
  217. '#name' => $parents_prefix . '_upload_button',
  218. '#type' => 'submit',
  219. '#value' => t('Upload'),
  220. '#attributes' => ['class' => ['js-hide']],
  221. '#validate' => [],
  222. '#submit' => ['file_managed_file_submit'],
  223. '#limit_validation_errors' => [$element['#parents']],
  224. '#ajax' => $ajax_settings,
  225. '#weight' => -5,
  226. ];
  227. // Force the progress indicator for the remove button to be either 'none' or
  228. // 'throbber', even if the upload button is using something else.
  229. $ajax_settings['progress']['type'] = ($element['#progress_indicator'] == 'none') ? 'none' : 'throbber';
  230. $ajax_settings['progress']['message'] = NULL;
  231. $ajax_settings['effect'] = 'none';
  232. $element['remove_button'] = [
  233. '#name' => $parents_prefix . '_remove_button',
  234. '#type' => 'submit',
  235. '#value' => $element['#multiple'] ? t('Remove selected') : t('Remove'),
  236. '#validate' => [],
  237. '#submit' => ['file_managed_file_submit'],
  238. '#limit_validation_errors' => [$element['#parents']],
  239. '#ajax' => $ajax_settings,
  240. '#weight' => 1,
  241. ];
  242. $element['fids'] = [
  243. '#type' => 'hidden',
  244. '#value' => $fids,
  245. ];
  246. // Add progress bar support to the upload if possible.
  247. if ($element['#progress_indicator'] == 'bar' && $implementation = file_progress_implementation()) {
  248. $upload_progress_key = mt_rand();
  249. if ($implementation == 'uploadprogress') {
  250. $element['UPLOAD_IDENTIFIER'] = [
  251. '#type' => 'hidden',
  252. '#value' => $upload_progress_key,
  253. '#attributes' => ['class' => ['file-progress']],
  254. // Uploadprogress extension requires this field to be at the top of
  255. // the form.
  256. '#weight' => -20,
  257. ];
  258. }
  259. elseif ($implementation == 'apc') {
  260. $element['APC_UPLOAD_PROGRESS'] = [
  261. '#type' => 'hidden',
  262. '#value' => $upload_progress_key,
  263. '#attributes' => ['class' => ['file-progress']],
  264. // Uploadprogress extension requires this field to be at the top of
  265. // the form.
  266. '#weight' => -20,
  267. ];
  268. }
  269. // Add the upload progress callback.
  270. $element['upload_button']['#ajax']['progress']['url'] = Url::fromRoute('file.ajax_progress', ['key' => $upload_progress_key]);
  271. }
  272. // The file upload field itself.
  273. $element['upload'] = [
  274. '#name' => 'files[' . $parents_prefix . ']',
  275. '#type' => 'file',
  276. '#title' => t('Choose a file'),
  277. '#title_display' => 'invisible',
  278. '#size' => $element['#size'],
  279. '#multiple' => $element['#multiple'],
  280. '#theme_wrappers' => [],
  281. '#weight' => -10,
  282. '#error_no_message' => TRUE,
  283. ];
  284. if (!empty($element['#accept'])) {
  285. $element['upload']['#attributes'] = ['accept' => $element['#accept']];
  286. }
  287. if (!empty($fids) && $element['#files']) {
  288. foreach ($element['#files'] as $delta => $file) {
  289. $file_link = [
  290. '#theme' => 'file_link',
  291. '#file' => $file,
  292. ];
  293. if ($element['#multiple']) {
  294. $element['file_' . $delta]['selected'] = [
  295. '#type' => 'checkbox',
  296. '#title' => \Drupal::service('renderer')->renderPlain($file_link),
  297. ];
  298. }
  299. else {
  300. $element['file_' . $delta]['filename'] = $file_link + ['#weight' => -10];
  301. }
  302. // Anonymous users who have uploaded a temporary file need a
  303. // non-session-based token added so $this->valueCallback() can check
  304. // that they have permission to use this file on subsequent submissions
  305. // of the same form (for example, after an Ajax upload or form
  306. // validation error).
  307. if ($file->isTemporary() && \Drupal::currentUser()->isAnonymous()) {
  308. $element['file_' . $delta]['fid_token'] = [
  309. '#type' => 'hidden',
  310. '#value' => Crypt::hmacBase64('file-' . $delta, \Drupal::service('private_key')->get() . Settings::getHashSalt()),
  311. ];
  312. }
  313. }
  314. }
  315. // Add the extension list to the page as JavaScript settings.
  316. if (isset($element['#upload_validators']['file_validate_extensions'][0])) {
  317. $extension_list = implode(',', array_filter(explode(' ', $element['#upload_validators']['file_validate_extensions'][0])));
  318. $element['upload']['#attached']['drupalSettings']['file']['elements']['#' . $element['#id']] = $extension_list;
  319. }
  320. // Let #id point to the file element, so the field label's 'for' corresponds
  321. // with it.
  322. $element['#id'] = &$element['upload']['#id'];
  323. // Prefix and suffix used for Ajax replacement.
  324. $element['#prefix'] = '<div id="' . $ajax_wrapper_id . '">';
  325. $element['#suffix'] = '</div>';
  326. return $element;
  327. }
  328. /**
  329. * Render API callback: Hides display of the upload or remove controls.
  330. *
  331. * Upload controls are hidden when a file is already uploaded. Remove controls
  332. * are hidden when there is no file attached. Controls are hidden here instead
  333. * of in \Drupal\file\Element\ManagedFile::processManagedFile(), because
  334. * #access for these buttons depends on the managed_file element's #value. See
  335. * the documentation of \Drupal\Core\Form\FormBuilderInterface::doBuildForm()
  336. * for more detailed information about the relationship between #process,
  337. * #value, and #access.
  338. *
  339. * Because #access is set here, it affects display only and does not prevent
  340. * JavaScript or other untrusted code from submitting the form as though
  341. * access were enabled. The form processing functions for these elements
  342. * should not assume that the buttons can't be "clicked" just because they are
  343. * not displayed.
  344. *
  345. * @see \Drupal\file\Element\ManagedFile::processManagedFile()
  346. * @see \Drupal\Core\Form\FormBuilderInterface::doBuildForm()
  347. */
  348. public static function preRenderManagedFile($element) {
  349. // If we already have a file, we don't want to show the upload controls.
  350. if (!empty($element['#value']['fids'])) {
  351. if (!$element['#multiple']) {
  352. $element['upload']['#access'] = FALSE;
  353. $element['upload_button']['#access'] = FALSE;
  354. }
  355. }
  356. // If we don't already have a file, there is nothing to remove.
  357. else {
  358. $element['remove_button']['#access'] = FALSE;
  359. }
  360. return $element;
  361. }
  362. /**
  363. * Render API callback: Validates the managed_file element.
  364. */
  365. public static function validateManagedFile(&$element, FormStateInterface $form_state, &$complete_form) {
  366. $clicked_button = end($form_state->getTriggeringElement()['#parents']);
  367. if ($clicked_button != 'remove_button' && !empty($element['fids']['#value'])) {
  368. $fids = $element['fids']['#value'];
  369. foreach ($fids as $fid) {
  370. if ($file = File::load($fid)) {
  371. // If referencing an existing file, only allow if there are existing
  372. // references. This prevents unmanaged files from being deleted if
  373. // this item were to be deleted. When files that are no longer in use
  374. // are automatically marked as temporary (now disabled by default),
  375. // it is not safe to reference a permanent file without usage. Adding
  376. // a usage and then later on removing it again would delete the file,
  377. // but it is unknown if and where it is currently referenced. However,
  378. // when files are not marked temporary (and then removed)
  379. // automatically, it is safe to add and remove usages, as it would
  380. // simply return to the current state.
  381. // @see https://www.drupal.org/node/2891902
  382. if ($file->isPermanent() && \Drupal::config('file.settings')->get('make_unused_managed_files_temporary')) {
  383. $references = static::fileUsage()->listUsage($file);
  384. if (empty($references)) {
  385. // We expect the field name placeholder value to be wrapped in t()
  386. // here, so it won't be escaped again as it's already marked safe.
  387. $form_state->setError($element, t('The file used in the @name field may not be referenced.', ['@name' => $element['#title']]));
  388. }
  389. }
  390. }
  391. else {
  392. // We expect the field name placeholder value to be wrapped in t()
  393. // here, so it won't be escaped again as it's already marked safe.
  394. $form_state->setError($element, t('The file referenced by the @name field does not exist.', ['@name' => $element['#title']]));
  395. }
  396. }
  397. }
  398. // Check required property based on the FID.
  399. if ($element['#required'] && empty($element['fids']['#value']) && !in_array($clicked_button, ['upload_button', 'remove_button'])) {
  400. // We expect the field name placeholder value to be wrapped in t()
  401. // here, so it won't be escaped again as it's already marked safe.
  402. $form_state->setError($element, t('@name field is required.', ['@name' => $element['#title']]));
  403. }
  404. // Consolidate the array value of this field to array of FIDs.
  405. if (!$element['#extended']) {
  406. $form_state->setValueForElement($element, $element['fids']['#value']);
  407. }
  408. }
  409. /**
  410. * Wraps the file usage service.
  411. *
  412. * @return \Drupal\file\FileUsage\FileUsageInterface
  413. */
  414. protected static function fileUsage() {
  415. return \Drupal::service('file.usage');
  416. }
  417. }