You are here

class FileEntityDataProvider in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 src/DataProvider/FileEntityDataProvider.php \Drupal\gutenberg\DataProvider\FileEntityDataProvider

Provides data for file entity type for Gutenberg editor.

@package Drupal\gutenberg\DataProvider

Hierarchy

Expanded class hierarchy of FileEntityDataProvider

1 string reference to 'FileEntityDataProvider'
gutenberg.services.yml in ./gutenberg.services.yml
gutenberg.services.yml
1 service uses FileEntityDataProvider
gutenberg.entity_type.data_provider.file in ./gutenberg.services.yml
Drupal\gutenberg\DataProvider\FileEntityDataProvider

File

src/DataProvider/FileEntityDataProvider.php, line 13

Namespace

Drupal\gutenberg\DataProvider
View source
class FileEntityDataProvider extends BaseDataProvider {
  use AssertMediaTrait;

  /**
   * {@inheritDoc}
   *
   * @see wp_prepare_attachment_for_js
   */
  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());
    $mime = $entity
      ->getMimeType();
    $mime_type_sections = explode('/', $mime);
    $mime_type = $mime_type_sections[0];
    $result = [
      'id' => (int) $entity
        ->id(),
      'title' => $file_data['title'] ?? '',
      'filename' => urldecode($entity
        ->getFilename()),
      'url' => $source_url,
      'link' => file_create_url($source_url),
      'alt' => $file_data['alt_text'] ?? '',
      'author' => $entity
        ->getOwnerId(),
      'description' => '',
      'caption' => $file_data['caption'] ?? '',
      'name' => '',
      'status' => 'inherit',
      'uploadedTo' => 0,
      'date' => date('c', $entity
        ->getCreatedTime()),
      'modified' => date('c', $entity
        ->getChangedTime()),
      /* 'menuOrder' => 0, */
      'mime' => $mime,
      'type' => $mime_type,
      'subtype' => $mime_type_sections[1],
      /* 'icon' => '', */

      /* 'dateFormatted' => date('F j, Y', $entity->getCreatedTime()), */

      /* 'nonces' => ['update' => FALSE, 'delete' => FALSE, 'edit' => FALSE], */

      /* 'editLink' => FALSE, */

      /* 'meta' => FALSE, */

      // Drupal specific properties below.
      'source_url' => $source_url,
      'media_type' => $mime_type,
      'mime_type' => $mime,
      'date_gmt' => date('c', $entity
        ->getCreatedTime()),
      // 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),
      ],
    ];
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssertMediaTrait::assertIsFileEntity protected function Assert if provided entity is type of FileInterface.
AssertMediaTrait::assertIsMediaEntity protected function Assert if provided entity is type of MediaInterface.
BaseDataProvider::$connection protected property Database connection.
BaseDataProvider::$entityTypeManager protected property Entity type manager instance.
BaseDataProvider::$imageFactory protected property Image factory instance.
BaseDataProvider::getFileData protected function Get file managed data of the provided file.
BaseDataProvider::getSizes protected function Get sizes of image styles for the source.
BaseDataProvider::__construct public function BaseDataProvider constructor.
FileEntityDataProvider::getData public function Overrides DataProviderInterface::getData