本文整理汇总了PHP中Drupal\file\Entity\File::getFileUri方法的典型用法代码示例。如果您正苦于以下问题:PHP File::getFileUri方法的具体用法?PHP File::getFileUri怎么用?PHP File::getFileUri使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\file\Entity\File
的用法示例。
在下文中一共展示了File::getFileUri方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testFileItem
/**
* Tests using entity fields of the file field type.
*/
public function testFileItem()
{
// Create a test entity with the
$entity = entity_create('entity_test');
$entity->file_test->target_id = $this->file->id();
$entity->file_test->display = 1;
$entity->file_test->description = $description = $this->randomName();
$entity->name->value = $this->randomName();
$entity->save();
$entity = entity_load('entity_test', $entity->id());
$this->assertTrue($entity->file_test instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->file_test->target_id, $this->file->id());
$this->assertEqual($entity->file_test->display, 1);
$this->assertEqual($entity->file_test->description, $description);
$this->assertEqual($entity->file_test->entity->getFileUri(), $this->file->getFileUri());
$this->assertEqual($entity->file_test->entity->url(), $url = file_create_url($this->file->getFileUri()));
$this->assertEqual($entity->file_test->entity->id(), $this->file->id());
$this->assertEqual($entity->file_test->entity->uuid(), $this->file->uuid());
// Make sure the computed files reflects updates to the file.
file_put_contents('public://example-2.txt', $this->randomName());
$file2 = entity_create('file', array('uri' => 'public://example-2.txt'));
$file2->save();
$entity->file_test->target_id = $file2->id();
$this->assertEqual($entity->file_test->entity->id(), $file2->id());
$this->assertEqual($entity->file_test->entity->getFileUri(), $file2->getFileUri());
// Test the deletion of an entity having an entity reference field targeting
// a non-existing entity.
$file2->delete();
$entity->delete();
}
示例2: testFileValidateIsImage
/**
* This ensures a specific file is actually an image.
*/
function testFileValidateIsImage()
{
$this->assertTrue(file_exists($this->image->getFileUri()), 'The image being tested exists.', 'File');
$errors = file_validate_is_image($this->image);
$this->assertEqual(count($errors), 0, 'No error reported for our image file.', 'File');
$this->assertTrue(file_exists($this->nonImage->getFileUri()), 'The non-image being tested exists.', 'File');
$errors = file_validate_is_image($this->nonImage);
$this->assertEqual(count($errors), 1, 'An error reported for our non-image file.', 'File');
}
示例3: testFileItem
/**
* Tests using entity fields of the file field type.
*/
public function testFileItem()
{
// Check that the selection handler was automatically assigned to
// 'default:file'.
$field_definition = FieldConfig::load('entity_test.entity_test.file_test');
$handler_id = $field_definition->getSetting('handler');
$this->assertEqual($handler_id, 'default:file');
// Create a test entity with the
$entity = EntityTest::create();
$entity->file_test->target_id = $this->file->id();
$entity->file_test->display = 1;
$entity->file_test->description = $description = $this->randomMachineName();
$entity->name->value = $this->randomMachineName();
$entity->save();
$entity = EntityTest::load($entity->id());
$this->assertTrue($entity->file_test instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->file_test->target_id, $this->file->id());
$this->assertEqual($entity->file_test->display, 1);
$this->assertEqual($entity->file_test->description, $description);
$this->assertEqual($entity->file_test->entity->getFileUri(), $this->file->getFileUri());
$this->assertEqual($entity->file_test->entity->url(), $url = file_create_url($this->file->getFileUri()));
$this->assertEqual($entity->file_test->entity->id(), $this->file->id());
$this->assertEqual($entity->file_test->entity->uuid(), $this->file->uuid());
// Make sure the computed files reflects updates to the file.
file_put_contents('public://example-2.txt', $this->randomMachineName());
$file2 = File::create(['uri' => 'public://example-2.txt']);
$file2->save();
$entity->file_test->target_id = $file2->id();
$this->assertEqual($entity->file_test->entity->id(), $file2->id());
$this->assertEqual($entity->file_test->entity->getFileUri(), $file2->getFileUri());
// Test the deletion of an entity having an entity reference field targeting
// a non-existing entity.
$file2->delete();
$entity->delete();
// Test the generateSampleValue() method.
$entity = EntityTest::create();
$entity->file_test->generateSampleItems();
$this->entityValidateAndSave($entity);
// Verify that the sample file was stored in the correct directory.
$uri = $entity->file_test->entity->getFileUri();
$this->assertEqual($this->directory, dirname(file_uri_target($uri)));
// Make sure the computed files reflects updates to the file.
file_put_contents('public://example-3.txt', $this->randomMachineName());
// Test unsaved file entity.
$file3 = File::create(['uri' => 'public://example-3.txt']);
$display = entity_get_display('entity_test', 'entity_test', 'default');
$display->setComponent('file_test', ['label' => 'above', 'type' => 'file_default', 'weight' => 1])->save();
$entity = EntityTest::create();
$entity->file_test = array('entity' => $file3);
$uri = $file3->getFileUri();
$output = entity_view($entity, 'default');
\Drupal::service('renderer')->renderRoot($output);
$this->assertTrue(!empty($entity->file_test->entity));
$this->assertEqual($entity->file_test->entity->getFileUri(), $uri);
}
示例4: testFileItem
/**
* Tests using entity fields of the file field type.
*/
public function testFileItem()
{
// Create a test entity with the
$entity = entity_create('entity_test');
$entity->file_test->target_id = $this->file->id();
$entity->file_test->display = 1;
$entity->file_test->description = $description = $this->randomMachineName();
$entity->name->value = $this->randomMachineName();
$entity->save();
$entity = entity_load('entity_test', $entity->id());
$this->assertTrue($entity->file_test instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->file_test->target_id, $this->file->id());
$this->assertEqual($entity->file_test->display, 1);
$this->assertEqual($entity->file_test->description, $description);
$this->assertEqual($entity->file_test->entity->getFileUri(), $this->file->getFileUri());
$this->assertEqual($entity->file_test->entity->url(), $url = file_create_url($this->file->getFileUri()));
$this->assertEqual($entity->file_test->entity->id(), $this->file->id());
$this->assertEqual($entity->file_test->entity->uuid(), $this->file->uuid());
// Make sure the computed files reflects updates to the file.
file_put_contents('public://example-2.txt', $this->randomMachineName());
$file2 = entity_create('file', array('uri' => 'public://example-2.txt'));
$file2->save();
$entity->file_test->target_id = $file2->id();
$this->assertEqual($entity->file_test->entity->id(), $file2->id());
$this->assertEqual($entity->file_test->entity->getFileUri(), $file2->getFileUri());
// Test the deletion of an entity having an entity reference field targeting
// a non-existing entity.
$file2->delete();
$entity->delete();
// Test the generateSampleValue() method.
$entity = entity_create('entity_test');
$entity->file_test->generateSampleItems();
$this->entityValidateAndSave($entity);
// Make sure the computed files reflects updates to the file.
file_put_contents('public://example-3.txt', $this->randomMachineName());
// Test unsaved file entity.
$file3 = entity_create('file', array('uri' => 'public://example-3.txt'));
$display = entity_get_display('entity_test', 'entity_test', 'default');
$display->setComponent('file_test', ['label' => 'above', 'type' => 'file_default', 'weight' => 1])->save();
$entity = entity_create('entity_test');
$entity->file_test = array('entity' => $file3);
$uri = $file3->getFileUri();
$output = entity_view($entity, 'default');
\Drupal::service('renderer')->renderRoot($output);
$this->assertTrue(!empty($entity->file_test->entity));
$this->assertEqual($entity->file_test->entity->getFileUri(), $uri);
}
示例5: testFileValidateImageResolution
/**
* This ensures the resolution of a specific file is within bounds.
* The image will be resized if it's too large.
*/
function testFileValidateImageResolution()
{
// Non-images.
$errors = file_validate_image_resolution($this->non_image);
$this->assertEqual(count($errors), 0, 'Should not get any errors for a non-image file.', 'File');
$errors = file_validate_image_resolution($this->non_image, '50x50', '100x100');
$this->assertEqual(count($errors), 0, 'Do not check the resolution on non files.', 'File');
// Minimum size.
$errors = file_validate_image_resolution($this->image);
$this->assertEqual(count($errors), 0, 'No errors for an image when there is no minimum or maximum resolution.', 'File');
$errors = file_validate_image_resolution($this->image, 0, '200x1');
$this->assertEqual(count($errors), 1, 'Got an error for an image that was not wide enough.', 'File');
$errors = file_validate_image_resolution($this->image, 0, '1x200');
$this->assertEqual(count($errors), 1, 'Got an error for an image that was not tall enough.', 'File');
$errors = file_validate_image_resolution($this->image, 0, '200x200');
$this->assertEqual(count($errors), 1, 'Small images report an error.', 'File');
// Maximum size.
if ($this->container->get('image.factory')->getToolkitId()) {
// Copy the image so that the original doesn't get resized.
copy('core/misc/druplicon.png', 'temporary://druplicon.png');
$this->image->setFileUri('temporary://druplicon.png');
$errors = file_validate_image_resolution($this->image, '10x5');
$this->assertEqual(count($errors), 0, 'No errors should be reported when an oversized image can be scaled down.', 'File');
$image = $this->container->get('image.factory')->get($this->image->getFileUri());
$this->assertTrue($image->getWidth() <= 10, 'Image scaled to correct width.', 'File');
$this->assertTrue($image->getHeight() <= 5, 'Image scaled to correct height.', 'File');
// Once again, now with negative width and height to force an error.
copy('core/misc/druplicon.png', 'temporary://druplicon.png');
$this->image->setFileUri('temporary://druplicon.png');
$errors = file_validate_image_resolution($this->image, '-10x-5');
$this->assertEqual(count($errors), 1, 'An error reported for an oversized image that can not be scaled down.', 'File');
drupal_unlink('temporary://druplicon.png');
} else {
// TODO: should check that the error is returned if no toolkit is available.
$errors = file_validate_image_resolution($this->image, '5x10');
$this->assertEqual(count($errors), 1, 'Oversize images that cannot be scaled get an error.', 'File');
}
}
示例6: buildUrl
/**
* Create the URL for a preview image including a query parameter.
*
* @param \Drupal\image\Entity\ImageStyle $style
* The image style being previewed.
* @param \Drupal\file\Entity\File $image
* The image being previewed.
* @param string $focal_point_value
* The focal point being previewed in the format XxY where x and y are the
* left and top offsets in percentages.
*
* @return \Drupal\Core\GeneratedUrl|string
* The URL of the preview image.
*/
protected function buildUrl(ImageStyle $style, File $image, $focal_point_value) {
$url = $style->buildUrl($image->getFileUri());
$url .= (strpos($url, '?') !== FALSE ? '&' : '?') . 'focal_point_preview_value=' . $focal_point_value;
return $url;
}
示例7: getExternalUrl
/**
* Prepare Url objects to prevent exceptions by the URL generator.
*
* Helper function to get us an external URL if this is legal, and to catch
* the exception Drupal throws if this is not possible.
*
* In Drupal 8, the URL generator is very sensitive to how you set things
* up, and some functions, in particular LinkGeneratorTrait::l(), will throw
* exceptions if you deviate from what's expected. This function will raise
* the chances your URL will be valid, and not do this.
*
* @param \Drupal\file\Entity\File|string $file_object
* A file entity object.
*
* @return \Drupal\Core\Url
* A Url object that can be displayed as an internal URL.
*/
protected function getExternalUrl($file_object)
{
if ($file_object instanceof FileInterface) {
$uri = $file_object->getFileUri();
} else {
// A little tricky, since file.inc is a little inconsistent, but often
// this is a Uri.
$uri = file_create_url($file_object);
}
try {
// If we have been given a PHP stream URI, ask the stream itself if it
// knows how to create an external URL.
$wrapper = $this->streamWrapperManager->getViaUri($uri);
if ($wrapper) {
$external_url = $wrapper->getExternalUrl();
// Some streams may not have the concept of an external URL, so we
// check here to make sure, since the example assumes this.
if ($external_url) {
$url = Url::fromUri($external_url);
return $url;
}
} else {
$url = Url::fromUri($uri);
// If we did not throw on ::fromUri (you can), we return the URL.
return $url;
}
} catch (\Exception $e) {
return FALSE;
}
return FALSE;
}