Commit 6fe6fd3a authored by Al Munnings's avatar Al Munnings Committed by Jesus Manuel Olivas
Browse files

Resolve #3321787 "File resolver"

parent 95f86071
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -291,6 +291,13 @@ type ImageStyle {
  height: Int
}

type File {
  url: String
  name: String
  size: Int
  mime: String
}

type Text {
  format: String
  value: String
+63 −1
Original line number Diff line number Diff line
@@ -862,7 +862,47 @@ class DataManager {
                  ],
                ],
              ],

            ],
          ],
          'file' => [
            'type_sdl' => 'File',
            'name_sdl' => 'file',
            'isUnion' => FALSE,
            'isMultiple' => FALSE,
            'isRequired' => FALSE,
            'producers' => [
              [
                'type' => 'dataProducer',
                'id' => 'property_path',
                'map' => [
                  [
                    'id' => 'fromValue',
                    'key' => 'type',
                    'value' => 'entity:file',
                  ],
                  [
                    'id' => 'fromParent',
                    'key' => 'value',
                    'value' => NULL,
                  ],
                  [
                    'id' => 'fromValue',
                    'key' => 'path',
                    'value' => '{field_name}.entity',
                  ],
                ],
              ],
              [
                'type' => 'dataProducer',
                'id' => 'field_file',
                'map' => [
                  [
                    'id' => 'fromParent',
                    'key' => 'entity',
                    'value' => NULL,
                  ],
                ],
              ],
            ],
          ],
          # Metatags module
@@ -964,6 +1004,28 @@ class DataManager {
            ],
          ]
        ],
        'File' => [
          'type_sdl' => 'File',
          'isFielddable' => TRUE,
          'fields' => [
            'url' => [
              'name_sdl' => 'url',
              'type_sdl' => 'String',
            ],
            'name' => [
              'name_sdl' => 'name',
              'type_sdl' => 'String',
            ],
            'size' => [
              'name_sdl' => 'size',
              'type_sdl' => 'Int',
            ],
            'mime' => [
              'name_sdl' => 'mime',
              'type_sdl' => 'String',
            ],
          ]
        ],
        'Text' => [
          'type_sdl' => 'Text',
          'isFielddable' => TRUE,
+51 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\graphql_compose\Plugin\GraphQL\DataProducer\Field;

use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\file\FileInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;

/**
 * Returns an file style derivative of an file.
 *
 * @DataProducer(
 *   id = "field_file",
 *   name = @Translation("File"),
 *   description = @Translation("Returns file and derivatives."),
 *   produces = @ContextDefinition("any",
 *     label = @Translation("File properties")
 *   ),
 *   consumes = {
 *     "entity" = @ContextDefinition("entity",
 *       label = @Translation("Entity"),
 *       required = FALSE
 *     ),
 *   }
 * )
 */
class FieldFile extends DataProducerPluginBase {

  /**
   * {@inheritDoc}
   */
  public function resolve(FileInterface $entity = NULL, RefinableCacheableDependencyInterface $metadata) {
    if (!$entity) {
      return NULL;
    }

    $access = $entity->access('view', NULL, TRUE);
    $metadata->addCacheableDependency($access);
    if ($access->isAllowed()) {
      return [
        'url' => \Drupal::service('file_url_generator')->generateAbsoluteString($entity->getFileUri()),
        'name' => $entity->getFilename(),
        'size' => (int) $entity->getSize(),
        'mime' => $entity->getMimeType(),
      ];
    }

    return NULL;
  }

}