/** * Tests the Drupal 6 user pictures to Drupal 8 migration. */ public function testUserPictures() { $file_ids = array(); foreach (entity_load('migration', 'd6_user_picture_file')->getIdMap() as $destination_ids) { $file_ids[] = reset($destination_ids); } $files = File::loadMultiple($file_ids); /** @var \Drupal\file\FileInterface $file */ $file = array_shift($files); $this->assertIdentical('image-test.jpg', $file->getFilename()); $this->assertIdentical('public://image-test.jpg', $file->getFileUri()); $this->assertIdentical('2', $file->getOwnerId()); $this->assertIdentical('1901', $file->getSize()); $this->assertIdentical('image/jpeg', $file->getMimeType()); $file = array_shift($files); $this->assertIdentical('image-test.png', $file->getFilename()); $this->assertIdentical('public://image-test.png', $file->getFileUri()); $this->assertIdentical('8', $file->getOwnerId()); $this->assertFalse($files); }
/** * This will test loading file data from the database. */ function testMultiple() { // Create a new file entity. $file = $this->createFile('druplicon.txt', NULL, 'public'); // Load by path. file_test_reset(); $by_path_files = entity_load_multiple_by_properties('file', array('uri' => $file->getFileUri())); $this->assertFileHookCalled('load'); $this->assertEqual(1, count($by_path_files), 'entity_load_multiple_by_properties() returned an array of the correct size.'); $by_path_file = reset($by_path_files); $this->assertTrue($by_path_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.'); $this->assertEqual($by_path_file->id(), $file->id(), 'Loading by filepath got the correct fid.', 'File'); // Load by fid. file_test_reset(); $by_fid_files = File::loadMultiple(array($file->id())); $this->assertFileHooksCalled(array()); $this->assertEqual(1, count($by_fid_files), '\\Drupal\\file\\Entity\\File::loadMultiple() returned an array of the correct size.'); $by_fid_file = reset($by_fid_files); $this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.'); $this->assertEqual($by_fid_file->getFileUri(), $file->getFileUri(), 'Loading by fid got the correct filepath.', 'File'); }
/** * Test the file_save_upload() function. */ function testNormal() { $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); $this->assertTrue($max_fid_after > $this->maxFidBefore, 'A new file was created.'); $file1 = File::load($max_fid_after); $this->assertTrue($file1, 'Loaded the file.'); // MIME type of the uploaded image may be either image/jpeg or image/png. $this->assertEqual(substr($file1->getMimeType(), 0, 5), 'image', 'A MIME type was set.'); // Reset the hook counters to get rid of the 'load' we just called. file_test_reset(); // Upload a second file. $image2 = current($this->drupalGetTestFiles('image')); $edit = array('files[file_test_upload]' => drupal_realpath($image2->uri)); $this->drupalPostForm('file-test/upload', $edit, t('Submit')); $this->assertResponse(200, 'Received a 200 response for posted test file.'); $this->assertRaw(t('You WIN!')); $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); // Check that the correct hooks were called. $this->assertFileHooksCalled(array('validate', 'insert')); $file2 = File::load($max_fid_after); $this->assertTrue($file2, 'Loaded the file'); // MIME type of the uploaded image may be either image/jpeg or image/png. $this->assertEqual(substr($file2->getMimeType(), 0, 5), 'image', 'A MIME type was set.'); // Load both files using File::loadMultiple(). $files = File::loadMultiple(array($file1->id(), $file2->id())); $this->assertTrue(isset($files[$file1->id()]), 'File was loaded successfully'); $this->assertTrue(isset($files[$file2->id()]), 'File was loaded successfully'); // Upload a third file to a subdirectory. $image3 = current($this->drupalGetTestFiles('image')); $image3_realpath = drupal_realpath($image3->uri); $dir = $this->randomMachineName(); $edit = array('files[file_test_upload]' => $image3_realpath, 'file_subdir' => $dir); $this->drupalPostForm('file-test/upload', $edit, t('Submit')); $this->assertResponse(200, 'Received a 200 response for posted test file.'); $this->assertRaw(t('You WIN!')); $this->assertTrue(is_file('temporary://' . $dir . '/' . trim(drupal_basename($image3_realpath)))); }
/** * Render API callback: Expands the managed_file element type. * * Expands the file type to include Upload and Remove buttons, as well as * support for a default value. */ public static function processManagedFile(&$element, FormStateInterface $form_state, &$complete_form) { // Append the '-upload' to the #id so the field label's 'for' attribute // corresponds with the file element. $element['#id'] .= '-upload'; // This is used sometimes so let's implode it just once. $parents_prefix = implode('_', $element['#parents']); $fids = isset($element['#value']['fids']) ? $element['#value']['fids'] : []; // Set some default element properties. $element['#progress_indicator'] = empty($element['#progress_indicator']) ? 'none' : $element['#progress_indicator']; $element['#files'] = !empty($fids) ? File::loadMultiple($fids) : FALSE; $element['#tree'] = TRUE; $ajax_settings = ['path' => 'file/ajax', 'options' => ['query' => ['element_parents' => implode('/', $element['#array_parents']), 'form_build_id' => $complete_form['form_build_id']['#value']]], 'wrapper' => $element['#id'] . '-ajax-wrapper', 'effect' => 'fade', 'progress' => ['type' => $element['#progress_indicator'], 'message' => $element['#progress_message']]]; // Set up the buttons first since we need to check if they were clicked. $element['upload_button'] = ['#name' => $parents_prefix . '_upload_button', '#type' => 'submit', '#value' => t('Upload'), '#attributes' => ['class' => ['js-hide']], '#validate' => [], '#submit' => ['file_managed_file_submit'], '#limit_validation_errors' => [$element['#parents']], '#ajax' => $ajax_settings, '#weight' => -5]; // Force the progress indicator for the remove button to be either 'none' or // 'throbber', even if the upload button is using something else. $ajax_settings['progress']['type'] = $element['#progress_indicator'] == 'none' ? 'none' : 'throbber'; $ajax_settings['progress']['message'] = NULL; $ajax_settings['effect'] = 'none'; $element['remove_button'] = ['#name' => $parents_prefix . '_remove_button', '#type' => 'submit', '#value' => $element['#multiple'] ? t('Remove selected') : t('Remove'), '#validate' => [], '#submit' => ['file_managed_file_submit'], '#limit_validation_errors' => [$element['#parents']], '#ajax' => $ajax_settings, '#weight' => 1]; $element['fids'] = ['#type' => 'hidden', '#value' => $fids]; // Add progress bar support to the upload if possible. if ($element['#progress_indicator'] == 'bar' && ($implementation = file_progress_implementation())) { $upload_progress_key = mt_rand(); if ($implementation == 'uploadprogress') { $element['UPLOAD_IDENTIFIER'] = ['#type' => 'hidden', '#value' => $upload_progress_key, '#attributes' => ['class' => ['file-progress']], '#weight' => -20]; } elseif ($implementation == 'apc') { $element['APC_UPLOAD_PROGRESS'] = ['#type' => 'hidden', '#value' => $upload_progress_key, '#attributes' => ['class' => ['file-progress']], '#weight' => -20]; } // Add the upload progress callback. $element['upload_button']['#ajax']['progress']['path'] = 'file/progress/' . $upload_progress_key; } // The file upload field itself. $element['upload'] = ['#name' => 'files[' . $parents_prefix . ']', '#type' => 'file', '#title' => t('Choose a file'), '#title_display' => 'invisible', '#size' => $element['#size'], '#multiple' => $element['#multiple'], '#theme_wrappers' => [], '#weight' => -10]; if (!empty($fids) && $element['#files']) { foreach ($element['#files'] as $delta => $file) { $file_link = ['#theme' => 'file_link', '#file' => $file]; if ($element['#multiple']) { $element['file_' . $delta]['selected'] = ['#type' => 'checkbox', '#title' => drupal_render($file_link)]; } else { $element['file_' . $delta]['filename'] = $file_link + ['#weight' => -10]; } } } // Add the extension list to the page as JavaScript settings. if (isset($element['#upload_validators']['file_validate_extensions'][0])) { $extension_list = implode(',', array_filter(explode(' ', $element['#upload_validators']['file_validate_extensions'][0]))); $element['upload']['#attached']['js'] = [['type' => 'setting', 'data' => ['file' => ['elements' => ['#' . $element['#id'] => $extension_list]]]]]; } // Prefix and suffix used for Ajax replacement. $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">'; $element['#suffix'] = '</div>'; return $element; }
/** * Implements SourceInterface::getAttachments(). */ function getAttachments() { if ($cache = $this->cache->get('data', 'attachments')) { return $cache; } $attachments = array(); $build = $this->build(); $fids = array(); foreach ($this->getEntity()->getFieldDefinitions() as $field_name => $field_definition) { // @todo: Find a better way to support more field types. // Only add fields of type file which are enabled for the current view // mode as attachments. if ($field_definition->getType() == 'file' && isset($build[$field_name])) { if ($items = $this->getEntity()->get($field_name)) { foreach ($items as $item) { $fids[] = $item->target_id; } } } } if (!empty($fids)) { $attachments = File::loadMultiple($fids); } $this->cache->set('data', 'attachments', $attachments); return $attachments; }
/** * Render API callback: Expands the managed_file element type. * * Expands the file type to include Upload and Remove buttons, as well as * support for a default value. */ public static function processManagedFile(&$element, FormStateInterface $form_state, &$complete_form) { // This is used sometimes so let's implode it just once. $parents_prefix = implode('_', $element['#parents']); $fids = isset($element['#value']['fids']) ? $element['#value']['fids'] : []; // Set some default element properties. $element['#progress_indicator'] = empty($element['#progress_indicator']) ? 'none' : $element['#progress_indicator']; $element['#files'] = !empty($fids) ? File::loadMultiple($fids) : FALSE; $element['#tree'] = TRUE; // Generate a unique wrapper HTML ID. $ajax_wrapper_id = Html::getUniqueId('ajax-wrapper'); $ajax_settings = ['callback' => [get_called_class(), 'uploadAjaxCallback'], 'options' => ['query' => ['element_parents' => implode('/', $element['#array_parents'])]], 'wrapper' => $ajax_wrapper_id, 'effect' => 'fade', 'progress' => ['type' => $element['#progress_indicator'], 'message' => $element['#progress_message']]]; // Set up the buttons first since we need to check if they were clicked. $element['upload_button'] = ['#name' => $parents_prefix . '_upload_button', '#type' => 'submit', '#value' => t('Upload'), '#attributes' => ['class' => ['js-hide']], '#validate' => [], '#submit' => ['file_managed_file_submit'], '#limit_validation_errors' => [$element['#parents']], '#ajax' => $ajax_settings, '#weight' => -5]; // Force the progress indicator for the remove button to be either 'none' or // 'throbber', even if the upload button is using something else. $ajax_settings['progress']['type'] = $element['#progress_indicator'] == 'none' ? 'none' : 'throbber'; $ajax_settings['progress']['message'] = NULL; $ajax_settings['effect'] = 'none'; $element['remove_button'] = ['#name' => $parents_prefix . '_remove_button', '#type' => 'submit', '#value' => $element['#multiple'] ? t('Remove selected') : t('Remove'), '#validate' => [], '#submit' => ['file_managed_file_submit'], '#limit_validation_errors' => [$element['#parents']], '#ajax' => $ajax_settings, '#weight' => 1]; $element['fids'] = ['#type' => 'hidden', '#value' => $fids]; // Add progress bar support to the upload if possible. if ($element['#progress_indicator'] == 'bar' && ($implementation = file_progress_implementation())) { $upload_progress_key = mt_rand(); if ($implementation == 'uploadprogress') { $element['UPLOAD_IDENTIFIER'] = ['#type' => 'hidden', '#value' => $upload_progress_key, '#attributes' => ['class' => ['file-progress']], '#weight' => -20]; } elseif ($implementation == 'apc') { $element['APC_UPLOAD_PROGRESS'] = ['#type' => 'hidden', '#value' => $upload_progress_key, '#attributes' => ['class' => ['file-progress']], '#weight' => -20]; } // Add the upload progress callback. $element['upload_button']['#ajax']['progress']['url'] = Url::fromRoute('file.ajax_progress', ['key' => $upload_progress_key]); } // The file upload field itself. $element['upload'] = ['#name' => 'files[' . $parents_prefix . ']', '#type' => 'file', '#title' => t('Choose a file'), '#title_display' => 'invisible', '#size' => $element['#size'], '#multiple' => $element['#multiple'], '#theme_wrappers' => [], '#weight' => -10, '#error_no_message' => TRUE]; if (!empty($fids) && $element['#files']) { foreach ($element['#files'] as $delta => $file) { $file_link = ['#theme' => 'file_link', '#file' => $file]; if ($element['#multiple']) { $element['file_' . $delta]['selected'] = ['#type' => 'checkbox', '#title' => \Drupal::service('renderer')->renderPlain($file_link)]; } else { $element['file_' . $delta]['filename'] = $file_link + ['#weight' => -10]; } // Anonymous users who have uploaded a temporary file need a // non-session-based token added so $this->valueCallback() can check // that they have permission to use this file on subsequent submissions // of the same form (for example, after an Ajax upload or form // validation error). if ($file->isTemporary() && \Drupal::currentUser()->isAnonymous()) { $element['file_' . $delta]['fid_token'] = array('#type' => 'hidden', '#value' => Crypt::hmacBase64('file-' . $delta, \Drupal::service('private_key')->get() . Settings::getHashSalt())); } } } // Add the extension list to the page as JavaScript settings. if (isset($element['#upload_validators']['file_validate_extensions'][0])) { $extension_list = implode(',', array_filter(explode(' ', $element['#upload_validators']['file_validate_extensions'][0]))); $element['upload']['#attached']['drupalSettings']['file']['elements']['#' . $element['#id']] = $extension_list; } // Let #id point to the file element, so the field label's 'for' corresponds // with it. $element['#id'] =& $element['upload']['#id']; // Prefix and suffix used for Ajax replacement. $element['#prefix'] = '<div id="' . $ajax_wrapper_id . '">'; $element['#suffix'] = '</div>'; return $element; }