class FileEntityDataProvider in Gutenberg 8
Same name and namespace in other branches
- 8.2 src/DataProvider/FileEntityDataProvider.php \Drupal\gutenberg\DataProvider\FileEntityDataProvider
Provides data for file entity type for Gutenberg editor.
@package Drupal\gutenberg\DataProvider
Hierarchy
- class \Drupal\gutenberg\DataProvider\BaseDataProvider implements DataProviderInterface
- class \Drupal\gutenberg\DataProvider\FileEntityDataProvider uses AssertMediaTrait
Expanded class hierarchy of FileEntityDataProvider
1 string reference to 'FileEntityDataProvider'
1 service uses FileEntityDataProvider
File
- src/
DataProvider/ FileEntityDataProvider.php, line 13
Namespace
Drupal\gutenberg\DataProviderView source
class FileEntityDataProvider extends BaseDataProvider {
use AssertMediaTrait;
/**
* {@inheritDoc}
*/
public function getData(ContentEntityInterface $entity, array $data = []) {
$this
->assertIsFileEntity($entity);
/** @var \Drupal\file\FileInterface $entity */
$uri = $entity
->getFileUri();
$source_url = file_url_transform_relative(file_create_url($uri));
$image = $this->imageFactory
->get($uri);
$file_data = $this
->getFileData($entity
->id());
$result = [
'id' => (int) $entity
->id(),
'link' => $source_url,
'source_url' => $source_url,
'url' => $source_url,
'media_type' => explode('/', $entity
->getMimeType())[0],
'mime_type' => $entity
->getMimeType(),
'author' => 1,
'status' => 'inherit',
'type' => 'attachment',
'date_gmt' => date('c', $entity
->getCreatedTime()),
'date' => date('c', $entity
->getCreatedTime()),
'title' => [
'raw' => $file_data['title'] ?? '',
'rendered' => $file_data['title'] ?? '',
],
// Prop used on inline-image.
'alt' => $file_data['alt_text'] ?? '',
'alt_text' => $file_data['alt_text'] ?? '',
// Prop used on inline-image.
'width' => $image
->getWidth(),
'data' => [
'entity_type' => 'file',
'entity_uuid' => $entity
->uuid(),
'image_style' => 'original',
],
'media_data' => $file_data,
'media_details' => [
'file' => $entity
->getFilename(),
'width' => $image
->getWidth(),
'height' => $image
->getHeight(),
'filesize' => $entity
->getSize(),
'image_meta' => [],
// See issue: https://www.drupal.org/project/gutenberg/issues/3035313
'sizes' => $this
->getSizes($source_url, $uri),
],
'caption' => [
'raw' => $file_data['caption'] ?? '',
'rendered' => $file_data['caption'] ?? '',
],
];
return $result;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AssertMediaTrait:: |
protected | function | Assert if provided entity is type of FileInterface. | |
AssertMediaTrait:: |
protected | function | Assert if provided entity is type of MediaInterface. | |
BaseDataProvider:: |
protected | property | Database connection. | |
BaseDataProvider:: |
protected | property | Entity type manager instance. | |
BaseDataProvider:: |
protected | property | Image factory instance. | |
BaseDataProvider:: |
protected | function | Get file managed data of the provided file. | |
BaseDataProvider:: |
protected | function | Get sizes of image styles for the source. | |
BaseDataProvider:: |
public | function | BaseDataProvider constructor. | |
FileEntityDataProvider:: |
public | function |
Returns array data. Overrides DataProviderInterface:: |