本文整理汇总了PHP中Drupal\file\Entity\File::create方法的典型用法代码示例。如果您正苦于以下问题:PHP File::create方法的具体用法?PHP File::create怎么用?PHP File::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\file\Entity\File
的用法示例。
在下文中一共展示了File::create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testFileDenormalize
/**
* Tests file entity denormalization.
*/
public function testFileDenormalize()
{
$file_params = array('filename' => 'test_1.txt', 'uri' => 'public://test_1.txt', 'filemime' => 'text/plain', 'status' => FILE_STATUS_PERMANENT);
// Create a new file entity.
$file = File::create($file_params);
file_put_contents($file->getFileUri(), 'hello world');
$file->save();
$serializer = \Drupal::service('serializer');
$normalized_data = $serializer->normalize($file, 'hal_json');
$denormalized = $serializer->denormalize($normalized_data, 'Drupal\\file\\Entity\\File', 'hal_json');
$this->assertTrue($denormalized instanceof File, 'A File instance was created.');
$this->assertIdentical('temporary://' . $file->getFilename(), $denormalized->getFileUri(), 'The expected file URI was found.');
$this->assertTrue(file_exists($denormalized->getFileUri()), 'The temporary file was found.');
$this->assertIdentical($file->uuid(), $denormalized->uuid(), 'The expected UUID was found');
$this->assertIdentical($file->getMimeType(), $denormalized->getMimeType(), 'The expected MIME type was found.');
$this->assertIdentical($file->getFilename(), $denormalized->getFilename(), 'The expected filename was found.');
$this->assertTrue($denormalized->isPermanent(), 'The file has a permanent status.');
// Try to denormalize with the file uri only.
$file_name = 'test_2.txt';
$file_path = 'public://' . $file_name;
file_put_contents($file_path, 'hello world');
$file_uri = file_create_url($file_path);
$data = array('uri' => array(array('value' => $file_uri)));
$denormalized = $serializer->denormalize($data, 'Drupal\\file\\Entity\\File', 'hal_json');
$this->assertIdentical('temporary://' . $file_name, $denormalized->getFileUri(), 'The expected file URI was found.');
$this->assertTrue(file_exists($denormalized->getFileUri()), 'The temporary file was found.');
$this->assertIdentical('text/plain', $denormalized->getMimeType(), 'The expected MIME type was found.');
$this->assertIdentical($file_name, $denormalized->getFilename(), 'The expected filename was found.');
$this->assertFalse($denormalized->isPermanent(), 'The file has a permanent status.');
}
示例2: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('file');
$this->installEntitySchema('node');
$this->installSchema('file', ['file_usage']);
$this->installSchema('node', ['node_access']);
$id_mappings = array('d6_file' => array());
// Create new file entities.
for ($i = 1; $i <= 3; $i++) {
$file = File::create(array('fid' => $i, 'uid' => 1, 'filename' => 'druplicon.txt', 'uri' => "public://druplicon-{$i}.txt", 'filemime' => 'text/plain', 'created' => 1, 'changed' => 1, 'status' => FILE_STATUS_PERMANENT));
$file->enforceIsNew();
file_put_contents($file->getFileUri(), 'hello world');
// Save it, inserting a new record.
$file->save();
$id_mappings['d6_file'][] = array(array($i), array($i));
}
$this->prepareMigrations($id_mappings);
$this->migrateContent();
// Since we are only testing a subset of the file migration, do not check
// that the full file migration has been run.
$migration = $this->getMigration('d6_upload');
$migration->set('requirements', []);
$this->executeMigration($migration);
}
示例3: testFileAccess
/**
* Tests if public file is always accessible.
*/
function testFileAccess()
{
// Create a new file entity.
$file = File::create(array('uid' => 1, 'filename' => 'drupal.txt', 'uri' => 'public://drupal.txt', 'filemime' => 'text/plain', 'status' => FILE_STATUS_PERMANENT));
file_put_contents($file->getFileUri(), 'hello world');
// Save it, inserting a new record.
$file->save();
// Create authenticated user to check file access.
$account = $this->createUser(array('access site reports'));
$this->assertTrue($file->access('view', $account), 'Public file is viewable to authenticated user');
$this->assertTrue($file->access('download', $account), 'Public file is downloadable to authenticated user');
// Create anonymous user to check file access.
$account = $this->createUser()->getAnonymousUser();
$this->assertTrue($file->access('view', $account), 'Public file is viewable to anonymous user');
$this->assertTrue($file->access('download', $account), 'Public file is downloadable to anonymous user');
// Create a new file entity.
$file = File::create(array('uid' => 1, 'filename' => 'drupal.txt', 'uri' => 'private://drupal.txt', 'filemime' => 'text/plain', 'status' => FILE_STATUS_PERMANENT));
file_put_contents($file->getFileUri(), 'hello world');
// Save it, inserting a new record.
$file->save();
// Create authenticated user to check file access.
$account = $this->createUser(array('access site reports'));
$this->assertFalse($file->access('view', $account), 'Private file is not viewable to authenticated user');
$this->assertFalse($file->access('download', $account), 'Private file is not downloadable to authenticated user');
// Create anonymous user to check file access.
$account = $this->createUser()->getAnonymousUser();
$this->assertFalse($file->access('view', $account), 'Private file is not viewable to anonymous user');
$this->assertFalse($file->access('download', $account), 'Private file is not downloadable to anonymous user');
}
示例4: createFileWithSize
/**
* Creates a file with a given size.
*
* @param string $uri
* URI of the file to create.
* @param int $size
* Size of the file.
* @param int $uid
* File owner ID.
* @param int $status
* Whether the file should be permanent or temporary.
*
* @return \Drupal\Core\Entity\EntityInterface
* The file entity.
*/
protected function createFileWithSize($uri, $size, $uid, $status = FILE_STATUS_PERMANENT)
{
file_put_contents($uri, $this->randomMachineName($size));
$file = File::create(['uri' => $uri, 'uid' => $uid, 'status' => $status]);
$file->save();
return $file;
}
示例5: testEditorFileReferenceFilter
/**
* Tests the editor file reference filter.
*/
function testEditorFileReferenceFilter()
{
$filter = $this->filters['editor_file_reference'];
$test = function ($input) use($filter) {
return $filter->process($input, 'und');
};
file_put_contents('public://llama.jpg', $this->randomMachineName());
$image = File::create(['uri' => 'public://llama.jpg']);
$image->save();
$id = $image->id();
$uuid = $image->uuid();
$cache_tag = ['file:' . $id];
file_put_contents('public://alpaca.jpg', $this->randomMachineName());
$image_2 = File::create(['uri' => 'public://alpaca.jpg']);
$image_2->save();
$id_2 = $image_2->id();
$uuid_2 = $image_2->uuid();
$cache_tag_2 = ['file:' . $id_2];
$this->pass('No data-entity-type and no data-entity-uuid attribute.');
$input = '<img src="llama.jpg" />';
$output = $test($input);
$this->assertIdentical($input, $output->getProcessedText());
$this->pass('A non-file data-entity-type attribute value.');
$input = '<img src="llama.jpg" data-entity-type="invalid-entity-type-value" data-entity-uuid="' . $uuid . '" />';
$output = $test($input);
$this->assertIdentical($input, $output->getProcessedText());
$this->pass('One data-entity-uuid attribute.');
$input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
$output = $test($input);
$this->assertIdentical($input, $output->getProcessedText());
$this->assertEqual($cache_tag, $output->getCacheTags());
$this->pass('One data-entity-uuid attribute with odd capitalization.');
$input = '<img src="llama.jpg" data-entity-type="file" DATA-entity-UUID = "' . $uuid . '" />';
$output = $test($input);
$this->assertIdentical($input, $output->getProcessedText());
$this->assertEqual($cache_tag, $output->getCacheTags());
$this->pass('One data-entity-uuid attribute on a non-image tag.');
$input = '<video src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
$output = $test($input);
$this->assertIdentical($input, $output->getProcessedText());
$this->assertEqual($cache_tag, $output->getCacheTags());
$this->pass('One data-entity-uuid attribute with an invalid value.');
$input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="invalid-' . $uuid . '" />';
$output = $test($input);
$this->assertIdentical($input, $output->getProcessedText());
$this->assertEqual(array(), $output->getCacheTags());
$this->pass('Two different data-entity-uuid attributes.');
$input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
$input .= '<img src="alpaca.jpg" data-entity-type="file" data-entity-uuid="' . $uuid_2 . '" />';
$output = $test($input);
$this->assertIdentical($input, $output->getProcessedText());
$this->assertEqual(Cache::mergeTags($cache_tag, $cache_tag_2), $output->getCacheTags());
$this->pass('Two identical data-entity-uuid attributes.');
$input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
$input .= '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
$output = $test($input);
$this->assertIdentical($input, $output->getProcessedText());
$this->assertEqual($cache_tag, $output->getCacheTags());
}
示例6: getTestFile
/**
* Retrieves a sample file of the specified type.
*
* @return \Drupal\file\FileInterface
*/
function getTestFile($type_name, $size = NULL)
{
// Get a file to upload.
$file = current($this->drupalGetTestFiles($type_name, $size));
// Add a filesize property to files as would be read by
// \Drupal\file\Entity\File::load().
$file->filesize = filesize($file->uri);
return File::create((array) $file);
}
示例7: testNodeDisplay
/**
* Tests normal formatter display on node display.
*/
function testNodeDisplay()
{
$field_name = strtolower($this->randomMachineName());
$type_name = 'article';
$field_storage_settings = array('display_field' => '1', 'display_default' => '1', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$field_settings = array('description_field' => '1');
$widget_settings = array();
$this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
// Create a new node *without* the file field set, and check that the field
// is not shown for each node display.
$node = $this->drupalCreateNode(array('type' => $type_name));
// Check file_default last as the assertions below assume that this is the
// case.
$file_formatters = array('file_table', 'file_url_plain', 'hidden', 'file_default');
foreach ($file_formatters as $formatter) {
$edit = array("fields[{$field_name}][type]" => $formatter);
$this->drupalPostForm("admin/structure/types/manage/{$type_name}/display", $edit, t('Save'));
$this->drupalGet('node/' . $node->id());
$this->assertNoText($field_name, format_string('Field label is hidden when no file attached for formatter %formatter', array('%formatter' => $formatter)));
}
$test_file = $this->getTestFile('text');
simpletest_generate_file('escaped-&-text', 64, 10, 'text');
$test_file = File::create(['uri' => 'public://escaped-&-text.txt', 'name' => 'escaped-&-text', 'filesize' => filesize('public://escaped-&-text.txt')]);
// Create a new node with the uploaded file.
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
// Check that the default formatter is displaying with the file name.
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$node_storage->resetCache(array($nid));
$node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$file_link = array('#theme' => 'file_link', '#file' => $node_file);
$default_output = \Drupal::service('renderer')->renderRoot($file_link);
$this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
// Turn the "display" option off and check that the file is no longer displayed.
$edit = array($field_name . '[0][display]' => FALSE);
$this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save and keep published'));
$this->assertNoRaw($default_output, 'Field is hidden when "display" option is unchecked.');
// Add a description and make sure that it is displayed.
$description = $this->randomMachineName();
$edit = array($field_name . '[0][description]' => $description, $field_name . '[0][display]' => TRUE);
$this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save and keep published'));
$this->assertText($description);
// Ensure the filename in the link's title attribute is escaped.
$this->assertRaw('title="escaped-&-text.txt"');
// Test that fields appear as expected after during the preview.
// Add a second file.
$name = 'files[' . $field_name . '_1][]';
$edit[$name] = drupal_realpath($test_file->getFileUri());
// Uncheck the display checkboxes and go to the preview.
$edit[$field_name . '[0][display]'] = FALSE;
$edit[$field_name . '[1][display]'] = FALSE;
$this->drupalPostForm("node/{$nid}/edit", $edit, t('Preview'));
$this->clickLink(t('Back to content editing'));
$this->assertRaw($field_name . '[0][display]', 'First file appears as expected.');
$this->assertRaw($field_name . '[1][display]', 'Second file appears as expected.');
}
示例8: setUp
protected function setUp()
{
parent::setUp();
FieldStorageConfig::create(array('entity_type' => 'entity_test', 'field_name' => 'image_test', 'type' => 'image', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED))->save();
FieldConfig::create(['entity_type' => 'entity_test', 'field_name' => 'image_test', 'bundle' => 'entity_test'])->save();
file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
$this->image = File::create(['uri' => 'public://example.jpg']);
$this->image->save();
$this->imageFactory = $this->container->get('image.factory');
}
示例9: testNormalize
/**
* Tests the normalize function.
*/
public function testNormalize()
{
$file_params = array('filename' => 'test_1.txt', 'uri' => 'public://test_1.txt', 'filemime' => 'text/plain', 'status' => FILE_STATUS_PERMANENT);
// Create a new file entity.
$file = File::create($file_params);
file_put_contents($file->getFileUri(), 'hello world');
$file->save();
$expected_array = array('uri' => array(array('value' => file_create_url($file->getFileUri()))));
$normalized = $this->serializer->normalize($file, $this->format);
$this->assertEqual($normalized['uri'], $expected_array['uri'], 'URI is normalized.');
}
示例10: setUp
/**
* {@inheritdoc}
*/
function setUp()
{
parent::setUp();
$this->addLanguage('de');
$filtered_html_format = FilterFormat::create(array('format' => 'filtered_html', 'name' => 'Filtered HTML'));
$filtered_html_format->save();
$this->drupalCreateContentType(array('type' => 'test_bundle'));
$this->loginAsAdmin(array('create translation jobs', 'submit translation jobs', 'create test_bundle content', $filtered_html_format->getPermissionName()));
file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/druplicon.png', 'public://example.jpg');
$this->image = File::create(array('uri' => 'public://example.jpg'));
$this->image->save();
}
示例11: testImageItem
/**
* Tests using entity fields of the image field type.
*/
public function testImageItem()
{
// Create a test entity with the image field set.
$entity = EntityTest::create();
$entity->image_test->target_id = $this->image->id();
$entity->image_test->alt = $alt = $this->randomMachineName();
$entity->image_test->title = $title = $this->randomMachineName();
$entity->name->value = $this->randomMachineName();
$entity->save();
$entity = entity_load('entity_test', $entity->id());
$this->assertTrue($entity->image_test instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->image_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->image_test->target_id, $this->image->id());
$this->assertEqual($entity->image_test->alt, $alt);
$this->assertEqual($entity->image_test->title, $title);
$image = $this->imageFactory->get('public://example.jpg');
$this->assertEqual($entity->image_test->width, $image->getWidth());
$this->assertEqual($entity->image_test->height, $image->getHeight());
$this->assertEqual($entity->image_test->entity->id(), $this->image->id());
$this->assertEqual($entity->image_test->entity->uuid(), $this->image->uuid());
// Make sure the computed entity reflects updates to the referenced file.
file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example-2.jpg');
$image2 = File::create(['uri' => 'public://example-2.jpg']);
$image2->save();
$entity->image_test->target_id = $image2->id();
$entity->image_test->alt = $new_alt = $this->randomMachineName();
// The width and height is only updated when width is not set.
$entity->image_test->width = NULL;
$entity->save();
$this->assertEqual($entity->image_test->entity->id(), $image2->id());
$this->assertEqual($entity->image_test->entity->getFileUri(), $image2->getFileUri());
$image = $this->imageFactory->get('public://example-2.jpg');
$this->assertEqual($entity->image_test->width, $image->getWidth());
$this->assertEqual($entity->image_test->height, $image->getHeight());
$this->assertEqual($entity->image_test->alt, $new_alt);
// Check that the image item can be set to the referenced file directly.
$entity->image_test = $this->image;
$this->assertEqual($entity->image_test->target_id, $this->image->id());
// Delete the image and try to save the entity again.
$this->image->delete();
$entity = EntityTest::create(array('mame' => $this->randomMachineName()));
$entity->save();
// Test image item properties.
$expected = array('target_id', 'entity', 'alt', 'title', 'width', 'height');
$properties = $entity->getFieldDefinition('image_test')->getFieldStorageDefinition()->getPropertyDefinitions();
$this->assertEqual(array_keys($properties), $expected);
// Test the generateSampleValue() method.
$entity = EntityTest::create();
$entity->image_test->generateSampleItems();
$this->entityValidateAndSave($entity);
$this->assertEqual($entity->image_test->entity->get('filemime')->value, 'image/jpeg');
}
示例12: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('file');
$this->installEntitySchema('user');
$this->installSchema('file', array('file_usage'));
$this->installSchema('system', 'sequences');
$this->user1 = User::create(['name' => 'user1', 'status' => 1]);
$this->user1->save();
$this->user2 = User::create(['name' => 'user2', 'status' => 1]);
$this->user2->save();
$this->file = File::create(array('uid' => $this->user1->id(), 'filename' => 'druplicon.txt', 'filemime' => 'text/plain'));
}
示例13: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
ViewTestData::createTestViews(get_class($this), array('file_test_views'));
$this->installEntitySchema('file');
file_put_contents('public://file.png', '');
File::create(['uri' => 'public://file.png', 'filename' => 'file.png'])->save();
file_put_contents('public://file.tar', '');
File::create(['uri' => 'public://file.tar', 'filename' => 'file.tar'])->save();
file_put_contents('public://file.tar.gz', '');
File::create(['uri' => 'public://file.tar.gz', 'filename' => 'file.tar.gz'])->save();
file_put_contents('public://file', '');
File::create(['uri' => 'public://file', 'filename' => 'file'])->save();
}
示例14: createFile
/**
* Saves a file for an entity and returns the file's ID.
*
* @param string $filename
* The file name given by the user.
* @param string $files_path
* The file path where the file exists in.
*
* @return int
* The file ID returned by the File::save() method.
*
* @throws \Exception
* Throws an exception when the file is not found.
*/
public function createFile($filename, $files_path)
{
$path = rtrim(realpath($files_path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
if (!is_file($path)) {
throw new \Exception("File '{$filename}' was not found in file path '{$files_path}'.");
}
// Copy the file into the public files folder and turn it into a File
// entity before linking it to the collection.
$uri = 'public://' . $filename;
$destination = file_unmanaged_copy($path, $uri);
$file = File::create(['uri' => $destination]);
$file->save();
$this->files[$file->id()] = $file;
return $file->id();
}
示例15: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('file');
$this->installSchema('file', ['file_usage']);
$file = File::create(array('fid' => 2, 'uid' => 2, 'filename' => 'image-test.jpg', 'uri' => "public://image-test.jpg", 'filemime' => 'image/jpeg', 'created' => 1, 'changed' => 1, 'status' => FILE_STATUS_PERMANENT));
$file->enforceIsNew();
file_put_contents($file->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png'));
$file->save();
$file = File::create(array('fid' => 8, 'uid' => 8, 'filename' => 'image-test.png', 'uri' => "public://image-test.png", 'filemime' => 'image/png', 'created' => 1, 'changed' => 1, 'status' => FILE_STATUS_PERMANENT));
$file->enforceIsNew();
file_put_contents($file->getFileUri(), file_get_contents('core/modules/simpletest/files/image-2.jpg'));
$file->save();
$this->migrateUsers();
}