You are here

protected function MediaBridge::resolveFileItem in Open Social 10.1.x

Same name and namespace in other branches
  1. 10.3.x modules/custom/social_graphql/src/Plugin/GraphQL/DataProducer/MediaBridge.php \Drupal\social_graphql\Plugin\GraphQL\DataProducer\MediaBridge::resolveFileItem()
  2. 10.0.x modules/custom/social_graphql/src/Plugin/GraphQL/DataProducer/MediaBridge.php \Drupal\social_graphql\Plugin\GraphQL\DataProducer\MediaBridge::resolveFileItem()
  3. 10.2.x modules/custom/social_graphql/src/Plugin/GraphQL/DataProducer/MediaBridge.php \Drupal\social_graphql\Plugin\GraphQL\DataProducer\MediaBridge::resolveFileItem()

Resolve field for FileItem.

Parameters

\Drupal\file\Plugin\Field\FieldType\FileItem $file: The media source or file item.

string $field: The name of the data to return.

Return value

mixed The resolved value for a File Item.

2 calls to MediaBridge::resolveFileItem()
MediaBridge::resolve in modules/custom/social_graphql/src/Plugin/GraphQL/DataProducer/MediaBridge.php
Resolve the value for this data producer.
MediaBridge::resolveMediaEntity in modules/custom/social_graphql/src/Plugin/GraphQL/DataProducer/MediaBridge.php
Resolve field for media entity.

File

modules/custom/social_graphql/src/Plugin/GraphQL/DataProducer/MediaBridge.php, line 137

Class

MediaBridge
The media bridge provides a way to get data from File fields and Media.

Namespace

Drupal\social_graphql\Plugin\GraphQL\DataProducer

Code

protected function resolveFileItem(FileItem $file, $field) {
  switch ($field) {
    case 'id':
      if ($file
        ->isEmpty()) {
        return NULL;
      }
      $entity_id = $file
        ->get('target_id')
        ->getValue();
      $target_type = $file
        ->getFieldDefinition()
        ->getSetting('target_type');
      $resolver = $this->entityBuffer
        ->add($target_type, $entity_id);
      return new Deferred(function () use ($resolver) {

        /** @var \Drupal\file\Entity\File $file_entity */
        $file_entity = $resolver();
        return $file_entity
          ->uuid();
      });
    case 'url':
      if ($file
        ->isEmpty()) {
        return NULL;
      }
      $entity_id = $file
        ->get('target_id')
        ->getValue();
      $target_type = $file
        ->getFieldDefinition()
        ->getSetting('target_type');
      $resolver = $this->entityBuffer
        ->add($target_type, $entity_id);
      return new Deferred(function () use ($resolver) {

        /** @var \Drupal\file\Entity\File $file_entity */
        $file_entity = $resolver();
        return $file_entity
          ->createFileUrl(FALSE);
      });
    case 'title':
      return $file
        ->get('title')
        ->getString();
    case 'alt':
      return $file
        ->get('alt')
        ->getString();
    default:
      throw new \RuntimeException("Unsupported field for FileItem: '{$field}'.");
  }
}